1: <?php
2:
3: /*
4: * The MIT License (MIT)
5: *
6: * Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
7: *
8: * Permission is hereby granted, free of charge, to any person obtaining a copy of
9: * this software and associated documentation files (the "Software"), to deal in
10: * the Software without restriction, including without limitation the rights to
11: * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12: * the Software, and to permit persons to whom the Software is furnished to do so,
13: * subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in all
16: * copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21: * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22: * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23: * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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: * Creates a new basic object with a name and access path.
211: *
212: * @param null|string $name
213: * @param null|string $access_path
214: *
215: * @return \Kint\Object\BasicObject
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: