|
| 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 BackedEnum; |
| 29 | +use Kint\Zval\EnumValue; |
| 30 | +use Kint\Zval\Representation\Representation; |
| 31 | +use Kint\Zval\Value; |
| 32 | +use UnitEnum; |
| 33 | + |
| 34 | +class EnumPlugin extends Plugin |
| 35 | +{ |
| 36 | + private static $cache = []; |
| 37 | + |
| 38 | + public function getTypes() |
| 39 | + { |
| 40 | + return ['object']; |
| 41 | + } |
| 42 | + |
| 43 | + public function getTriggers() |
| 44 | + { |
| 45 | + if (!KINT_PHP81) { |
| 46 | + return Parser::TRIGGER_NONE; |
| 47 | + } |
| 48 | + |
| 49 | + return Parser::TRIGGER_SUCCESS; |
| 50 | + } |
| 51 | + |
| 52 | + public function parse(&$var, Value &$o, $trigger) |
| 53 | + { |
| 54 | + if (!$var instanceof UnitEnum) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + $class = \get_class($var); |
| 59 | + |
| 60 | + if (!isset(self::$cache[$class])) { |
| 61 | + $cases = new Representation('Enum values', 'enum'); |
| 62 | + $cases->contents = []; |
| 63 | + |
| 64 | + foreach ($var->cases() as $case) { |
| 65 | + $base_obj = Value::blank($class.'::'.$case->name, '\\'.$class.'::'.$case->name); |
| 66 | + $base_obj->depth = $o->depth + 1; |
| 67 | + |
| 68 | + if ($var instanceof BackedEnum) { |
| 69 | + $c = $case->value; |
| 70 | + $cases->contents[] = $this->parser->parse($c, $base_obj); |
| 71 | + } else { |
| 72 | + $cases->contents[] = $base_obj; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + self::$cache[$class] = $cases; |
| 77 | + } |
| 78 | + |
| 79 | + $object = new EnumValue($var); |
| 80 | + $object->transplant($o); |
| 81 | + |
| 82 | + $object->addRepresentation(self::$cache[$class], 0); |
| 83 | + |
| 84 | + $o = $object; |
| 85 | + } |
| 86 | +} |
0 commit comments