|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @see https://github.com/open-code-modeling/json-schema-to-php-ast for the canonical source repository |
| 5 | + * @copyright https://github.com/open-code-modeling/json-schema-to-php-ast/blob/master/COPYRIGHT.md |
| 6 | + * @license https://github.com/open-code-modeling/json-schema-to-php-ast/blob/master/LICENSE.md MIT License |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace OpenCodeModeling\JsonSchemaToPhpAst\Common; |
| 12 | + |
| 13 | +use OpenCodeModeling\CodeAst\Builder\ClassBuilder; |
| 14 | +use OpenCodeModeling\CodeAst\Code\BodyGenerator; |
| 15 | +use OpenCodeModeling\CodeAst\Code\MethodGenerator; |
| 16 | +use OpenCodeModeling\CodeAst\NodeVisitor\ClassImplements; |
| 17 | +use OpenCodeModeling\CodeAst\NodeVisitor\ClassMethod; |
| 18 | +use OpenCodeModeling\CodeAst\NodeVisitor\Property; |
| 19 | +use OpenCodeModeling\JsonSchemaToPhpAst\PropertyFactory; |
| 20 | +use PhpParser\NodeVisitor; |
| 21 | +use PhpParser\Parser; |
| 22 | + |
| 23 | +/** |
| 24 | + * This file creates node visitors for an iterable value object. |
| 25 | + * |
| 26 | + * The following code will be generated: |
| 27 | + */ |
| 28 | +final class IteratorFactory |
| 29 | +{ |
| 30 | + private Parser $parser; |
| 31 | + private PropertyFactory $propertyFactory; |
| 32 | + private bool $typed; |
| 33 | + |
| 34 | + public function __construct(Parser $parser, bool $typed) |
| 35 | + { |
| 36 | + $this->parser = $parser; |
| 37 | + $this->typed = $typed; |
| 38 | + $this->propertyFactory = new PropertyFactory($typed); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param string $name |
| 43 | + * @param string $itemType |
| 44 | + * @param string $positionPropertyName |
| 45 | + * @return array<NodeVisitor> |
| 46 | + */ |
| 47 | + public function nodeVisitorsFromNative( |
| 48 | + string $name, |
| 49 | + string $itemType, |
| 50 | + string $positionPropertyName = 'position' |
| 51 | + ): array { |
| 52 | + $nodeVisitors = []; |
| 53 | + |
| 54 | + $nodeVisitors[] = new Property( |
| 55 | + $this->propertyFactory->propertyGenerator($positionPropertyName, 'int')->setDefaultValue(0) |
| 56 | + ); |
| 57 | + $nodeVisitors[] = new Property( |
| 58 | + $this->propertyFactory->propertyGenerator($name, 'array')->setTypeDocBlockHint($itemType . '[]') |
| 59 | + ); |
| 60 | + |
| 61 | + $nodeVisitors[] = new ClassMethod($this->methodRewind($positionPropertyName)); |
| 62 | + $nodeVisitors[] = new ClassMethod($this->methodCurrent($name, $itemType, $positionPropertyName)); |
| 63 | + $nodeVisitors[] = new ClassMethod($this->methodKey($positionPropertyName)); |
| 64 | + $nodeVisitors[] = new ClassMethod($this->methodNext($positionPropertyName)); |
| 65 | + $nodeVisitors[] = new ClassMethod($this->methodValid($name, $positionPropertyName)); |
| 66 | + $nodeVisitors[] = new ClassMethod($this->methodCount($name)); |
| 67 | + |
| 68 | + $nodeVisitors[] = new ClassImplements('\\Iterator', '\\Countable'); |
| 69 | + |
| 70 | + return $nodeVisitors; |
| 71 | + } |
| 72 | + |
| 73 | + public function classBuilderFromNative( |
| 74 | + string $name, |
| 75 | + string $itemType, |
| 76 | + string $positionPropertyName = 'position' |
| 77 | + ): ClassBuilder { |
| 78 | + return ClassBuilder::fromNodes( |
| 79 | + $this->propertyFactory->propertyGenerator($positionPropertyName, 'int')->setDefaultValue(0)->generate(), |
| 80 | + $this->propertyFactory->propertyGenerator($name, 'array')->setTypeDocBlockHint($itemType . '[]')->generate(), |
| 81 | + $this->methodRewind($positionPropertyName)->generate(), |
| 82 | + $this->methodCurrent($name, $itemType, $positionPropertyName)->generate(), |
| 83 | + $this->methodKey($positionPropertyName)->generate(), |
| 84 | + $this->methodNext($positionPropertyName)->generate(), |
| 85 | + $this->methodValid($name, $positionPropertyName)->generate(), |
| 86 | + $this->methodCount($name)->generate(), |
| 87 | + )->setImplements('\\Iterator', '\\Countable') |
| 88 | + ->setTyped($this->typed); |
| 89 | + } |
| 90 | + |
| 91 | + public function methodRewind(string $positionPropertyName): MethodGenerator |
| 92 | + { |
| 93 | + $method = new MethodGenerator( |
| 94 | + 'rewind', |
| 95 | + [], |
| 96 | + MethodGenerator::FLAG_PUBLIC, |
| 97 | + new BodyGenerator($this->parser, '$this->' . $positionPropertyName . ' = 0;') |
| 98 | + ); |
| 99 | + $method->setTyped($this->typed); |
| 100 | + $method->setReturnType('void'); |
| 101 | + |
| 102 | + return $method; |
| 103 | + } |
| 104 | + |
| 105 | + public function methodCurrent(string $name, string $itemType, string $positionPropertyName): MethodGenerator |
| 106 | + { |
| 107 | + $method = new MethodGenerator( |
| 108 | + 'current', |
| 109 | + [], |
| 110 | + MethodGenerator::FLAG_PUBLIC, |
| 111 | + new BodyGenerator($this->parser, \sprintf('return $this->%s[$this->%s];', $name, $positionPropertyName)) |
| 112 | + ); |
| 113 | + $method->setTyped($this->typed); |
| 114 | + $method->setReturnType($itemType); |
| 115 | + |
| 116 | + return $method; |
| 117 | + } |
| 118 | + |
| 119 | + public function methodKey(string $positionPropertyName): MethodGenerator |
| 120 | + { |
| 121 | + $method = new MethodGenerator( |
| 122 | + 'key', |
| 123 | + [], |
| 124 | + MethodGenerator::FLAG_PUBLIC, |
| 125 | + new BodyGenerator($this->parser, 'return $this->' . $positionPropertyName . ';') |
| 126 | + ); |
| 127 | + $method->setTyped($this->typed); |
| 128 | + $method->setReturnType('int'); |
| 129 | + |
| 130 | + return $method; |
| 131 | + } |
| 132 | + |
| 133 | + public function methodNext(string $positionPropertyName): MethodGenerator |
| 134 | + { |
| 135 | + $method = new MethodGenerator( |
| 136 | + 'next', |
| 137 | + [], |
| 138 | + MethodGenerator::FLAG_PUBLIC, |
| 139 | + new BodyGenerator($this->parser, '++$this->' . $positionPropertyName . ';') |
| 140 | + ); |
| 141 | + $method->setTyped($this->typed); |
| 142 | + $method->setReturnType('void'); |
| 143 | + |
| 144 | + return $method; |
| 145 | + } |
| 146 | + |
| 147 | + public function methodValid(string $name, string $positionPropertyName): MethodGenerator |
| 148 | + { |
| 149 | + $method = new MethodGenerator( |
| 150 | + 'valid', |
| 151 | + [], |
| 152 | + MethodGenerator::FLAG_PUBLIC, |
| 153 | + new BodyGenerator( |
| 154 | + $this->parser, |
| 155 | + \sprintf('return isset($this->%s[$this->%s]);', $name, $positionPropertyName) |
| 156 | + ) |
| 157 | + ); |
| 158 | + $method->setTyped($this->typed); |
| 159 | + $method->setReturnType('bool'); |
| 160 | + |
| 161 | + return $method; |
| 162 | + } |
| 163 | + |
| 164 | + public function methodCount(string $name): MethodGenerator |
| 165 | + { |
| 166 | + $method = new MethodGenerator( |
| 167 | + 'count', |
| 168 | + [], |
| 169 | + MethodGenerator::FLAG_PUBLIC, |
| 170 | + new BodyGenerator($this->parser, 'return count($this->' . $name . ');') |
| 171 | + ); |
| 172 | + $method->setTyped($this->typed); |
| 173 | + $method->setReturnType('int'); |
| 174 | + |
| 175 | + return $method; |
| 176 | + } |
| 177 | +} |
0 commit comments