Skip to content

Commit

Permalink
Add: attr emptyToNull. InvalidArgumentException
Browse files Browse the repository at this point in the history
  • Loading branch information
yzen-dev committed Jul 14, 2023
1 parent 1f01828 commit ddbb7e1
Show file tree
Hide file tree
Showing 20 changed files with 139 additions and 29 deletions.
2 changes: 1 addition & 1 deletion composer.json
@@ -1,6 +1,6 @@
{
"name": "yzen.dev/plain-to-class",
"version": "3.0.3",
"version": "3.0.4",
"description": "Class-transformer to transform your dataset into a structured object",
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ArgumentsRepository.php
Expand Up @@ -26,7 +26,7 @@ final class ArgumentsRepository

/**
*
* @param iterable<mixed>|array ...$args
* @param iterable<mixed>|object ...$args
*/
public function __construct(...$args)
{
Expand Down
16 changes: 16 additions & 0 deletions src/Attributes/EmptyToNull.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace ClassTransformer\Attributes;

use Attribute;

/**
*
* @psalm-api
*/
#[Attribute(Attribute::TARGET_PARAMETER)]
final class EmptyToNull
{
}
1 change: 1 addition & 0 deletions src/CacheGenerator/CacheGenerator.php
Expand Up @@ -120,6 +120,7 @@ private function convertToCacheProperty(RuntimeReflectionProperty $property): Ca
$property->type,
$property->hasSetMutator(),
$property->notTransform(),
$property->convertEmptyToNull(),
$property->getDocComment(),
$args,
$this->getAliases($args),
Expand Down
11 changes: 4 additions & 7 deletions src/ClassRepository.php
Expand Up @@ -3,8 +3,6 @@
namespace ClassTransformer;

use ClassTransformer\Contracts\ReflectionProperty;
use ClassTransformer\Exceptions\ClassNotFoundException;
use ClassTransformer\Reflection\CacheReflectionProperty;
use ClassTransformer\Contracts\ReflectionClassRepository;

/**
Expand All @@ -14,22 +12,21 @@
*/
final class ClassRepository
{
/** @var class-string $class */
/** @var string $class */
private string $class;

/** @var ReflectionClassRepository $class */
private ReflectionClassRepository $classRepository;

/**
* @var array<string,CacheReflectionProperty[]>
* @var array<string,ReflectionProperty[]>
*/
private static array $propertiesTypesCache = [];


/**
* @param class-string $class
*
* @throws ClassNotFoundException
* @param string $class
* @param ReflectionClassRepository $classRepository
*/
public function __construct(
string $class,
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/ReflectionClassRepository.php
Expand Up @@ -8,7 +8,7 @@
interface ReflectionClassRepository
{
/**
* @return array<ReflectionProperty>
* @return ReflectionProperty[]
*/
public function getProperties(): array;

Expand Down
9 changes: 7 additions & 2 deletions src/Contracts/ReflectionProperty.php
Expand Up @@ -19,14 +19,14 @@ abstract class ReflectionProperty
public PropertyType $type;

/**
* @param string $name
* @param class-string $name
*
* @return mixed
*/
abstract public function getAttribute(string $name): mixed;

/**
* @param string $name
* @param class-string $name
*
* @return null|array<string>
*/
Expand All @@ -47,6 +47,11 @@ abstract public function hasSetMutator(): bool;
*/
abstract public function notTransform(): bool;

/**
* @return bool
*/
abstract public function convertEmptyToNull(): bool;

/**
* @return array<string>
*/
Expand Down
5 changes: 3 additions & 2 deletions src/Reflection/CacheReflectionClass.php
Expand Up @@ -17,14 +17,15 @@
*/
final class CacheReflectionClass implements ReflectionClassRepository
{
/** @var class-string $class */
/** @var string $class */
private string $class;

/** @var array CacheReflectionProperty[] */
private array $properties;

/**
* @param class-string $class
* @param string $class
* @param CacheReflectionProperty[] $properties
*/
public function __construct(string $class, array $properties)
{
Expand Down
18 changes: 18 additions & 0 deletions src/Reflection/CacheReflectionProperty.php
Expand Up @@ -14,13 +14,23 @@
final class CacheReflectionProperty extends \ClassTransformer\Contracts\ReflectionProperty
{
/**
* @param class-string $class
* @param class-string|string $name
* @param PropertyType $type
* @param bool $hasSetMutator
* @param bool $notTransform
* @param bool $convertEmptyToNull
* @param string $docComment
* @param array $attributes
* @param array $aliases
*/
public function __construct(
public string $class,
public string $name,
public PropertyType $type,
public bool $hasSetMutator,
public bool $notTransform,
public bool $convertEmptyToNull,
public string $docComment,
public array $attributes,
public array $aliases,
Expand All @@ -42,6 +52,14 @@ public function notTransform(): bool
{
return $this->notTransform;
}

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

/**
* @param string $name
Expand Down
15 changes: 12 additions & 3 deletions src/Reflection/RuntimeReflectionProperty.php
Expand Up @@ -4,6 +4,7 @@

namespace ClassTransformer\Reflection;

use ClassTransformer\Attributes\EmptyToNull;
use ReflectionProperty;
use ReflectionAttribute;
use ClassTransformer\TransformUtils;
Expand Down Expand Up @@ -58,7 +59,15 @@ public function notTransform(): bool
}

/**
* @param string $name
* @return bool
*/
public function convertEmptyToNull(): bool
{
return $this->getAttribute(EmptyToNull::class) !== null;
}

/**
* @param class-string $name
*
* @template T
* @return null|ReflectionAttribute
Expand All @@ -77,11 +86,11 @@ public function getAttribute(string $name): ?ReflectionAttribute
}

/**
* @param string|null $name
* @param class-string $name
*
* @return null|array<string>
*/
public function getAttributeArguments(?string $name = null): ?array
public function getAttributeArguments(string $name): ?array
{
return $this->getAttribute($name)?->getArguments();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/Types/ArrayType.php
Expand Up @@ -11,6 +11,8 @@
*/
class ArrayType extends PropertyType
{
/** @var string|class-string */
public string $itemsType;
public bool $isScalarItems;

}
1 change: 1 addition & 0 deletions src/Reflection/Types/EnumType.php
Expand Up @@ -5,6 +5,7 @@
/**
* Class EnumType
*
* @psalm-api
* @author yzen.dev <yzen.dev@gmail.com>
*/
class EnumType extends PropertyType
Expand Down
3 changes: 2 additions & 1 deletion src/Reflection/Types/PropertyType.php
Expand Up @@ -7,12 +7,13 @@
/**
* Class PropertyType
*
* @psalm-api
* @author yzen.dev <yzen.dev@gmail.com>
*/
class PropertyType
{
/**
* @param string $name Name of type
* @param string|class-string $name Name of type
* @param bool $isScalar
* @param bool $isNullable
*/
Expand Down
22 changes: 15 additions & 7 deletions src/Reflection/Types/PropertyTypeFactory.php
Expand Up @@ -32,7 +32,7 @@ public static function create(RuntimeReflectionProperty $property)
$isNullable = true;

if ($reflectionType instanceof ReflectionType) {
$type = $reflectionType;
$type = (string)$reflectionType;
$isNullable = $reflectionType->allowsNull();
}
if ($reflectionType instanceof ReflectionNamedType) {
Expand All @@ -41,6 +41,14 @@ public static function create(RuntimeReflectionProperty $property)
$isNullable = $reflectionType->allowsNull();
}

if ($property->notTransform()) {
return new ScalarType(
$type,
$isScalar,
$isNullable
);
}

if ($type === TypeEnums::TYPE_ARRAY) {
$arrayTypeAttr = $property->getAttributeArguments(ConvertArray::class);

Expand All @@ -50,18 +58,19 @@ public static function create(RuntimeReflectionProperty $property)
$arrayType = TransformUtils::getClassFromPhpDoc($property->getDocComment());
}
$arrayType ??= TypeEnums::TYPE_MIXED;
$type = new ArrayType(

$typeInstance = new ArrayType(
$type,
$isScalar,
$isNullable
);
$type->itemsType = $arrayType ?? TypeEnums::TYPE_MIXED;
$type->isScalarItems = in_array($arrayType, [TypeEnums::TYPE_INTEGER, TypeEnums::TYPE_FLOAT, TypeEnums::TYPE_STRING, TypeEnums::TYPE_BOOLEAN, TypeEnums::TYPE_MIXED]);
$typeInstance->itemsType = $arrayType ?? TypeEnums::TYPE_MIXED;
$typeInstance->isScalarItems = in_array($arrayType, [TypeEnums::TYPE_INTEGER, TypeEnums::TYPE_FLOAT, TypeEnums::TYPE_STRING, TypeEnums::TYPE_BOOLEAN, TypeEnums::TYPE_MIXED]);

return $type;
return $typeInstance;
}

if ($isScalar || $property->notTransform()) {
if ($isScalar) {
return new ScalarType(
$type,
$isScalar,
Expand All @@ -80,7 +89,6 @@ public static function create(RuntimeReflectionProperty $property)
return new TransformableType(
$type,
$isScalar,
$isNullable
);
}
}
1 change: 1 addition & 0 deletions src/Reflection/Types/ScalarType.php
Expand Up @@ -7,6 +7,7 @@
/**
* Class ScalarType
*
* @psalm-api
* @author yzen.dev <yzen.dev@gmail.com>
*/
class ScalarType extends PropertyType
Expand Down
10 changes: 10 additions & 0 deletions src/Reflection/Types/TransformableType.php
Expand Up @@ -11,4 +11,14 @@
*/
class TransformableType extends PropertyType
{
/**
* @param class-string $name Name of type
* @param bool $isNullable
*/
public function __construct(
public string $name,
public bool $isNullable
) {
parent::__construct($this->name, false, $isNullable);
}
}

0 comments on commit ddbb7e1

Please sign in to comment.