|
| 1 | +<?php |
| 2 | + |
| 3 | +/* MIT License |
| 4 | +
|
| 5 | +Copyright (c) 2018 Eridan Domoratskiy |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. */ |
| 24 | + |
| 25 | +namespace PHPDataGen\Parsing; |
| 26 | + |
| 27 | +use PHPDataGen\Building\ClassBuilder; |
| 28 | + |
| 29 | +/** |
| 30 | + * Class parsing state |
| 31 | + */ |
| 32 | +class ClassState extends State { |
| 33 | + |
| 34 | + /** |
| 35 | + * @var State Parent state |
| 36 | + */ |
| 37 | + protected $parent = null; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var ClassBuilder Builder |
| 41 | + */ |
| 42 | + protected $builder = null; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var FieldState Last produced field state |
| 46 | + */ |
| 47 | + protected $field = null; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var int Current file parsing state |
| 51 | + * |
| 52 | + * 0 - Class name |
| 53 | + * 1 - After class name |
| 54 | + * 2 - Extends |
| 55 | + * 3 - Implements |
| 56 | + * 4 - After implement |
| 57 | + * 5 - Fields |
| 58 | + * 6 - Field |
| 59 | + * |
| 60 | + */ |
| 61 | + protected $state = 0; |
| 62 | + |
| 63 | + /** |
| 64 | + * @param State $parent Parent state |
| 65 | + */ |
| 66 | + public function __construct(State $parent) { |
| 67 | + $this->builder = new ClassBuilder(); |
| 68 | + $this->parent = $parent; |
| 69 | + } |
| 70 | + |
| 71 | + public function getBuilder(): ClassBuilder { |
| 72 | + return $this->builder; |
| 73 | + } |
| 74 | + |
| 75 | + public function step(string &$next): State { |
| 76 | + switch ($this->state) { |
| 77 | + case 0: |
| 78 | + $this->builder->setName(Parser::readClassname($next)); |
| 79 | + |
| 80 | + $this->state = 1; |
| 81 | + return $this; |
| 82 | + |
| 83 | + case 1: |
| 84 | + if (strpos($next, 'extends') === 0) { |
| 85 | + $this->state = 2; |
| 86 | + |
| 87 | + $next = substr($next, 7); |
| 88 | + return $this; |
| 89 | + } |
| 90 | + |
| 91 | + if (strpos($next, 'implements') === 0) { |
| 92 | + $this->state = 3; |
| 93 | + |
| 94 | + $next = substr($next, 10); |
| 95 | + return $this; |
| 96 | + } |
| 97 | + |
| 98 | + if (strpos($next, '{') === 0) { |
| 99 | + $this->state = 5; |
| 100 | + |
| 101 | + $next = substr($next, 1); |
| 102 | + return $this; |
| 103 | + } |
| 104 | + |
| 105 | + throw new \Exception('Open bracket not found'); |
| 106 | + |
| 107 | + case 2: |
| 108 | + $this->builder->setExtends(Parser::readExtendedClassname($next)); |
| 109 | + |
| 110 | + $this->state = 1; |
| 111 | + return $this; |
| 112 | + |
| 113 | + case 3: |
| 114 | + $this->builder->addImplement(Parser::readExtendedClassname($next)); |
| 115 | + |
| 116 | + $this->state = 4; |
| 117 | + return $this; |
| 118 | + |
| 119 | + case 4: |
| 120 | + if (strpos($next, ',') === 0) { |
| 121 | + $this->state = 3; |
| 122 | + |
| 123 | + $next = substr($next, 1); |
| 124 | + return $this; |
| 125 | + } |
| 126 | + |
| 127 | + $this->state = 1; |
| 128 | + return $this; |
| 129 | + |
| 130 | + case 5: |
| 131 | + if ( |
| 132 | + strpos($next, 'direct') === 0 || |
| 133 | + strpos($next, 'val') === 0 || |
| 134 | + strpos($next, 'var') === 0 |
| 135 | + ) { |
| 136 | + $this->state = 6; |
| 137 | + |
| 138 | + $this->field = new FieldState($this); |
| 139 | + return $this->field; |
| 140 | + } |
| 141 | + |
| 142 | + if (strpos($next, '}') === 0) { |
| 143 | + $next = substr($next, 1); |
| 144 | + |
| 145 | + return $this->parent; |
| 146 | + } |
| 147 | + |
| 148 | + throw new \Exception('Close bracket not found'); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments