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\Parser;
27:
28: use Kint\Object\BasicObject;
29: use Kint\Object\Representation\Representation;
30:
31: class TablePlugin extends Plugin
32: {
33: public function getTypes()
34: {
35: return array('array');
36: }
37:
38: public function getTriggers()
39: {
40: return Parser::TRIGGER_SUCCESS;
41: }
42:
43: public function parse(&$var, BasicObject &$o, $trigger)
44: {
45: if (empty($o->value->contents)) {
46: return;
47: }
48:
49: $array = $this->parser->getCleanArray($var);
50:
51: if (\count($array) < 2) {
52: return;
53: }
54:
55: // Ensure this is an array of arrays and that all child arrays have the
56: // same keys. We don't care about their children - if there's another
57: // "table" inside we'll just make another one down the value tab
58: $keys = null;
59: foreach ($array as $elem) {
60: if (!\is_array($elem) || \count($elem) < 2) {
61: return;
62: }
63:
64: if (null === $keys) {
65: $keys = \array_keys($elem);
66: } elseif (\array_keys($elem) !== $keys) {
67: return;
68: }
69: }
70:
71: // Ensure none of the child arrays are recursion or depth limit. We
72: // don't care if their children are since they are the table cells
73: foreach ($o->value->contents as $childarray) {
74: if (empty($childarray->value->contents)) {
75: return;
76: }
77: }
78:
79: // Objects by reference for the win! We can do a copy-paste of the value
80: // representation contents and just slap a new hint on there and hey
81: // presto we have our table representation with no extra memory used!
82: $table = new Representation('Table');
83: $table->contents = $o->value->contents;
84: $table->hints[] = 'table';
85: $o->addRepresentation($table, 0);
86: }
87: }
88: