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