1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: bthapaliya |
6
|
|
|
* @since: 2016-10-16 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
namespace Chapi\Entity\Marathon; |
10
|
|
|
|
11
|
|
|
class MarathonEntityUtils |
12
|
|
|
{ |
13
|
37 |
|
public static function setPropertyIfExist($source, $target, $property) |
14
|
|
|
{ |
15
|
37 |
|
if (isset($source[$property]) && |
16
|
37 |
|
property_exists($target, $property)) { |
17
|
37 |
|
$target->{$property} = $source[$property]; |
18
|
37 |
|
return true; |
19
|
|
|
} |
20
|
5 |
|
return false; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Sets all possible properties in the class from $data. |
25
|
|
|
* If the type is array or object, then it is ignored if there is no conversion in $conversion_map. |
26
|
|
|
* @param $data |
27
|
|
|
* @param $target |
28
|
|
|
* @param $conversionMap |
29
|
|
|
* |
30
|
|
|
* @return array all fields in $data that weren't stored in $target |
31
|
|
|
*/ |
32
|
47 |
|
public static function setAllPossibleProperties($data, $target, $conversionMap = []) |
33
|
|
|
{ |
34
|
47 |
|
$unknownProperties = []; |
35
|
47 |
|
foreach ($data as $attributeName => $attributeValue) { |
36
|
|
|
// Don't set array or objects if no conversion method is specified. |
37
|
|
|
// Because this would need further type information to properly set. |
38
|
37 |
|
if (isset($conversionMap[$attributeName])) { |
39
|
26 |
|
$data[$attributeName] = $conversionMap[$attributeName]($attributeValue); |
40
|
|
|
} |
41
|
37 |
|
if (!self::setPropertyIfExist($data, $target, $attributeName)) { |
42
|
37 |
|
$unknownProperties[$attributeName] = $attributeValue; |
43
|
|
|
} |
44
|
|
|
} |
45
|
47 |
|
return $unknownProperties; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This is useful if you don't want an array or object to be skipped by setAllPossibleProperties(). |
50
|
|
|
*/ |
51
|
21 |
|
public static function dontConvert() { |
52
|
|
|
return function($data) { |
53
|
11 |
|
return $data; |
54
|
21 |
|
}; |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
public static function convertToArray() { |
58
|
|
|
return function($data) { |
59
|
2 |
|
return (array) $data; |
60
|
6 |
|
}; |
61
|
|
|
} |
62
|
|
|
|
63
|
11 |
|
public static function convertToObject() { |
64
|
|
|
return function($data) { |
65
|
3 |
|
return (object) $data; |
66
|
11 |
|
}; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* This is usefull for shorter and stable diff output. |
71
|
|
|
*/ |
72
|
21 |
|
public static function convertToSortedObject() { |
73
|
|
|
return function($data) { |
74
|
12 |
|
$a = (array) $data; // ksort is inplace, so we need a copy |
75
|
12 |
|
ksort($a); |
76
|
12 |
|
return (object) $a; |
77
|
21 |
|
}; |
78
|
|
|
} |
79
|
|
|
|
80
|
30 |
|
public static function convertToClass($class) { |
81
|
|
|
return function($data) use ($class) { |
82
|
8 |
|
return new $class((array) $data); |
83
|
30 |
|
}; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public static function convertToArrayOfClass($class) { |
87
|
27 |
|
return function($data) use ($class) { |
88
|
6 |
|
$array = []; |
89
|
6 |
|
if ($data !== null) { |
90
|
6 |
|
foreach ($data as $item) { |
91
|
2 |
|
$array[] = new $class((array) $item); |
92
|
|
|
} |
93
|
|
|
} |
94
|
6 |
|
return $array; |
95
|
27 |
|
}; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|