|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Happyr\SerializerBundle\PropertyManager; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Extract attributes from object. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
class AttributeExtractor |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
private $attributesCache = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Gets and caches attributes for this class and context. |
|
19
|
|
|
* |
|
20
|
|
|
* @param object|string $object |
|
21
|
|
|
* |
|
22
|
|
|
* @return string[] |
|
23
|
|
|
*/ |
|
24
|
23 |
|
public function getAttributes($object) |
|
25
|
|
|
{ |
|
26
|
23 |
|
if (is_string($object)) { |
|
27
|
23 |
|
$class = $object; |
|
28
|
23 |
|
} else { |
|
29
|
11 |
|
$class = get_class($object); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// get cache key |
|
33
|
23 |
|
if (false !== $key = $this->getCacheKey($class)) { |
|
34
|
23 |
|
if (isset($this->attributesCache[$key])) { |
|
35
|
1 |
|
return $this->attributesCache[$key]; |
|
36
|
|
|
} |
|
37
|
23 |
|
} |
|
38
|
|
|
|
|
39
|
23 |
|
return $this->attributesCache[$key] = $this->extractAttributes($class); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Extracts attributes for this class and context. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $class |
|
46
|
|
|
* |
|
47
|
|
|
* @return string[] |
|
48
|
|
|
*/ |
|
49
|
23 |
|
private function extractAttributes($class) |
|
50
|
|
|
{ |
|
51
|
|
|
// If not using groups, detect manually |
|
52
|
23 |
|
$attributes = ['property' => [], 'method' => []]; |
|
53
|
|
|
|
|
54
|
|
|
// methods |
|
55
|
23 |
|
$reflClass = new \ReflectionClass($class); |
|
56
|
23 |
|
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) { |
|
57
|
|
|
if ( |
|
58
|
23 |
|
$reflMethod->getNumberOfRequiredParameters() !== 0 || |
|
59
|
23 |
|
$reflMethod->isStatic() || |
|
60
|
23 |
|
$reflMethod->isConstructor() || |
|
61
|
23 |
|
$reflMethod->isDestructor() |
|
62
|
23 |
|
) { |
|
63
|
23 |
|
continue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
23 |
|
$name = $reflMethod->name; |
|
67
|
|
|
|
|
68
|
23 |
|
$attributes['method'][$name] = true; |
|
69
|
23 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
// properties |
|
72
|
23 |
|
foreach ($reflClass->getProperties() as $reflProperty) { |
|
73
|
23 |
|
if ($reflProperty->isStatic()) { |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
23 |
|
$attributes['property'][$reflProperty->name] = true; |
|
78
|
23 |
|
} |
|
79
|
|
|
|
|
80
|
23 |
|
return $attributes; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get a good cache key for this class. |
|
85
|
|
|
* |
|
86
|
|
|
* @param $class |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool|string |
|
89
|
|
|
*/ |
|
90
|
23 |
|
private function getCacheKey($class) |
|
91
|
|
|
{ |
|
92
|
|
|
try { |
|
93
|
23 |
|
return md5($class); |
|
94
|
|
|
} catch (\Exception $e) { |
|
95
|
|
|
// The context cannot be serialized, skip the cache |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.