Passed
Push — master ( 439db2...e4b223 )
by Jean-Christophe
10:58
created

UModel   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 75.81%

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 18
eloc 46
c 5
b 0
f 3
dl 0
loc 152
ccs 47
cts 62
cp 0.7581
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A asArray() 0 2 1
A asJson() 0 2 1
A asJsonProperties() 0 2 1
A getSet() 0 3 1
A asArrayProperties() 0 7 2
A toggleProperty() 0 3 1
A decProperty() 0 3 1
A incProperty() 0 3 1
A concatProperty() 0 6 2
A addTo() 0 5 1
A removeFrom() 0 10 2
A equals() 0 6 2
A removeFromByIndex() 0 10 2
1
<?php
2
3
4
namespace Ubiquity\utils\models;
5
6
7
8
/**
9
 * Ubiquity\utils\models$UModel
10
 * This class is part of Ubiquity
11
 * @author jc
12
 * @version 1.0.0
13
 *
14
 */
15
class UModel {
16 6
	private static function getSet(string $propertyName):array{
17 6
		$propertyName=\ucfirst($propertyName);
18 6
		return ['get'.$propertyName,'set'.$propertyName];
19
	}
20
	
21
	/**
22
	 * @param object $object
23
	 * @param string $propertyName
24
	 */
25 1
	public static function toggleProperty(object $object, string $propertyName):void{
26 1
		list($getter,$setter)=self::getSet($propertyName);
27 1
		$object->$setter(!($object->$getter()));
28 1
	}
29
30
	/**
31
	 * @param object $object
32
	 * @param string $propertyName
33
	 * @param number $val
34
	 */
35 1
	public static function incProperty(object $object, string $propertyName,$val=1):void{
36 1
		list($getter,$setter)=self::getSet($propertyName);
37 1
		$object->$setter($object->$getter()+$val);
38 1
	}
39
40
	/**
41
	 * @param object $object
42
	 * @param string $propertyName
43
	 * @param number $val
44
	 */
45 1
	public static function decProperty(object $object, string $propertyName,$val=1):void{
46 1
		list($getter,$setter)=self::getSet($propertyName);
47 1
		$object->$setter($object->$getter()-$val);
48 1
	}
49
	
50
	/**
51
	 * @param object $object
52
	 * @param string $propertyName
53
	 * @param mixed $val
54
	 * @param bool $after
55
	 */
56 1
	public static function concatProperty(object $object, string $propertyName,$val,bool $after=true):void{
57 1
		list($getter,$setter)=self::getSet($propertyName);
58 1
		if($after){
59 1
			$object->$setter($object->$getter().$val);
60
		}else{
61 1
			$object->$setter($val.($object->$getter()));
62
		}
63 1
	}
64
	
65
	/**
66
	 * @param object $object
67
	 * @param string $propertyName
68
	 * @param mixed $val
69
	 */
70 1
	public static function addTo(object $object,string $propertyName,$val):void{
71 1
		list($getter,$setter)=self::getSet($propertyName);
72 1
		$array=$object->$getter();
73 1
		$array[]=$val;
74 1
		$object->$setter($array);
75 1
	}
76
	
77
	/**
78
	 * @param object $object
79
	 * @param string $propertyName
80
	 * @param mixed $val
81
	 * @return mixed
82
	 */
83 1
	public static function removeFrom(object $object,string $propertyName,$val){
84 1
		list($getter,$setter)=self::getSet($propertyName);
85 1
		$array=$object->$getter();
86 1
		if(($index=\array_search($val, $array))!==false){
87 1
			$r=$array[$index];
88 1
			unset($array[$index]);
89 1
			$object->$setter($array);
90 1
			return $r;
91
		}
92
		return false;
93
	}
94
	
95
	/**
96
	 * @param object $object
97
	 * @param string $propertyName
98
	 * @param mixed $index
99
	 * @return mixed
100
	 */
101 1
	public static function removeFromByIndex(object $object,string $propertyName,$index){
102 1
		list($getter,$setter)=self::getSet($propertyName);
103 1
		$array=$object->$getter();
104 1
		if(isset($array[$index])){
105 1
			$r=$array[$index];
106 1
			unset($array[$index]);
107 1
			$object->$setter($array);
108 1
			return $r;
109
		}
110
		return false;
111
	}
112
	
113
	/**
114
	 * @param object $object
115
	 * @return array
116
	 */
117
	public static function asArray(object $object):array{
118
		return $object->_rest??[];
119
	}
120
	
121
	/**
122
	 * @param object $object
123
	 * @param int $options
124
	 * @return string
125
	 */
126
	public static function asJson(object $object,int $options=null):string{
127
		return \json_encode($object->_rest??[],$options);
128
	}
129
	
130
	/**
131
	 * @param object $object
132
	 * @param array $properties
133
	 * @return array
134
	 */
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
	
144
	/**
145
	 * @param object $object
146
	 * @param array $properties
147
	 * @param int $options
148
	 * @return string
149
	 */
150
	public static function asJsonProperties(object $object,array $properties,int $options=null):string{
151
		return \json_encode(self::asArrayProperties($object, $properties),$options);
152
	}
153
	
154
	/**
155
	 * Determines if 2 objects are equal.
156
	 * @param object $object
157
	 * @param object $toObject
158
	 * @param string $property
159
	 * @return boolean
160
	 */
161 1
	public static function equals(object $object,?object $toObject,string $property='id'){
162 1
		if($toObject==null){
163
			return false;
164
		}
165 1
		$get='get'.\ucfirst($property);
166 1
		return $object->$get()===$toObject->$get();
167
	}
168
}