21 public static function sort(array $array , callable $callback):array{
22 \usort($array,$callback);
34 public static function groupsBy(array $objects, array $gbCallbacks, ?callable $lineCallback=
null, ?
bool $sort=
null):array {
35 if(\count($gbCallbacks)==0) {
38 $objects=self::groupBy($objects, current($gbCallbacks), $lineCallback, $sort);
39 \array_shift($gbCallbacks);
41 foreach ($objects as $k=>$v){
42 $result[$k]=self::groupsBy($v,$gbCallbacks);
55 public static function groupBy(array $objects, callable $gbCallback, ?callable $lineCallback=
null, ?
bool $sort=
null):array {
57 $objects=self::sort($objects,
function ($item1, $item2) use ($gbCallback){
58 return $gbCallback($item1)<=>$gbCallback($item2);
60 }elseif($sort===
false){
61 $objects=self::sort($objects,
function ($item1, $item2) use ($gbCallback){
62 return $gbCallback($item2)<=>$gbCallback($item1);
67 foreach ($objects as $line){
68 if($groupBy!==($nGB=$gbCallback($line))){
71 $result[$groupBy][]=(isset($lineCallback))?$lineCallback($line):$line;
83 public static function asKeyValues(array $objects, $keyFunction = NULL, $valueFunction = NULL) {
85 if (isset($valueFunction) ===
false) {
86 $valueFunction =
'__toString';
88 if (isset($keyFunction) ===
false) {
89 foreach ($objects as $object) {
90 $result[] = self::callFunction($object, $valueFunction);
93 foreach ($objects as $object) {
94 $result[self::callFunction($object, $keyFunction)] = self::callFunction($object, $valueFunction);
106 public static function find(?array $objects,callable $callback){
107 if(\is_array($objects)) {
108 foreach ($objects as $o){
124 public static function findBy(?array $objects,$value,
string $property=
'id'){
125 if(!\is_array($objects)){
128 $get=
'get'.\ucfirst($property);
129 foreach ($objects as $index=>$o) {
130 if($value==$o->$get()){
144 public static function findById(?array $objects,$idValue){
145 if(!is_array($objects) && \count($objects)>0){
148 $property=OrmUtils::getFirstKey(\get_class(\current($objects)));
149 return self::findBy($objects, $idValue,$property);
159 public static function contains(?array $objects,callable $callback):bool{
160 return self::find($objects, $callback)!==null;
171 public static function containsBy(?array $objects,
object $object,
string $property=
'id'):bool{
175 $get=
'get'.\ucfirst($property);
176 $objectValue=$object->$get();
177 return self::findBy($objects, $objectValue,$property)!==
null;
187 public static function containsById(?array $objects,
object $object):bool {
191 $property=OrmUtils::getFirstKey(\get_class($object));
192 return self::containsBy($objects, $object,$property);
202 public static function remove(?array $objects,callable $callback):array{
203 foreach ($objects as $index=>$o) {
205 unset($objects[$index]);
219 public static function removeBy(?array $objects,
object $object,
string $property=
'id'):?array{
220 if(!is_array($objects) || $object==null){
223 $get=
'get'.\ucfirst($property);
224 $objectValue=$object->$get();
225 foreach ($objects as $index=>$o) {
226 if($objectValue===$o->$get()){
227 unset($objects[$index]);
241 public static function removeAllBy(?array $objects,
object $object,
string $property=
'id'):?array{
242 if(!is_array($objects) || $object==null){
245 $get=
'get'.\ucfirst($property);
246 $objectValue=$object->$get();
248 foreach ($objects as $index=>$o) {
249 if($objectValue===$o->$get()){
253 foreach ($toRemove as $index){
254 unset($objects[$index]);
259 public static function compute(?array $objects,callable $callable,callable $computeCall){
262 foreach ($objects as $object) {
263 $computeCall($res, $callable($object));
270 $getter=
'get'.\ucfirst($propertyName);
271 return self::compute($objects,fn($o)=>$o->$getter(),fn(&$r,$o)=>$r+=$o);
274 public static function computeSum(?array $objects,callable $callable){
275 return self::compute($objects,$callable,fn(&$r,$o)=>$r+=$o);
284 public static function removeAll(?array $objects,callable $callback):array{
286 foreach ($objects as $index=>$o) {
291 foreach ($toRemove as $index){
292 unset($objects[$index]);
301 public static function asArray(array $objects):array{
303 foreach ($objects as $index=>$o) {
304 $result[$index]=$o->_rest??[];
314 public static function asJson(array $objects,
int $options=0):string{
316 foreach ($objects as $index=>$o) {
317 $result[$index]=$o->_rest??[];
319 return \json_encode($result,$options);
329 $accessors=self::getAccessors($properties);
330 foreach ($objects as $object){
332 foreach ($accessors as $prop=>$get){
333 $or[$prop]=$object->$get();
346 public static function asJsonProperties(array $objects,array $properties,
int $options=0):string{
347 return \json_encode(self::asArrayProperties($objects, $properties),$options);
352 foreach ($properties as $property){
353 $res[$property]=$prefix.\ucfirst($property);
365 if (\is_string($callback)){
366 return \call_user_func(array(
371 if (\is_callable($callback)) {
372 return $callback($object);
Object/relational mapping utilities.
Ubiquity\utils\models$UArrayModels This class is part of Ubiquity.
static groupBy(array $objects, callable $gbCallback, ?callable $lineCallback=null, ?bool $sort=null)
Groups an array using a user defined comparison function.
static computeSumProperty(?array $objects, string $propertyName)
static sort(array $array, callable $callback)
Returns a sorted array using a user defined comparison function.
static getAccessors($properties, $prefix='get')
static removeAll(?array $objects, callable $callback)
Removes all the occurrences of the array satisfying the callback.
static removeBy(?array $objects, object $object, string $property='id')
Removes an object from an array of objects using one of its properties.
static asKeyValues(array $objects, $keyFunction=NULL, $valueFunction=NULL)
Returns an associative array of key/values from an array of objects.
static find(?array $objects, callable $callback)
Finds and returns the first occurrence of the array satisfying the callback.
static callFunction($object, $callback)
static containsBy(?array $objects, object $object, string $property='id')
Checks if an object exist in an array of objects using one of the object property.
static groupsBy(array $objects, array $gbCallbacks, ?callable $lineCallback=null, ?bool $sort=null)
Groups an array using an array of user defined comparison functions.
static removeAllBy(?array $objects, object $object, string $property='id')
Removes objects from an array of objects using one of their properties.
static contains(?array $objects, callable $callback)
Checks if an object exist in an array of objects satisfying the callback.
static asArrayProperties(array $objects, array $properties)
static findById(?array $objects, $idValue)
Finds and returns the first occurrence of the array using the objects id.
static asJsonProperties(array $objects, array $properties, int $options=0)
static findBy(?array $objects, $value, string $property='id')
Finds and returns the first occurrence of the array using one of the objects properties.
static asArray(array $objects)
static compute(?array $objects, callable $callable, callable $computeCall)
static containsById(?array $objects, object $object)
Checks if an object exist in an array of objects using the object id.
static asJson(array $objects, int $options=0)
static computeSum(?array $objects, callable $callable)