Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UModel.php
Go to the documentation of this file.
1<?php
2
3
5
6
7
15class UModel {
16 private static function getSet(string $propertyName):array{
17 $propertyName=\ucfirst($propertyName);
18 return ['get'.$propertyName,'set'.$propertyName];
19 }
20
25 public static function toggleProperty(object $object, string $propertyName):void{
26 list($getter,$setter)=self::getSet($propertyName);
27 $object->$setter(!($object->$getter()));
28 }
29
35 public static function incProperty(object $object, string $propertyName,$val=1):void{
36 list($getter,$setter)=self::getSet($propertyName);
37 $object->$setter($object->$getter()+$val);
38 }
39
45 public static function decProperty(object $object, string $propertyName,$val=1):void{
46 list($getter,$setter)=self::getSet($propertyName);
47 $object->$setter($object->$getter()-$val);
48 }
49
56 public static function concatProperty(object $object, string $propertyName,$val,bool $after=true):void{
57 list($getter,$setter)=self::getSet($propertyName);
58 if($after){
59 $object->$setter($object->$getter().$val);
60 }else{
61 $object->$setter($val.($object->$getter()));
62 }
63 }
64
70 public static function addTo(object $object,string $propertyName,$val):void{
71 list($getter,$setter)=self::getSet($propertyName);
72 $array=$object->$getter();
73 $array[]=$val;
74 $object->$setter($array);
75 }
76
83 public static function removeFrom(object $object,string $propertyName,$val){
84 list($getter,$setter)=self::getSet($propertyName);
85 $array=$object->$getter();
86 if(($index=\array_search($val, $array))!==false){
87 $r=$array[$index];
88 unset($array[$index]);
89 $object->$setter($array);
90 return $r;
91 }
92 return false;
93 }
94
101 public static function removeFromByIndex(object $object,string $propertyName,$index){
102 list($getter,$setter)=self::getSet($propertyName);
103 $array=$object->$getter();
104 if(isset($array[$index])){
105 $r=$array[$index];
106 unset($array[$index]);
107 $object->$setter($array);
108 return $r;
109 }
110 return false;
111 }
112
117 public static function asArray(object $object):array{
118 return $object->_rest??[];
119 }
120
126 public static function asJson(object $object,int $options=0):string{
127 return \json_encode($object->_rest??[],$options);
128 }
129
135 public static function asArrayProperties(object $object,array $properties):array{
136 $res=[];
137 foreach ($properties as $property){
138 $get='get'.\ucfirst($property);
139 $res[$property]=$object->$get();
140 }
141 return $res;
142 }
143
150 public static function asJsonProperties(object $object,array $properties,int $options=0):string{
151 return \json_encode(self::asArrayProperties($object, $properties),$options);
152 }
153
161 public static function equals(object $object,?object $toObject,string $property='id'){
162 if($toObject==null){
163 return false;
164 }
165 $get='get'.\ucfirst($property);
166 return $object->$get()===$toObject->$get();
167 }
168}
Ubiquity\utils\models$UModel This class is part of Ubiquity.
Definition UModel.php:15
static removeFromByIndex(object $object, string $propertyName, $index)
Definition UModel.php:101
static asArray(object $object)
Definition UModel.php:117
static asArrayProperties(object $object, array $properties)
Definition UModel.php:135
static toggleProperty(object $object, string $propertyName)
Definition UModel.php:25
static addTo(object $object, string $propertyName, $val)
Definition UModel.php:70
static removeFrom(object $object, string $propertyName, $val)
Definition UModel.php:83
static asJson(object $object, int $options=0)
Definition UModel.php:126
static asJsonProperties(object $object, array $properties, int $options=0)
Definition UModel.php:150
static getSet(string $propertyName)
Definition UModel.php:16
static decProperty(object $object, string $propertyName, $val=1)
Definition UModel.php:45
static concatProperty(object $object, string $propertyName, $val, bool $after=true)
Definition UModel.php:56
static equals(object $object,?object $toObject, string $property='id')
Determines if 2 objects are equal.
Definition UModel.php:161
static incProperty(object $object, string $propertyName, $val=1)
Definition UModel.php:35