Skip to content

Commit

Permalink
#7 - add support for regexp into split and dotted profile field names
Browse files Browse the repository at this point in the history
  • Loading branch information
jpahullo committed Mar 14, 2024
1 parent a18d438 commit 2d7f5dd
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,38 @@ public function user_profile_hook(&$user) {

// Find %split function.
foreach ($mainrulearray as $item) {
if (preg_match('/(?<full>%split\((?<fld>\w*)\|(?<delim>.{1,5})\))/', $item, $splitparams)) {
if (preg_match('/(?<full>%split\((?<fld>.*)\|(?<delim>.{1,5})\))/', $item, $splitparams)) {
// Split!
$parts = explode($splitparams['delim'], $userprofiledata[$splitparams['fld']]);
$splitparams['fldkey'] = sha1($splitparams['fld']);
$withruleinternalreplacement = preg_match('/(?<fld>.*)#(?<pattern>.*)#(?<replacement>\$\d{1,2})/', $splitparams['fld'],$ruleinternalreplacement);
if ($withruleinternalreplacement) {
$splitparams['fld'] = $ruleinternalreplacement['fld'];
}

// Supporting . notation in fld definition
$fieldvalue = '';
$userprofileaccessors = preg_split('/\./', $splitparams['fld'], -1, PREG_SPLIT_NO_EMPTY);
if (count($userprofileaccessors) > 1) {
$userprofiledataaccessed = $userprofiledata;
foreach ($userprofileaccessors as $accessor) {
if (!isset($userprofiledataaccessed[$accessor])) {
$fieldvalue = '';
break;
}
$userprofiledataaccessed = $userprofiledataaccessed[$accessor];
$fieldvalue = $userprofiledataaccessed;
}
} else {
$fieldvalue = $userprofiledata[$splitparams['fld']];
}
$parts = preg_split('/' . $splitparams['delim'] . '/', $fieldvalue, -1, PREG_SPLIT_NO_EMPTY);
foreach ($parts as $key => $val) {
$userprofiledata[$splitparams['fld']."_$key"] = $val;
$templates[] = strtr($item, array("{$splitparams['full']}" => "{{ ".$splitparams['fld']."_$key }}"));
$val = trim($val);
if ($withruleinternalreplacement) {
$val = preg_replace('/'.$ruleinternalreplacement['pattern'].'/i', $ruleinternalreplacement['replacement'], $val);
}
$userprofiledata[$splitparams['fldkey']."_$key"] = $val;
$templates[] = strtr($item, array("{$splitparams['full']}" => "{{ ".$splitparams['fldkey']."_$key }}"));
}
} else {
$templates[] = $item;
Expand Down

0 comments on commit 2d7f5dd

Please sign in to comment.