|
| 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\ValueObject; |
| 12 | + |
| 13 | +use OpenCodeModeling\CodeAst\Code\BodyGenerator; |
| 14 | +use OpenCodeModeling\CodeAst\Code\MethodGenerator; |
| 15 | +use OpenCodeModeling\CodeAst\Code\ParameterGenerator; |
| 16 | +use OpenCodeModeling\CodeAst\NodeVisitor\ClassMethod; |
| 17 | +use OpenCodeModeling\JsonSchemaToPhp\Type\IntegerType; |
| 18 | +use OpenCodeModeling\JsonSchemaToPhpAst\PropertyFactory; |
| 19 | +use PhpParser\NodeVisitor; |
| 20 | +use PhpParser\Parser; |
| 21 | + |
| 22 | +/** |
| 23 | + * This file creates node visitors for a value object of type int. |
| 24 | + * |
| 25 | + * The following code will be generated: |
| 26 | + * |
| 27 | + * private int $number; |
| 28 | + * |
| 29 | + * public static function fromInt(int $number): self |
| 30 | + * { |
| 31 | + * return new self($number); |
| 32 | + * } |
| 33 | + * |
| 34 | + * private function __construct(int $number) |
| 35 | + * { |
| 36 | + * $this->number = $number; |
| 37 | + * } |
| 38 | + * |
| 39 | + * public function toInt(): int |
| 40 | + * { |
| 41 | + * return $this->number; |
| 42 | + * } |
| 43 | + * |
| 44 | + * public function equals($other): bool |
| 45 | + * { |
| 46 | + * if(!$other instanceof self) { |
| 47 | + * return false; |
| 48 | + * } |
| 49 | + * |
| 50 | + * return $this->number === $other->number; |
| 51 | + * } |
| 52 | + * |
| 53 | + * public function __toString(): string |
| 54 | + * { |
| 55 | + * return (string)$this->number; |
| 56 | + * } |
| 57 | + */ |
| 58 | +final class IntegerFactory |
| 59 | +{ |
| 60 | + private Parser $parser; |
| 61 | + private PropertyFactory $propertyFactory; |
| 62 | + |
| 63 | + public function __construct(Parser $parser, bool $typed) |
| 64 | + { |
| 65 | + $this->parser = $parser; |
| 66 | + $this->propertyFactory = new PropertyFactory($typed); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param IntegerType $typeDefinition |
| 71 | + * @return array<NodeVisitor> |
| 72 | + */ |
| 73 | + public function nodeVisitors(IntegerType $typeDefinition): array |
| 74 | + { |
| 75 | + $name = $typeDefinition->name() ?: 'number'; |
| 76 | + |
| 77 | + return $this->nodeVisitorsFromNative($name); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @param string $name |
| 82 | + * @return array<NodeVisitor> |
| 83 | + */ |
| 84 | + public function nodeVisitorsFromNative(string $name): array |
| 85 | + { |
| 86 | + $nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, 'int'); |
| 87 | + $nodeVisitors[] = $this->methodFromInt($name); |
| 88 | + $nodeVisitors[] = $this->methodMagicConstruct($name); |
| 89 | + $nodeVisitors[] = $this->methodToInt($name); |
| 90 | + $nodeVisitors[] = $this->methodEquals($name); |
| 91 | + $nodeVisitors[] = $this->methodMagicToString($name); |
| 92 | + |
| 93 | + return $nodeVisitors; |
| 94 | + } |
| 95 | + |
| 96 | + public function methodFromInt(string $argumentName): NodeVisitor |
| 97 | + { |
| 98 | + $method = new MethodGenerator( |
| 99 | + 'fromInt', |
| 100 | + [ |
| 101 | + new ParameterGenerator($argumentName, 'int'), |
| 102 | + ], |
| 103 | + MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PUBLIC, |
| 104 | + new BodyGenerator($this->parser, 'return new self($' . $argumentName . ');') |
| 105 | + ); |
| 106 | + $method->setReturnType('self'); |
| 107 | + |
| 108 | + return new ClassMethod($method); |
| 109 | + } |
| 110 | + |
| 111 | + public function methodMagicConstruct(string $argumentName): NodeVisitor |
| 112 | + { |
| 113 | + $method = new MethodGenerator( |
| 114 | + '__construct', |
| 115 | + [ |
| 116 | + new ParameterGenerator($argumentName, 'int'), |
| 117 | + ], |
| 118 | + MethodGenerator::FLAG_PRIVATE, |
| 119 | + new BodyGenerator($this->parser, \sprintf('$this->%s = $%s;', $argumentName, $argumentName)) |
| 120 | + ); |
| 121 | + |
| 122 | + return new ClassMethod($method); |
| 123 | + } |
| 124 | + |
| 125 | + public function methodToInt(string $argumentName): NodeVisitor |
| 126 | + { |
| 127 | + $method = new MethodGenerator( |
| 128 | + 'toInt', |
| 129 | + [], |
| 130 | + MethodGenerator::FLAG_PUBLIC, |
| 131 | + new BodyGenerator($this->parser, 'return $this->' . $argumentName . ';') |
| 132 | + ); |
| 133 | + $method->setReturnType('int'); |
| 134 | + |
| 135 | + return new ClassMethod($method); |
| 136 | + } |
| 137 | + |
| 138 | + public function methodEquals(string $propertyName, string $argumentName = 'other'): NodeVisitor |
| 139 | + { |
| 140 | + $body = <<<PHP |
| 141 | + if(!\$$argumentName instanceof self) { |
| 142 | + return false; |
| 143 | + } |
| 144 | +
|
| 145 | + return \$this->$propertyName === \$$argumentName->$propertyName; |
| 146 | +PHP; |
| 147 | + |
| 148 | + $method = new MethodGenerator( |
| 149 | + 'equals', |
| 150 | + [ |
| 151 | + new ParameterGenerator($argumentName), |
| 152 | + ], |
| 153 | + MethodGenerator::FLAG_PUBLIC, |
| 154 | + new BodyGenerator($this->parser, $body) |
| 155 | + ); |
| 156 | + $method->setReturnType('bool'); |
| 157 | + |
| 158 | + return new ClassMethod($method); |
| 159 | + } |
| 160 | + |
| 161 | + public function methodMagicToString(string $argumentName): NodeVisitor |
| 162 | + { |
| 163 | + $method = new MethodGenerator( |
| 164 | + '__toString', |
| 165 | + [], |
| 166 | + MethodGenerator::FLAG_PUBLIC, |
| 167 | + new BodyGenerator($this->parser, 'return (string)$this->' . $argumentName . ';') |
| 168 | + ); |
| 169 | + $method->setReturnType('string'); |
| 170 | + |
| 171 | + return new ClassMethod($method); |
| 172 | + } |
| 173 | +} |
0 commit comments