Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykbaszak committed Oct 19, 2023
1 parent b2243b3 commit adf4660
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 892 deletions.
872 changes: 7 additions & 865 deletions output.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function export(): array

return [
'main' => $this->className,
'classes' => array_map(fn (ClassType $class) => $class->toArray(), $this->classes),
'classes' => array_map(fn (ClassType $class) => $class->toArray(false), $this->classes),
];
}

Expand Down
6 changes: 4 additions & 2 deletions src/Reflection/Type/ClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ public static function create(Type $type): static
return $instance;
}

public function toArray(): array
public function toArray(bool $linkOnly = true): array
{
return [
return $linkOnly ? [
'@link' => $this->reflection->getReflection()?->getName(),
] : [
'classType' => self::class,
'reflection' => $this->reflection?->toArray(),
'type' => $this->type->toArray(),
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Type/CompoundType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public function toArray(): array
'type' => $this->type->toArray(),
];
}

public function getType(): Type
{
return $this->type;
}
}
16 changes: 10 additions & 6 deletions src/Reflection/Type/SimpleObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class SimpleObjectType implements TypeInterface
public const NATIVE_SIMPLE_OBJECTS = [
\ArrayIterator::class => [],
\ArrayObject::class => [],
\DateTime::class => ['staticConstructor' => NativeSimpleObject::class.'::DateTimeConstructor'],
\DateTimeImmutable::class => ['staticConstructor' => NativeSimpleObject::class.'::DateTimeConstructor'],
\DateTimeZone::class => ['staticConstructor' => NativeSimpleObject::class.'::DateTimeZoneConstructor'],
\DateTime::class => ['staticConstructor' => NativeSimpleObject::class . '::DateTimeConstructor'],
\DateTimeImmutable::class => ['staticConstructor' => NativeSimpleObject::class . '::DateTimeConstructor'],
\DateTimeZone::class => ['staticConstructor' => NativeSimpleObject::class . '::DateTimeZoneConstructor'],
\DateInterval::class => [],
];

Expand All @@ -43,7 +43,10 @@ public static function supports(Type $type): bool

foreach ($type->getTypes() as $t) {
if (class_exists($t, false)) {
if (isset(self::NATIVE_SIMPLE_OBJECTS['\\' . ltrim($t, '\\')])) {
if (
array_key_exists($t, self::NATIVE_SIMPLE_OBJECTS)
|| array_key_exists(ltrim($t, '\\'), self::NATIVE_SIMPLE_OBJECTS)
) {
return true;
}
$reflection = new ReflectionClass($t);
Expand All @@ -52,7 +55,7 @@ public static function supports(Type $type): bool
}
}
}

return $type->hasAttribute(SimpleObject::class);
}

Expand All @@ -74,7 +77,8 @@ public function __construct(
* @var null|object{"class": string, "arguments": mixed[], "instance": ?object} $simpleObjectAttr
*/
protected ?object $simpleObjectAttr,
) {}
) {
}

/**
* @return Type
Expand Down
20 changes: 6 additions & 14 deletions src/Reflection/Type/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PBaszak\DedicatedMapper\Reflection\Type;

use LogicException;
use PBaszak\DedicatedMapper\Attribute\ApplyToCollectionItems;
use PBaszak\DedicatedMapper\Reflection\PropertyReflection;
use phpDocumentor\Reflection\Type as PhpDocumentorType;
Expand Down Expand Up @@ -51,11 +52,6 @@ class Type implements TypeInterface
*/
protected bool $collection = false;

/**
* @var bool $simpleObject
*/
protected bool $simpleObject = false;

/**
* @var null|ReflectionType $reflectionType
*/
Expand Down Expand Up @@ -87,10 +83,14 @@ public function toArray(): array
'intersection' => $this->intersection,
'class' => $this->class,
'collection' => $this->collection,
'simpleObject' => $this->simpleObject,
];
}

public function getType(): self
{
throw new LogicException('Cannot get Type from Type.');
}

/**
* @return null|PropertyReflection|TypeInterface
*/
Expand Down Expand Up @@ -180,14 +180,6 @@ public function isCollection(): bool
{
return $this->collection;
}

/**
* @return bool
*/
public function isSimpleObject(): bool
{
return $this->simpleObject;
}

/**
* @return null|ReflectionType
Expand Down
7 changes: 3 additions & 4 deletions src/Reflection/Type/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function createFromString(string $type, ?Reflector $reflection = null, nu
if ($reflection) {
$ref = new ReflectionClass(Type::class);
$ref->getProperty('reflectionType')->setValue(
$instance,
$instance instanceof Type ? $instance : $instance->getType(),
match (get_class($reflection)) {
ReflectionProperty::class => $reflection->getType(),
ReflectionType::class => $reflection,
Expand Down Expand Up @@ -158,15 +158,15 @@ protected function createFromPhpDocumentatorType(PhpDocumentorType $type, null|P
protected function combineTypes(TypeInterface ...$types): TypeInterface
{
$types = array_filter($types);
$types = array_map(fn(TypeInterface $type): Type => method_exists($type, 'getType') ? $type->{'getType'}() : $type, $types);
$types = array_map(fn (TypeInterface $type): Type => $type instanceof Type ? $type : $type->getType(), $types);
$ref = new \ReflectionClass(Type::class);
/** @var Type $instance */
$instance = $ref->newInstanceWithoutConstructor();

foreach ($types as $type) {
$ref->getProperty('parent')->setValue($instance, $type->getParent());
$instanceTypes = $ref->getProperty('types')->isInitialized($instance) ? $ref->getProperty('types')->getValue($instance) : [];
$ref->getProperty('types')->setValue($instance, array_merge($instanceTypes, $type->getTypes()));
$ref->getProperty('types')->setValue($instance, array_unique(array_merge($instanceTypes, $type->getTypes())));
$instanceInnerType = $ref->getProperty('innerType')->getValue($instance);
if (null === $instanceInnerType) {
$ref->getProperty('innerType')->setValue($instance, $type->getInnerType());
Expand All @@ -178,7 +178,6 @@ protected function combineTypes(TypeInterface ...$types): TypeInterface
$ref->getProperty('intersection')->setValue($instance, $type->isIntersection() || $ref->getProperty('intersection')->getValue($instance));
$ref->getProperty('collection')->setValue($instance, $type->isCollection() || $ref->getProperty('collection')->getValue($instance));
$ref->getProperty('class')->setValue($instance, $type->isClass() || $ref->getProperty('class')->getValue($instance));
$ref->getProperty('simpleObject')->setValue($instance, $type->isSimpleObject() || $ref->getProperty('simpleObject')->getValue($instance));
$ref->getProperty('phpDocumentorReflectionType')->setValue($instance, $type->getPhpDocumentorReflectionType() ?? $ref->getProperty('phpDocumentorReflectionType')->getValue($instance));
$ref->getProperty('reflectionType')->setValue($instance, $type->getReflectionType() ?? $ref->getProperty('reflectionType')->getValue($instance));
}
Expand Down
1 change: 1 addition & 0 deletions src/Reflection/Type/TypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
interface TypeInterface
{
public function toArray(): array;
public function getType(): Type;
public static function supports(Type $type): bool;
public static function create(Type $type): TypeInterface;
}

0 comments on commit adf4660

Please sign in to comment.