Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ReflectArray.php
Go to the documentation of this file.
1<?php
3
5
6class ReflectArray extends BaseArray {
7
8 private $objects;
9
10 private $properties;
11
12 public function parse() {
13 $object = current($this->objects);
14 if (! isset($object) || ! $object) {
15 return [
16 [
17 'Nothing to display'
18 ]
19 ];
20 }
21 if (! is_object($object)) {
22 $object = (object) $object;
23 }
24 if (! is_array($this->properties)) {
25 $this->properties = $this->getProperties($object);
26 }
27 $result = [
29 ];
30 $r = new \ReflectionClass($object);
31
32 foreach ($this->objects as $object) {
33 if (is_array($object)) {
34 $result[] = $object;
35 } elseif (is_object($object)) {
36 $row = [];
37 foreach ($this->properties as $prop) {
38 if ($r->hasProperty($prop)) {
39 $property = $r->getProperty($prop);
40 $property->setAccessible(true);
41 $row[] = $this->parseValue($property->getValue($object));
42 } else {
43 $this->messages[$prop] = "Property {$prop} does not exists!";
44 $row[] = '';
45 }
46 }
47 $result[] = $row;
48 }
49 }
50 return $result;
51 }
52
53 private function parseValue($value) {
54 $result = '-';
55 if (\is_array($value)) {
56 return $this->parseArray($value);
57 } elseif (UString::isValid($value)) {
58 $result = $value;
59 } elseif (\is_callable($value)) {
60 $result = '(x)=>{}';
61 } else {
62 $result = \var_export($value, true);
63 }
64 return $result;
65 }
66
67 private function getProperties($object) {
68 $result = [];
69 if (is_array($object)) {
70 return array_keys($object);
71 }
72 $reflect = new \ReflectionClass($object);
73 $properties = $reflect->getProperties();
74 foreach ($properties as $prop) {
75 $result[] = $prop->getName();
76 }
77 return $result;
78 }
79
84 public function setObjects($objects) {
85 $this->objects = $objects;
86 }
87
92 public function setProperties($properties) {
93 $this->properties = $properties;
94 }
95}
String utilities.
Definition UString.php:15