Skip to content

Commit 28b216b

Browse files
committed
type helpers
1 parent 24f992d commit 28b216b

File tree

1 file changed

+87
-2
lines changed
  • src/Symfony/Component/Serializer/Type

1 file changed

+87
-2
lines changed

src/Symfony/Component/Serializer/Type/Type.php

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,17 +344,102 @@ public static function fromString(string $string): self
344344
name: $type,
345345
isNullable: $isNullable,
346346
className: $className,
347-
genericParameterTypes: array_map(fn (string $t): Type => self::fromString($t), $genericParameters),
347+
genericParameterTypes: array_map(fn (string $t): self => self::fromString($t), $genericParameters),
348348
);
349349
}
350350

351351
return self::$cache[$cacheKey] = new self($string, $isNullable);
352352
}
353353

354+
public static function int(bool $nullable = false): self
355+
{
356+
return new self('int', isNullable: $nullable);
357+
}
358+
359+
public static function float(bool $nullable = false): self
360+
{
361+
return new self('float', isNullable: $nullable);
362+
}
363+
364+
public static function string(bool $nullable = false): self
365+
{
366+
return new self('string', isNullable: $nullable);
367+
}
368+
369+
public static function bool(bool $nullable = false): self
370+
{
371+
return new self('bool', isNullable: $nullable);
372+
}
373+
374+
public static function null(): self
375+
{
376+
return new self('null');
377+
}
378+
379+
public static function array(self $value, self $key = null, bool $nullable = false): self
380+
{
381+
return new self('array', isNullable: $nullable, genericParameterTypes: [$key ?? Type::int(), $value]);
382+
}
383+
384+
public static function list(self $value, bool $nullable = false): self
385+
{
386+
return new self('array', isNullable: $nullable, genericParameterTypes: [Type::int(), $value]);
387+
}
388+
389+
public static function dict(self $value, bool $nullable = false): self
390+
{
391+
return new self('array', isNullable: $nullable, genericParameterTypes: [Type::string(), $value]);
392+
}
393+
394+
public static function iterable(self $value, self $key = null, bool $nullable = false): self
395+
{
396+
return new self('iterable', isNullable: $nullable, genericParameterTypes: [$key ?? Type::int(), $value]);
397+
}
398+
399+
public static function iterableList(self $value, bool $nullable = false): self
400+
{
401+
return new self('array', isNullable: $nullable, genericParameterTypes: [Type::int(), $value]);
402+
}
403+
404+
public static function iterableDict(self $value, bool $nullable = false): self
405+
{
406+
return new self('array', isNullable: $nullable, genericParameterTypes: [Type::string(), $value]);
407+
}
408+
409+
public static function object(bool $nullable = false): self
410+
{
411+
return new self('object', isNullable: $nullable);
412+
}
413+
414+
/**
415+
* @param class-string $className
416+
* @param list<self> $genericParameterTypes
417+
*/
418+
public static function class(string $className, bool $nullable = false, array $genericParameterTypes = []): self
419+
{
420+
return new self('object', isNullable: $nullable, className: $className, genericParameterTypes: $genericParameterTypes);
421+
}
422+
423+
/**
424+
* @param class-string $enumClassName
425+
*/
426+
public static function enum(string $enumClassName, bool $nullable = false): self
427+
{
428+
return new self('enum', isNullable: $nullable, className: $enumClassName);
429+
}
430+
431+
public static function union(self ...$types): self
432+
{
433+
return new self(
434+
implode('|', array_map(fn (self $t): string => (string) $t, $types)),
435+
unionTypes: $types,
436+
);
437+
}
438+
354439
private function computeStringValue(): string
355440
{
356441
if ($this->isUnion()) {
357-
return implode('|', array_map(fn (Type $t): string => (string) $t, $this->unionTypes));
442+
return implode('|', array_map(fn (self $t): string => (string) $t, $this->unionTypes));
358443
}
359444

360445
if ($this->isNull()) {

0 commit comments

Comments
 (0)