|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Model; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\Repository\SingleRow; |
|
6
|
|
|
use stdClass; |
|
7
|
|
|
|
|
8
|
|
|
class BinderObject implements DumpToArrayInterface |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Bind the properties from an object to the properties matching to the current instance |
|
13
|
|
|
* |
|
14
|
|
|
* @param mixed $source |
|
15
|
|
|
*/ |
|
16
|
|
|
public function bind($source) |
|
17
|
|
|
{ |
|
18
|
|
|
self::bindObject($source, $this); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Bind the properties from the current instance to the properties matching to an object |
|
23
|
|
|
* |
|
24
|
|
|
* @param mixed $target |
|
25
|
|
|
*/ |
|
26
|
|
|
public function bindTo($target) |
|
27
|
|
|
{ |
|
28
|
|
|
self::bindObject($this, $target); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get all properties from the current instance as an associative array |
|
33
|
|
|
* |
|
34
|
|
|
* @return array The object properties as array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function toArray() |
|
37
|
|
|
{ |
|
38
|
|
|
return self::toArrayFrom($this); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Bind the properties from a source object to the properties matching to a target object |
|
43
|
|
|
* |
|
44
|
|
|
* @param mixed $source |
|
45
|
|
|
* @param mixed $target |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function bindObject($source, $target) |
|
48
|
|
|
{ |
|
49
|
|
|
$sourceArray = self::toArrayFrom($source); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($sourceArray as $propName => $value) { |
|
52
|
|
|
self::setPropValue($target, $propName, $value); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get all properties from a source object as an associative array |
|
58
|
|
|
* |
|
59
|
|
|
* @param mixed $source |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function toArrayFrom($source) |
|
63
|
|
|
{ |
|
64
|
|
|
// Prepare the source object type |
|
65
|
|
|
$object = new SerializerObject($source); |
|
66
|
|
|
$object->setStopFirstLevel(true); |
|
67
|
|
|
return $object->build(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected static $propNameLower = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set the property value |
|
74
|
|
|
* |
|
75
|
|
|
* @param mixed $obj |
|
76
|
|
|
* @param string $propName |
|
77
|
|
|
* @param string $value |
|
78
|
|
|
*/ |
|
79
|
|
|
protected static function setPropValue($obj, $propName, $value) |
|
80
|
|
|
{ |
|
81
|
|
|
if ($obj instanceof SingleRow) { |
|
82
|
|
|
$obj->setField($propName, $value); |
|
83
|
|
|
} else if (method_exists($obj, 'set' . $propName)) { |
|
84
|
|
|
$obj->{'set' . $propName}($value); |
|
85
|
|
|
} elseif (isset($obj->{$propName}) || $obj instanceof stdClass) { |
|
86
|
|
|
$obj->{$propName} = $value; |
|
87
|
|
|
} else { |
|
88
|
|
|
// Check if source property have property case name different from target |
|
89
|
|
|
$className = get_class($obj); |
|
90
|
|
|
if (!isset(self::$propNameLower[$className])) { |
|
91
|
|
|
self::$propNameLower[$className] = []; |
|
92
|
|
|
|
|
93
|
|
|
$classVars = get_class_vars($className); |
|
94
|
|
|
foreach ($classVars as $varKey => $varValue) { |
|
95
|
|
|
self::$propNameLower[$className][strtolower($varKey)] = $varKey; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$propLower = strtolower($propName); |
|
100
|
|
|
if (isset(self::$propNameLower[$className][$propLower])) { |
|
101
|
|
|
$obj->{self::$propNameLower[$className][$propLower]} = $value; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|