|
| 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\ClassConstGenerator; |
| 15 | +use OpenCodeModeling\CodeAst\Code\IdentifierGenerator; |
| 16 | +use OpenCodeModeling\CodeAst\Code\MethodGenerator; |
| 17 | +use OpenCodeModeling\CodeAst\Code\ParameterGenerator; |
| 18 | +use OpenCodeModeling\CodeAst\NodeVisitor\ClassConstant; |
| 19 | +use OpenCodeModeling\CodeAst\NodeVisitor\ClassMethod; |
| 20 | +use OpenCodeModeling\JsonSchemaToPhp\Type\StringType; |
| 21 | +use OpenCodeModeling\JsonSchemaToPhpAst\PropertyFactory; |
| 22 | +use PhpParser\NodeVisitor; |
| 23 | +use PhpParser\Parser; |
| 24 | + |
| 25 | +/** |
| 26 | + * This file creates node visitors for a value object of type bool. |
| 27 | + * |
| 28 | + * The following code will be generated: |
| 29 | + * |
| 30 | + * private const OUTPUT_FORMAT = 'Y-m-d\TH:i:s.uP'; |
| 31 | + * |
| 32 | + * private DateTimeImmutable $dateTime; |
| 33 | + * |
| 34 | + * public static function fromDateTime(DateTimeImmutable $dateTime): self |
| 35 | + * { |
| 36 | + * return new self(self::ensureUtc($dateTime)); |
| 37 | + * } |
| 38 | + * |
| 39 | + * public static function fromString(string $dateTime): self |
| 40 | + * { |
| 41 | + * try { |
| 42 | + * $dateTimeImmutable = new DateTimeImmutable($dateTime); |
| 43 | + * } catch (\Exception $e) { |
| 44 | + * throw new InvalidArgumentException( |
| 45 | + * sprintf( |
| 46 | + * 'String "%s" is not supported. Use a date time format which is compatible with ISO 8601.', |
| 47 | + * $dateTime |
| 48 | + * ) |
| 49 | + * ); |
| 50 | + * } |
| 51 | + * |
| 52 | + * $dateTimeImmutable = self::ensureUtc($dateTimeImmutable); |
| 53 | + * |
| 54 | + * return new self($dateTimeImmutable); |
| 55 | + * } |
| 56 | + * |
| 57 | + * private function __construct(DateTimeImmutable $dateTime) |
| 58 | + * { |
| 59 | + * $this->dateTime = $dateTime; |
| 60 | + * } |
| 61 | + * |
| 62 | + * public function toString(): string |
| 63 | + * { |
| 64 | + * return $this->dateTime->format(self::OUTPUT_FORMAT); |
| 65 | + * } |
| 66 | + * |
| 67 | + * public function dateTime(): DateTimeImmutable |
| 68 | + * { |
| 69 | + * return $this->dateTime; |
| 70 | + * } |
| 71 | + * |
| 72 | + * public function __toString(): string |
| 73 | + * { |
| 74 | + * return $this->toString(); |
| 75 | + * } |
| 76 | + * |
| 77 | + * private static function ensureUtc(DateTimeImmutable $dateTime): DateTimeImmutable |
| 78 | + * { |
| 79 | + * if ($dateTime->getTimezone()->getName() !== 'UTC') { |
| 80 | + * $dateTime = $dateTime->setTimezone(new \DateTimeZone('UTC')); |
| 81 | + * } |
| 82 | + * |
| 83 | + * return $dateTime; |
| 84 | + * } |
| 85 | + */ |
| 86 | +final class DateTimeFactory |
| 87 | +{ |
| 88 | + private Parser $parser; |
| 89 | + private PropertyFactory $propertyFactory; |
| 90 | + |
| 91 | + public function __construct(Parser $parser, bool $typed) |
| 92 | + { |
| 93 | + $this->parser = $parser; |
| 94 | + $this->propertyFactory = new PropertyFactory($typed); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param StringType $typeDefinition |
| 99 | + * @return array<NodeVisitor> |
| 100 | + */ |
| 101 | + public function nodeVisitors(StringType $typeDefinition): array |
| 102 | + { |
| 103 | + $name = $typeDefinition->name() ?: 'dateTime'; |
| 104 | + |
| 105 | + return $this->nodeVisitorsFromNative($name); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param string $name |
| 110 | + * @param string $outputFormat |
| 111 | + * @return array<NodeVisitor> |
| 112 | + */ |
| 113 | + public function nodeVisitorsFromNative(string $name, string $outputFormat = DATE_ATOM): array |
| 114 | + { |
| 115 | + $nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, 'DateTimeImmutable'); |
| 116 | + |
| 117 | + \array_unshift( |
| 118 | + $nodeVisitors, |
| 119 | + new ClassConstant( |
| 120 | + new IdentifierGenerator( |
| 121 | + 'OUTPUT_FORMAT', |
| 122 | + new ClassConstGenerator( |
| 123 | + 'OUTPUT_FORMAT', |
| 124 | + $outputFormat, |
| 125 | + ClassConstGenerator::FLAG_PRIVATE |
| 126 | + ) |
| 127 | + ) |
| 128 | + ) |
| 129 | + ); |
| 130 | + |
| 131 | + $nodeVisitors[] = $this->methodFromDateTime($name); |
| 132 | + $nodeVisitors[] = $this->methodFromString($name); |
| 133 | + $nodeVisitors[] = $this->methodMagicConstruct($name); |
| 134 | + $nodeVisitors[] = $this->methodToString($name); |
| 135 | + $nodeVisitors[] = $this->methodDateTime($name); |
| 136 | + $nodeVisitors[] = $this->methodMagicToString(); |
| 137 | + $nodeVisitors[] = $this->methodEnsureUtc($name); |
| 138 | + |
| 139 | + return $nodeVisitors; |
| 140 | + } |
| 141 | + |
| 142 | + public function methodFromDateTime(string $argumentName): NodeVisitor |
| 143 | + { |
| 144 | + $method = new MethodGenerator( |
| 145 | + 'fromDateTime', |
| 146 | + [ |
| 147 | + new ParameterGenerator($argumentName, 'DateTimeImmutable'), |
| 148 | + ], |
| 149 | + MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PUBLIC, |
| 150 | + new BodyGenerator($this->parser, 'return new self(self::ensureUtc($' . $argumentName . '));') |
| 151 | + ); |
| 152 | + $method->setReturnType('self'); |
| 153 | + |
| 154 | + return new ClassMethod($method); |
| 155 | + } |
| 156 | + |
| 157 | + public function methodFromString(string $argumentName): NodeVisitor |
| 158 | + { |
| 159 | + $body = <<<PHP |
| 160 | + try { |
| 161 | + \$dateTimeImmutable = new DateTimeImmutable(\$$argumentName); |
| 162 | + } catch (\Exception \$e) { |
| 163 | + throw new InvalidArgumentException(sprintf('String "%s" is not supported. Use a date time format which is compatible with ISO 8601.', \$$argumentName)); |
| 164 | + } |
| 165 | +
|
| 166 | + \$dateTimeImmutable = self::ensureUtc(\$dateTimeImmutable); |
| 167 | +
|
| 168 | + return new self(\$dateTimeImmutable); |
| 169 | +PHP; |
| 170 | + |
| 171 | + $method = new MethodGenerator( |
| 172 | + 'fromString', |
| 173 | + [ |
| 174 | + new ParameterGenerator($argumentName, 'string'), |
| 175 | + ], |
| 176 | + MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PUBLIC, |
| 177 | + new BodyGenerator($this->parser, $body) |
| 178 | + ); |
| 179 | + $method->setReturnType('self'); |
| 180 | + |
| 181 | + return new ClassMethod($method); |
| 182 | + } |
| 183 | + |
| 184 | + public function methodMagicConstruct(string $argumentName): NodeVisitor |
| 185 | + { |
| 186 | + $method = new MethodGenerator( |
| 187 | + '__construct', |
| 188 | + [ |
| 189 | + new ParameterGenerator($argumentName, 'DateTimeImmutable'), |
| 190 | + ], |
| 191 | + MethodGenerator::FLAG_PRIVATE, |
| 192 | + new BodyGenerator($this->parser, \sprintf('$this->%s = $%s;', $argumentName, $argumentName)) |
| 193 | + ); |
| 194 | + |
| 195 | + return new ClassMethod($method); |
| 196 | + } |
| 197 | + |
| 198 | + public function methodToString(string $argumentName): NodeVisitor |
| 199 | + { |
| 200 | + $method = new MethodGenerator( |
| 201 | + 'toString', |
| 202 | + [], |
| 203 | + MethodGenerator::FLAG_PUBLIC, |
| 204 | + new BodyGenerator($this->parser, 'return $this->' . $argumentName . '->format(self::OUTPUT_FORMAT);') |
| 205 | + ); |
| 206 | + $method->setReturnType('string'); |
| 207 | + |
| 208 | + return new ClassMethod($method); |
| 209 | + } |
| 210 | + |
| 211 | + public function methodDateTime(string $argumentName): NodeVisitor |
| 212 | + { |
| 213 | + $method = new MethodGenerator( |
| 214 | + 'dateTime', |
| 215 | + [], |
| 216 | + MethodGenerator::FLAG_PUBLIC, |
| 217 | + new BodyGenerator($this->parser, 'return $this->' . $argumentName . ';') |
| 218 | + ); |
| 219 | + $method->setReturnType('DateTimeImmutable'); |
| 220 | + |
| 221 | + return new ClassMethod($method); |
| 222 | + } |
| 223 | + |
| 224 | + public function methodMagicToString(): NodeVisitor |
| 225 | + { |
| 226 | + $method = new MethodGenerator( |
| 227 | + '__toString', |
| 228 | + [], |
| 229 | + MethodGenerator::FLAG_PUBLIC, |
| 230 | + new BodyGenerator($this->parser, 'return $this->toString();') |
| 231 | + ); |
| 232 | + $method->setReturnType('string'); |
| 233 | + |
| 234 | + return new ClassMethod($method); |
| 235 | + } |
| 236 | + |
| 237 | + public function methodEnsureUtc(string $argumentName): NodeVisitor |
| 238 | + { |
| 239 | + $body = <<<'PHP' |
| 240 | + if ($argumentName->getTimezone()->getName() !== 'UTC') { |
| 241 | + $argumentName = $argumentName->setTimezone(new \DateTimeZone('UTC')); |
| 242 | + } |
| 243 | +
|
| 244 | + return $argumentName; |
| 245 | +PHP; |
| 246 | + $body = \str_replace('argumentName', $argumentName, $body); |
| 247 | + |
| 248 | + $method = new MethodGenerator( |
| 249 | + 'ensureUtc', |
| 250 | + [ |
| 251 | + new ParameterGenerator($argumentName, 'DateTimeImmutable'), |
| 252 | + ], |
| 253 | + MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PRIVATE, |
| 254 | + new BodyGenerator($this->parser, $body) |
| 255 | + ); |
| 256 | + $method->setReturnType('DateTimeImmutable'); |
| 257 | + |
| 258 | + return new ClassMethod($method); |
| 259 | + } |
| 260 | +} |
0 commit comments