-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjects.php
More file actions
145 lines (134 loc) Β· 3.32 KB
/
Objects.php
File metadata and controls
145 lines (134 loc) Β· 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Vector\Lib;
use Vector\Core\Curry;
use Vector\Core\Exception\UndefinedPropertyException;
use Vector\Core\Module;
class Objects
{
use Module;
/**
* Set Property
*
* Sets a property on the object
*
* @param String $key Property to set
* @param mixed $val Value
* @param Object $obj Object
* @return Object $obj Object
* @example
* Object::setValue('value', new stdClass(), 'hi!');
* // object(stdClass)#1 (1) {
* // ["value"]=>
* // string(3) "hi!"
* // }
*
* @type String -> a -> Object a -> Object a
*
*/
#[Curry]
protected static function setProp($key, $val, $obj)
{
$newObj = clone $obj;
/** @noinspection PhpVariableVariableInspection */
$newObj->$key = $val;
return $newObj;
}
/**
* Assign Properties
*
* Set/Update properties on the object using a key/value array
*
* @param $props
* @param $objOriginal
* @return mixed
* @example
* Object::assign(['value' => 'hi!'], new stdClass);
* // object(stdClass)#1 (1) {
* // ["value"]=>
* // string(3) "hi!"
* // }
*
* @type array props -> Object objOriginal -> Object objUpdated
*
*/
#[Curry]
protected static function assign($props, $objOriginal)
{
$obj = clone $objOriginal;
return Arrays::reduce(
function ($obj, $setter) {
return $setter($obj);
},
$obj,
Arrays::mapIndexed(Lambda::flip(self::using('setProp')), $props)
);
}
/**
* Get Property
*
* Gets a property on the object
*
* @param String $prop Property to get
* @param Object $obj Object
* @return mixed $val value
* @throws UndefinedPropertyException
* @example
* $obj = new stdClass();
* $obj->value = 'hi!';
* Object::getValue('value', $obj); // 'hi!'
*
* @type String -> Object a -> a
*
*/
#[Curry]
protected static function getProp($prop, $obj)
{
if (! isset($obj->{$prop})) {
throw new UndefinedPropertyException("'getProp' function tried to access undefined property '{$prop}'");
}
return $obj->{$prop};
}
/**
* Invoke Method
*
* Invokes a method on the object
*
* @param String $method Method to call
* @param Object $obj Object
* @return mixed $val value
* @example
* $person = new stdObject(array(
* "sayHi" => function() {
* return "hi!";
* }
* ));
* Object::invokeMethod('sayHi', $person); // 'hi!'
*
* @type String -> Obj a -> mixed
*
*/
#[Curry]
protected static function invokeMethod($method, $obj)
{
return call_user_func([$obj, $method]);
}
/**
* Is Instance Of
*
* Checks if the object is an instance of the specified class
*
* @param String $expected Class
* @param Object $given Object
* @return mixed $val value
* @example
* Object::isInstanceOf('stdClass', (new stdClass())); // true
*
* @type String -> Obj a -> mixed
*
*/
#[Curry]
protected static function isInstanceOf($expected, $given)
{
return $given instanceof $expected;
}
}