modifier.filter.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 
12 /**
13  * Filter an array of PersistentObject instances by the value of an attribute.
14  *
15  * Example:
16  * @code
17  * {foreach $projects|filter:"name":"/^a/i":false as $project}
18  * ...
19  * {/foreach}
20  * @endcode
21  *
22  * @param $objects The array of objects to filter
23  * @param $attribte The attribute to match or not match (see invert)
24  * @param $regex A regular expresssion the attribute's value must match
25  * @param $invert Boolean indicating if the expression should not match (optional, default:false)
26  * @return Array
27  */
28 function smarty_modifier_filter($objects, $attribute, $regex, $invert=false) {
29  return array_filter($objects, function($obj) use ($attribute, $regex, $invert) {
30  $match = preg_match($regex, $obj->getValue($attribute));
31  return $invert ? !$match : $match;
32  });
33 }
34 ?>