Skip to content

Commit ddbb7e1

Browse files
committed
Add: attr emptyToNull. InvalidArgumentException
1 parent 1f01828 commit ddbb7e1

20 files changed

+139
-29
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "yzen.dev/plain-to-class",
3-
"version": "3.0.3",
3+
"version": "3.0.4",
44
"description": "Class-transformer to transform your dataset into a structured object",
55
"minimum-stability": "dev",
66
"prefer-stable": true,

composer.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ArgumentsRepository.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class ArgumentsRepository
2626

2727
/**
2828
*
29-
* @param iterable<mixed>|array ...$args
29+
* @param iterable<mixed>|object ...$args
3030
*/
3131
public function __construct(...$args)
3232
{

src/Attributes/EmptyToNull.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ClassTransformer\Attributes;
6+
7+
use Attribute;
8+
9+
/**
10+
*
11+
* @psalm-api
12+
*/
13+
#[Attribute(Attribute::TARGET_PARAMETER)]
14+
final class EmptyToNull
15+
{
16+
}

src/CacheGenerator/CacheGenerator.php

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ private function convertToCacheProperty(RuntimeReflectionProperty $property): Ca
120120
$property->type,
121121
$property->hasSetMutator(),
122122
$property->notTransform(),
123+
$property->convertEmptyToNull(),
123124
$property->getDocComment(),
124125
$args,
125126
$this->getAliases($args),

src/ClassRepository.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace ClassTransformer;
44

55
use ClassTransformer\Contracts\ReflectionProperty;
6-
use ClassTransformer\Exceptions\ClassNotFoundException;
7-
use ClassTransformer\Reflection\CacheReflectionProperty;
86
use ClassTransformer\Contracts\ReflectionClassRepository;
97

108
/**
@@ -14,22 +12,21 @@
1412
*/
1513
final class ClassRepository
1614
{
17-
/** @var class-string $class */
15+
/** @var string $class */
1816
private string $class;
1917

2018
/** @var ReflectionClassRepository $class */
2119
private ReflectionClassRepository $classRepository;
2220

2321
/**
24-
* @var array<string,CacheReflectionProperty[]>
22+
* @var array<string,ReflectionProperty[]>
2523
*/
2624
private static array $propertiesTypesCache = [];
2725

2826

2927
/**
30-
* @param class-string $class
31-
*
32-
* @throws ClassNotFoundException
28+
* @param string $class
29+
* @param ReflectionClassRepository $classRepository
3330
*/
3431
public function __construct(
3532
string $class,

src/Contracts/ReflectionClassRepository.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
interface ReflectionClassRepository
99
{
1010
/**
11-
* @return array<ReflectionProperty>
11+
* @return ReflectionProperty[]
1212
*/
1313
public function getProperties(): array;
1414

src/Contracts/ReflectionProperty.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ abstract class ReflectionProperty
1919
public PropertyType $type;
2020

2121
/**
22-
* @param string $name
22+
* @param class-string $name
2323
*
2424
* @return mixed
2525
*/
2626
abstract public function getAttribute(string $name): mixed;
2727

2828
/**
29-
* @param string $name
29+
* @param class-string $name
3030
*
3131
* @return null|array<string>
3232
*/
@@ -47,6 +47,11 @@ abstract public function hasSetMutator(): bool;
4747
*/
4848
abstract public function notTransform(): bool;
4949

50+
/**
51+
* @return bool
52+
*/
53+
abstract public function convertEmptyToNull(): bool;
54+
5055
/**
5156
* @return array<string>
5257
*/

src/Reflection/CacheReflectionClass.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
*/
1818
final class CacheReflectionClass implements ReflectionClassRepository
1919
{
20-
/** @var class-string $class */
20+
/** @var string $class */
2121
private string $class;
2222

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

2626
/**
27-
* @param class-string $class
27+
* @param string $class
28+
* @param CacheReflectionProperty[] $properties
2829
*/
2930
public function __construct(string $class, array $properties)
3031
{

src/Reflection/CacheReflectionProperty.php

+18
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@
1414
final class CacheReflectionProperty extends \ClassTransformer\Contracts\ReflectionProperty
1515
{
1616
/**
17+
* @param class-string $class
18+
* @param class-string|string $name
19+
* @param PropertyType $type
20+
* @param bool $hasSetMutator
21+
* @param bool $notTransform
22+
* @param bool $convertEmptyToNull
23+
* @param string $docComment
24+
* @param array $attributes
25+
* @param array $aliases
1726
*/
1827
public function __construct(
1928
public string $class,
2029
public string $name,
2130
public PropertyType $type,
2231
public bool $hasSetMutator,
2332
public bool $notTransform,
33+
public bool $convertEmptyToNull,
2434
public string $docComment,
2535
public array $attributes,
2636
public array $aliases,
@@ -42,6 +52,14 @@ public function notTransform(): bool
4252
{
4353
return $this->notTransform;
4454
}
55+
56+
/**
57+
* @return bool
58+
*/
59+
public function convertEmptyToNull(): bool
60+
{
61+
return $this->convertEmptyToNull;
62+
}
4563

4664
/**
4765
* @param string $name

src/Reflection/RuntimeReflectionProperty.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace ClassTransformer\Reflection;
66

7+
use ClassTransformer\Attributes\EmptyToNull;
78
use ReflectionProperty;
89
use ReflectionAttribute;
910
use ClassTransformer\TransformUtils;
@@ -58,7 +59,15 @@ public function notTransform(): bool
5859
}
5960

6061
/**
61-
* @param string $name
62+
* @return bool
63+
*/
64+
public function convertEmptyToNull(): bool
65+
{
66+
return $this->getAttribute(EmptyToNull::class) !== null;
67+
}
68+
69+
/**
70+
* @param class-string $name
6271
*
6372
* @template T
6473
* @return null|ReflectionAttribute
@@ -77,11 +86,11 @@ public function getAttribute(string $name): ?ReflectionAttribute
7786
}
7887

7988
/**
80-
* @param string|null $name
89+
* @param class-string $name
8190
*
8291
* @return null|array<string>
8392
*/
84-
public function getAttributeArguments(?string $name = null): ?array
93+
public function getAttributeArguments(string $name): ?array
8594
{
8695
return $this->getAttribute($name)?->getArguments();
8796
}

src/Reflection/Types/ArrayType.php

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
class ArrayType extends PropertyType
1313
{
14+
/** @var string|class-string */
1415
public string $itemsType;
1516
public bool $isScalarItems;
17+
1618
}

src/Reflection/Types/EnumType.php

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/**
66
* Class EnumType
77
*
8+
* @psalm-api
89
* @author yzen.dev <[email protected]>
910
*/
1011
class EnumType extends PropertyType

src/Reflection/Types/PropertyType.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
/**
88
* Class PropertyType
99
*
10+
* @psalm-api
1011
* @author yzen.dev <[email protected]>
1112
*/
1213
class PropertyType
1314
{
1415
/**
15-
* @param string $name Name of type
16+
* @param string|class-string $name Name of type
1617
* @param bool $isScalar
1718
* @param bool $isNullable
1819
*/

src/Reflection/Types/PropertyTypeFactory.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function create(RuntimeReflectionProperty $property)
3232
$isNullable = true;
3333

3434
if ($reflectionType instanceof ReflectionType) {
35-
$type = $reflectionType;
35+
$type = (string)$reflectionType;
3636
$isNullable = $reflectionType->allowsNull();
3737
}
3838
if ($reflectionType instanceof ReflectionNamedType) {
@@ -41,6 +41,14 @@ public static function create(RuntimeReflectionProperty $property)
4141
$isNullable = $reflectionType->allowsNull();
4242
}
4343

44+
if ($property->notTransform()) {
45+
return new ScalarType(
46+
$type,
47+
$isScalar,
48+
$isNullable
49+
);
50+
}
51+
4452
if ($type === TypeEnums::TYPE_ARRAY) {
4553
$arrayTypeAttr = $property->getAttributeArguments(ConvertArray::class);
4654

@@ -50,18 +58,19 @@ public static function create(RuntimeReflectionProperty $property)
5058
$arrayType = TransformUtils::getClassFromPhpDoc($property->getDocComment());
5159
}
5260
$arrayType ??= TypeEnums::TYPE_MIXED;
53-
$type = new ArrayType(
61+
62+
$typeInstance = new ArrayType(
5463
$type,
5564
$isScalar,
5665
$isNullable
5766
);
58-
$type->itemsType = $arrayType ?? TypeEnums::TYPE_MIXED;
59-
$type->isScalarItems = in_array($arrayType, [TypeEnums::TYPE_INTEGER, TypeEnums::TYPE_FLOAT, TypeEnums::TYPE_STRING, TypeEnums::TYPE_BOOLEAN, TypeEnums::TYPE_MIXED]);
67+
$typeInstance->itemsType = $arrayType ?? TypeEnums::TYPE_MIXED;
68+
$typeInstance->isScalarItems = in_array($arrayType, [TypeEnums::TYPE_INTEGER, TypeEnums::TYPE_FLOAT, TypeEnums::TYPE_STRING, TypeEnums::TYPE_BOOLEAN, TypeEnums::TYPE_MIXED]);
6069

61-
return $type;
70+
return $typeInstance;
6271
}
6372

64-
if ($isScalar || $property->notTransform()) {
73+
if ($isScalar) {
6574
return new ScalarType(
6675
$type,
6776
$isScalar,
@@ -80,7 +89,6 @@ public static function create(RuntimeReflectionProperty $property)
8089
return new TransformableType(
8190
$type,
8291
$isScalar,
83-
$isNullable
8492
);
8593
}
8694
}

src/Reflection/Types/ScalarType.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/**
88
* Class ScalarType
99
*
10+
* @psalm-api
1011
* @author yzen.dev <[email protected]>
1112
*/
1213
class ScalarType extends PropertyType

src/Reflection/Types/TransformableType.php

+10
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@
1111
*/
1212
class TransformableType extends PropertyType
1313
{
14+
/**
15+
* @param class-string $name Name of type
16+
* @param bool $isNullable
17+
*/
18+
public function __construct(
19+
public string $name,
20+
public bool $isNullable
21+
) {
22+
parent::__construct($this->name, false, $isNullable);
23+
}
1424
}

0 commit comments

Comments
 (0)