1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | namespace Kint\Object; |
27: | |
28: | use Kint\Object\Representation\Representation; |
29: | |
30: | class BasicObject |
31: | { |
32: | const ACCESS_NONE = null; |
33: | const ACCESS_PUBLIC = 1; |
34: | const ACCESS_PROTECTED = 2; |
35: | const ACCESS_PRIVATE = 3; |
36: | |
37: | const OPERATOR_NONE = null; |
38: | const OPERATOR_ARRAY = 1; |
39: | const OPERATOR_OBJECT = 2; |
40: | const OPERATOR_STATIC = 3; |
41: | |
42: | public $name; |
43: | public $type; |
44: | public $static = false; |
45: | public $const = false; |
46: | public $access = self::ACCESS_NONE; |
47: | public $owner_class; |
48: | public $access_path; |
49: | public $operator = self::OPERATOR_NONE; |
50: | public $reference = false; |
51: | public $depth = 0; |
52: | public $size; |
53: | public $value; |
54: | public $hints = array(); |
55: | |
56: | protected $representations = array(); |
57: | |
58: | public function __construct() |
59: | { |
60: | } |
61: | |
62: | public function addRepresentation(Representation $rep, $pos = null) |
63: | { |
64: | if (isset($this->representations[$rep->getName()])) { |
65: | return false; |
66: | } |
67: | |
68: | if (null === $pos) { |
69: | $this->representations[$rep->getName()] = $rep; |
70: | } else { |
71: | $this->representations = \array_merge( |
72: | \array_slice($this->representations, 0, $pos), |
73: | array($rep->getName() => $rep), |
74: | \array_slice($this->representations, $pos) |
75: | ); |
76: | } |
77: | |
78: | return true; |
79: | } |
80: | |
81: | public function replaceRepresentation(Representation $rep, $pos = null) |
82: | { |
83: | if (null === $pos) { |
84: | $this->representations[$rep->getName()] = $rep; |
85: | } else { |
86: | $this->removeRepresentation($rep); |
87: | $this->addRepresentation($rep, $pos); |
88: | } |
89: | } |
90: | |
91: | public function removeRepresentation($rep) |
92: | { |
93: | if ($rep instanceof Representation) { |
94: | unset($this->representations[$rep->getName()]); |
95: | } elseif (\is_string($rep)) { |
96: | unset($this->representations[$rep]); |
97: | } |
98: | } |
99: | |
100: | public function getRepresentation($name) |
101: | { |
102: | if (isset($this->representations[$name])) { |
103: | return $this->representations[$name]; |
104: | } |
105: | } |
106: | |
107: | public function getRepresentations() |
108: | { |
109: | return $this->representations; |
110: | } |
111: | |
112: | public function clearRepresentations() |
113: | { |
114: | $this->representations = array(); |
115: | } |
116: | |
117: | public function getType() |
118: | { |
119: | return $this->type; |
120: | } |
121: | |
122: | public function getModifiers() |
123: | { |
124: | $out = $this->getAccess(); |
125: | |
126: | if ($this->const) { |
127: | $out .= ' const'; |
128: | } |
129: | |
130: | if ($this->static) { |
131: | $out .= ' static'; |
132: | } |
133: | |
134: | if (\strlen($out)) { |
135: | return \ltrim($out); |
136: | } |
137: | } |
138: | |
139: | public function getAccess() |
140: | { |
141: | switch ($this->access) { |
142: | case self::ACCESS_PRIVATE: |
143: | return 'private'; |
144: | case self::ACCESS_PROTECTED: |
145: | return 'protected'; |
146: | case self::ACCESS_PUBLIC: |
147: | return 'public'; |
148: | } |
149: | } |
150: | |
151: | public function getName() |
152: | { |
153: | return $this->name; |
154: | } |
155: | |
156: | public function getOperator() |
157: | { |
158: | switch ($this->operator) { |
159: | case self::OPERATOR_ARRAY: |
160: | return '=>'; |
161: | case self::OPERATOR_OBJECT: |
162: | return '->'; |
163: | case self::OPERATOR_STATIC: |
164: | return '::'; |
165: | } |
166: | } |
167: | |
168: | public function getSize() |
169: | { |
170: | return $this->size; |
171: | } |
172: | |
173: | public function getValueShort() |
174: | { |
175: | if ($rep = $this->value) { |
176: | if ('boolean' === $this->type) { |
177: | return $rep->contents ? 'true' : 'false'; |
178: | } |
179: | |
180: | if ('integer' === $this->type || 'double' === $this->type) { |
181: | return $rep->contents; |
182: | } |
183: | } |
184: | } |
185: | |
186: | public function getAccessPath() |
187: | { |
188: | return $this->access_path; |
189: | } |
190: | |
191: | public function transplant(BasicObject $old) |
192: | { |
193: | $this->name = $old->name; |
194: | $this->size = $old->size; |
195: | $this->access_path = $old->access_path; |
196: | $this->access = $old->access; |
197: | $this->static = $old->static; |
198: | $this->const = $old->const; |
199: | $this->type = $old->type; |
200: | $this->depth = $old->depth; |
201: | $this->owner_class = $old->owner_class; |
202: | $this->operator = $old->operator; |
203: | $this->reference = $old->reference; |
204: | $this->value = $old->value; |
205: | $this->representations += $old->representations; |
206: | $this->hints = \array_merge($this->hints, $old->hints); |
207: | } |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | public static function blank($name = null, $access_path = null) |
218: | { |
219: | $o = new self(); |
220: | $o->name = $name; |
221: | $o->access_path = $access_path; |
222: | |
223: | return $o; |
224: | } |
225: | |
226: | public static function sortByAccess(BasicObject $a, BasicObject $b) |
227: | { |
228: | static $sorts = array( |
229: | self::ACCESS_PUBLIC => 1, |
230: | self::ACCESS_PROTECTED => 2, |
231: | self::ACCESS_PRIVATE => 3, |
232: | self::ACCESS_NONE => 4, |
233: | ); |
234: | |
235: | return $sorts[$a->access] - $sorts[$b->access]; |
236: | } |
237: | |
238: | public static function sortByName(BasicObject $a, BasicObject $b) |
239: | { |
240: | $ret = \strnatcasecmp($a->name, $b->name); |
241: | |
242: | if (0 === $ret) { |
243: | return (int) \is_int($b->name) - (int) \is_int($a->name); |
244: | } |
245: | |
246: | return $ret; |
247: | } |
248: | } |
249: | |