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
|
35 |
|
public static function setPropertyIfExist($source, $target, $property) |
14
|
|
|
{ |
15
|
35 |
|
if (isset($source[$property]) && |
16
|
35 |
|
property_exists($target, $property)) { |
17
|
35 |
|
$target->{$property} = $source[$property]; |
18
|
35 |
|
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
|
45 |
|
public static function setAllPossibleProperties($data, $target, $conversionMap = []) |
33
|
|
|
{ |
34
|
45 |
|
$unknownProperties = []; |
35
|
45 |
|
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
|
|
|
#if ((is_array($attributeValue) || is_object($attributeValue)) |
|
|
|
|
39
|
|
|
# && !isset($conversionMap[$attributeName])) { |
|
|
|
|
40
|
|
|
# $unknownProperties[$attributeName] = $attributeValue; |
|
|
|
|
41
|
|
|
# continue; |
42
|
|
|
#} |
43
|
35 |
|
if (isset($conversionMap[$attributeName])) { |
44
|
26 |
|
$data[$attributeName] = $conversionMap[$attributeName]($attributeValue); |
45
|
|
|
} |
46
|
35 |
|
if (!self::setPropertyIfExist($data, $target, $attributeName)) { |
47
|
35 |
|
$unknownProperties[$attributeName] = $attributeValue; |
48
|
|
|
} |
49
|
|
|
} |
50
|
45 |
|
return $unknownProperties; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* This is useful if you don't want an array or object to be skipped by setAllPossibleProperties(). |
55
|
|
|
*/ |
56
|
|
|
public static function noConv() { |
57
|
|
|
return function($data) { return $data; }; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function convArray() { |
61
|
|
|
return function($data) { return (array) $data; }; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function convObject() { |
65
|
|
|
return function($data) { return (object) $data; }; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* This is usefull for shorter and stable diff output. |
70
|
|
|
*/ |
71
|
21 |
|
public static function convSortedObject() { |
72
|
|
|
return function($data) { |
73
|
12 |
|
$a = (array) $data; // ksort is inplace, so we need a copy |
74
|
12 |
|
ksort($a); |
75
|
12 |
|
return (object) $a; |
76
|
21 |
|
}; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function convClass($class) { |
80
|
|
|
return function($data) use ($class) { return new $class((array) $data); }; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function convArrayOfClass($class) { |
84
|
27 |
|
return function($data) use ($class) { |
85
|
6 |
|
$return = []; |
86
|
6 |
|
if ($data !== null) { |
87
|
6 |
|
foreach ($data as $item) { |
88
|
2 |
|
$return[] = new $class((array) $item); |
89
|
|
|
} |
90
|
|
|
} |
91
|
6 |
|
return $return; |
92
|
27 |
|
}; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.