|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Parser; |
| 4 | + |
| 5 | +class Context |
| 6 | +{ |
| 7 | + public $classDefinition = null; |
| 8 | + |
| 9 | + public $implements = []; |
| 10 | + |
| 11 | + public $extends = null; |
| 12 | + |
| 13 | + public $methodDefinition = null; |
| 14 | + |
| 15 | + public $methodDefinitionParams = []; |
| 16 | + |
| 17 | + public $methodExistingArgs = []; |
| 18 | + |
| 19 | + public $classUsed = null; |
| 20 | + |
| 21 | + public $methodUsed = null; |
| 22 | + |
| 23 | + public $child = null; |
| 24 | + |
| 25 | + public $variables = []; |
| 26 | + |
| 27 | + public $definedProperties = []; |
| 28 | + |
| 29 | + protected $freshObject = []; |
| 30 | + |
| 31 | + public $fillingInArrayKey = false; |
| 32 | + |
| 33 | + public $fillingInArrayValue = false; |
| 34 | + |
| 35 | + public $paramIndex = 0; |
| 36 | + |
| 37 | + public function __construct(public ?Context $parent = null) |
| 38 | + { |
| 39 | + $this->freshObject = $this->toArray(); |
| 40 | + } |
| 41 | + |
| 42 | + public function pristine(): bool |
| 43 | + { |
| 44 | + return $this->toArray() === $this->freshObject; |
| 45 | + } |
| 46 | + |
| 47 | + public function touched(): bool |
| 48 | + { |
| 49 | + return !$this->pristine(); |
| 50 | + } |
| 51 | + |
| 52 | + public function addVariable(string $name, array $attributes) |
| 53 | + { |
| 54 | + if (isset($attributes['name'])) { |
| 55 | + unset($attributes['name']); |
| 56 | + } |
| 57 | + |
| 58 | + $this->variables[ltrim($name, '$')] = $attributes; |
| 59 | + } |
| 60 | + |
| 61 | + public function searchForProperty(string $name) |
| 62 | + { |
| 63 | + $prop = $this->definedProperties[$name]; |
| 64 | + |
| 65 | + if ($prop) { |
| 66 | + return $prop; |
| 67 | + } |
| 68 | + |
| 69 | + if ($this->parent) { |
| 70 | + return $this->parent->searchForProperty($name); |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + public function searchForVar(string $name) |
| 77 | + { |
| 78 | + $param = array_filter( |
| 79 | + $this->methodDefinitionParams, |
| 80 | + fn($param) => $param['name'] === $name, |
| 81 | + ); |
| 82 | + |
| 83 | + if (count($param)) { |
| 84 | + return array_values($param)[0]; |
| 85 | + } |
| 86 | + |
| 87 | + if (array_key_exists($name, $this->variables)) { |
| 88 | + return $this->variables[$name]; |
| 89 | + } |
| 90 | + |
| 91 | + if ($this->parent) { |
| 92 | + return $this->parent->searchForVar($name); |
| 93 | + } |
| 94 | + |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + public function toArray() |
| 99 | + { |
| 100 | + return [ |
| 101 | + 'classDefinition' => $this->classDefinition, |
| 102 | + 'implements' => $this->implements, |
| 103 | + 'extends' => $this->extends, |
| 104 | + 'methodDefinition' => $this->methodDefinition, |
| 105 | + 'methodDefinitionParams' => $this->methodDefinitionParams, |
| 106 | + 'methodExistingArgs' => $this->methodExistingArgs, |
| 107 | + 'classUsed' => $this->classUsed, |
| 108 | + 'methodUsed' => $this->methodUsed, |
| 109 | + 'parent' => $this->parent?->toArray(), |
| 110 | + 'variables' => $this->variables, |
| 111 | + 'definedProperties' => $this->definedProperties, |
| 112 | + 'fillingInArrayKey' => $this->fillingInArrayKey, |
| 113 | + 'fillingInArrayValue' => $this->fillingInArrayValue, |
| 114 | + 'paramIndex' => $this->paramIndex, |
| 115 | + ]; |
| 116 | + } |
| 117 | + |
| 118 | + public function toJson($flags = 0) |
| 119 | + { |
| 120 | + return json_encode($this->toArray(), $flags); |
| 121 | + } |
| 122 | +} |
0 commit comments