Skip to content

Commit cccab23

Browse files
authored
Merge pull request #70 from ltb-project/69-function-to-sort-attribute-values
Create method sortEntry
2 parents 8932be8 + d52eead commit cccab23

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/Ltb/Ldap.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,5 +513,63 @@ public function matchDn($dn, $dnAttribute, $filter, $base, $scope): bool {
513513
if ( $count == 1) { return true; }
514514
return false;
515515
}
516+
517+
/**
518+
* sort values of each attributes of an entry
519+
* @param array $entry: LDAP entry
520+
* @param array $attributes_map: confguration map
521+
* @return array: entry sorted
522+
*/
523+
public function sortEntry($entry, $attributes_map): array {
524+
525+
foreach ($entry as $attr => $values) {
526+
if ( is_array($values) && $values['count'] > 1 ) {
527+
528+
# Find key in attributes_map
529+
$attributes_map_filter = array_filter($attributes_map, function($v) use(&$attr) {
530+
return $v['attribute'] == "$attr";
531+
});
532+
if( count($attributes_map_filter) < 1 )
533+
{
534+
$k = "";
535+
error_log("WARN: no key found for attribute $attr in \$attributes_map");
536+
}
537+
elseif( count($attributes_map_filter) > 1 )
538+
{
539+
$k = array_key_first($attributes_map_filter);
540+
error_log("WARN: multiple keys found for attribute $attr in \$attributes_map, using first one: $k");
541+
}
542+
else
543+
{
544+
$k = array_key_first($attributes_map_filter);
545+
}
546+
547+
if(isset($attributes_map[$k]['sort']))
548+
{
549+
if($attributes_map[$k]['sort'] == "descending" )
550+
{
551+
# descending sort
552+
arsort($values);
553+
}
554+
else
555+
{
556+
# ascending sort
557+
asort($values);
558+
}
559+
}
560+
else
561+
{
562+
# if 'sort' param unset: default to ascending sort
563+
asort($values);
564+
}
565+
}
566+
if ( isset($values['count']) ) {
567+
unset($values['count']);
568+
}
569+
$entry[$attr] = $values;
570+
571+
}
572+
return $entry;
573+
}
516574
}
517575
?>

0 commit comments

Comments
 (0)