|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hospitalplugin\Twig; |
|
3
|
|
|
|
|
4
|
|
|
class GetPropertiesExtension extends \Twig_Extension |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
public function getFilters() |
|
8
|
|
|
{ |
|
9
|
|
|
return array( |
|
10
|
|
|
new \Twig_SimpleFilter('props', array( |
|
11
|
|
|
$this, |
|
12
|
|
|
'getPropsFilter' |
|
13
|
|
|
)) |
|
14
|
|
|
); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* return props of a class |
|
19
|
|
|
* @param unknown $class |
|
20
|
|
|
* @return boolean|multitype: |
|
|
|
|
|
|
21
|
|
|
*/ |
|
22
|
|
|
private static function getProps($class) |
|
23
|
|
|
{ |
|
24
|
|
|
if ($class == NULL) { |
|
25
|
|
|
return array(); |
|
26
|
|
|
} |
|
27
|
|
|
$class = new \ReflectionClass($class); |
|
28
|
|
|
$properties = array_filter($class->getProperties(), function ($prop) use($class) |
|
29
|
|
|
{ |
|
30
|
|
|
return $prop->getDeclaringClass()->name == $class->name; |
|
31
|
|
|
}); |
|
32
|
|
|
return $properties; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private static function exludeAndReplace($properties, $exclude, $replace, $titles) |
|
36
|
|
|
{ |
|
37
|
|
|
$propsArray = []; |
|
38
|
|
|
foreach ($properties as $value) { |
|
39
|
|
|
if (in_array($value->name, $exclude)) { |
|
40
|
|
|
continue; |
|
41
|
|
|
} |
|
42
|
|
|
if (array_key_exists($value->name, $replace)) { |
|
43
|
|
|
$name = $replace[$value->name]; |
|
44
|
|
|
} else { |
|
45
|
|
|
$name = $value->name; |
|
46
|
|
|
} |
|
47
|
|
|
if (array_key_exists($value->name, $titles)) { |
|
48
|
|
|
$title = $titles[$value->name]; |
|
49
|
|
|
} else { |
|
50
|
|
|
$title = strtoupper(substr($name, 0, 1)); |
|
51
|
|
|
} |
|
52
|
|
|
array_push($propsArray, array( |
|
53
|
|
|
'data' => $name, |
|
54
|
|
|
'title' => $title |
|
55
|
|
|
)); |
|
56
|
|
|
} |
|
57
|
|
|
return $propsArray; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* returns properties of an object excluding those that have name same as in $exclude array |
|
62
|
|
|
* |
|
63
|
|
|
* order of returned properties is: first parent class of $obj props then base class props of an $obj |
|
64
|
|
|
* |
|
65
|
|
|
* @param unknown $obj |
|
66
|
|
|
* @param unknown $exclude |
|
67
|
|
|
* @return boolean|string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getPropsFilter($obj, $exclude, $replace, $titles) |
|
70
|
|
|
{ |
|
71
|
|
|
// properties of an object, not array |
|
72
|
|
|
while (is_array($obj)) { |
|
73
|
|
|
$obj = $obj[0]; |
|
74
|
|
|
} |
|
75
|
|
|
$parentProperties = GetPropertiesExtension::getProps(get_parent_class($obj)); |
|
|
|
|
|
|
76
|
|
|
$properties = GetPropertiesExtension::getProps($obj); |
|
77
|
|
|
$propertiesMerged = array_merge($parentProperties, $properties); |
|
78
|
|
|
$propsArray = GetPropertiesExtension::exludeAndReplace($propertiesMerged, $exclude, $replace, $titles); |
|
79
|
|
|
return $propsArray; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getName() |
|
83
|
|
|
{ |
|
84
|
|
|
return 'get_properties_extension'; |
|
85
|
|
|
} |
|
86
|
|
|
} |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.