|
1 | 1 | <?php |
2 | 2 | namespace Ubiquity\devtools\utils\arrays; |
| 3 | + |
3 | 4 | use Ubiquity\utils\base\UString; |
4 | 5 |
|
5 | | -class ReflectArray extends BaseArray{ |
| 6 | +class ReflectArray extends BaseArray { |
| 7 | + |
6 | 8 | private $objects; |
| 9 | + |
7 | 10 | private $properties; |
8 | 11 |
|
9 | | - public function parse(){ |
10 | | - $object=current($this->objects); |
11 | | - if(!isset($object) || !$object){ |
12 | | - return [['Nothing to display']]; |
| 12 | + public function parse() { |
| 13 | + $object = current($this->objects); |
| 14 | + if (! isset($object) || ! $object) { |
| 15 | + return [ |
| 16 | + [ |
| 17 | + 'Nothing to display' |
| 18 | + ] |
| 19 | + ]; |
13 | 20 | } |
14 | | - if(!is_array($this->properties)){ |
15 | | - $this->properties=$this->getProperties($object); |
| 21 | + if (! is_array($this->properties)) { |
| 22 | + $this->properties = $this->getProperties($object); |
16 | 23 | } |
17 | | - $result=[$this->properties]; |
18 | | - $r=new \ReflectionClass($object); |
| 24 | + $result = [ |
| 25 | + $this->properties |
| 26 | + ]; |
| 27 | + $r = new \ReflectionClass($object); |
19 | 28 |
|
20 | | - foreach ($this->objects as $object){ |
21 | | - if(is_array($object)){ |
22 | | - $result[]=$object; |
23 | | - }elseif(is_object($object)){ |
24 | | - $row=[]; |
25 | | - foreach ($this->properties as $prop){ |
26 | | - if($r->hasProperty($prop)){ |
27 | | - $property=$r->getProperty($prop); |
| 29 | + foreach ($this->objects as $object) { |
| 30 | + if (is_array($object)) { |
| 31 | + $result[] = $object; |
| 32 | + } elseif (is_object($object)) { |
| 33 | + $row = []; |
| 34 | + foreach ($this->properties as $prop) { |
| 35 | + if ($r->hasProperty($prop)) { |
| 36 | + $property = $r->getProperty($prop); |
28 | 37 | $property->setAccessible(true); |
29 | | - $row[]=$this->parseValue($property->getValue($object)); |
30 | | - }else{ |
31 | | - $this->messages[$prop]="Property {$prop} does not exists!"; |
32 | | - $row[]=''; |
| 38 | + $row[] = $this->parseValue($property->getValue($object)); |
| 39 | + } else { |
| 40 | + $this->messages[$prop] = "Property {$prop} does not exists!"; |
| 41 | + $row[] = ''; |
33 | 42 | } |
34 | 43 | } |
35 | | - $result[]=$row; |
| 44 | + $result[] = $row; |
36 | 45 | } |
37 | 46 | } |
38 | 47 | return $result; |
39 | 48 | } |
40 | 49 |
|
41 | | - private function parseValue($value){ |
42 | | - $result="-"; |
43 | | - if(is_array($value)){ |
| 50 | + private function parseValue($value) { |
| 51 | + $result = "-"; |
| 52 | + if (is_array($value)) { |
44 | 53 | return $this->parseArray($value); |
45 | | - }elseif(UString::isValid($value)){ |
46 | | - $result=var_export($value,true); |
47 | | - }else{ |
48 | | - $result=$value; |
| 54 | + } elseif (UString::isValid($value)) { |
| 55 | + $result = var_export($value, true); |
| 56 | + } elseif (is_callable($value)) { |
| 57 | + $result = '(x)=>{}'; |
| 58 | + } else { |
| 59 | + $result = $value; |
49 | 60 | } |
50 | 61 | return $result; |
51 | 62 | } |
52 | 63 |
|
53 | | - private function getProperties($object){ |
54 | | - $result=[]; |
55 | | - if(is_array($object)){ |
| 64 | + private function getProperties($object) { |
| 65 | + $result = []; |
| 66 | + if (is_array($object)) { |
56 | 67 | return array_keys($object); |
57 | 68 | } |
58 | | - $reflect=new \ReflectionClass($object); |
59 | | - $properties=$reflect->getProperties(); |
60 | | - foreach ($properties as $prop){ |
61 | | - $result[]=$prop->getName(); |
| 69 | + $reflect = new \ReflectionClass($object); |
| 70 | + $properties = $reflect->getProperties(); |
| 71 | + foreach ($properties as $prop) { |
| 72 | + $result[] = $prop->getName(); |
62 | 73 | } |
63 | 74 | return $result; |
64 | 75 | } |
65 | 76 |
|
66 | 77 | /** |
| 78 | + * |
67 | 79 | * @param mixed $objects |
68 | 80 | */ |
69 | 81 | public function setObjects($objects) { |
70 | 82 | $this->objects = $objects; |
71 | 83 | } |
72 | 84 |
|
73 | 85 | /** |
74 | | - * @param multitype:NULL $properties |
| 86 | + * |
| 87 | + * @param multitype:NULL $properties |
75 | 88 | */ |
76 | 89 | public function setProperties($properties) { |
77 | 90 | $this->properties = $properties; |
78 | 91 | } |
79 | | - |
80 | 92 | } |
0 commit comments