Skip to content

Commit

Permalink
Add new Attribute SkipSerialization
Browse files Browse the repository at this point in the history
  • Loading branch information
compositephp committed May 20, 2023
1 parent 1c0ad37 commit dce9f54
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Attributes/SkipSerialization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Composite\Entity\Attributes;

#[\Attribute(\Attribute::TARGET_PARAMETER | \Attribute::TARGET_PROPERTY)]
class SkipSerialization
{
}
3 changes: 3 additions & 0 deletions src/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public static function fromReflection(\ReflectionClass $reflectionClass): array
$propertyAttributes = [];
foreach ($property->getAttributes() as $attribute) {
$attributeInstance = $attribute->newInstance();
if ($attributeInstance instanceof Attributes\SkipSerialization) {
continue 2;
}
$propertyAttributes[$attributeInstance::class] = $attributeInstance;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/ColumnBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Composite\Entity\Tests;

use Composite\Entity\AbstractEntity;
use Composite\Entity\Attributes\SkipSerialization;
use Composite\Entity\Columns\AbstractColumn;

final class ColumnBuilderTest extends \PHPUnit\Framework\TestCase
Expand All @@ -25,6 +26,26 @@ public function __construct(
},
'expected' => ['pub1', 'prot1', 'pub2', 'prot2']
],
[
'entity' => new class extends AbstractEntity {
public int $var4 = 4;
#[SkipSerialization]
public int $var5 = 5;
protected int $var6 = 6;
#[SkipSerialization]
protected int $var7 = 7;
private int $var8 = 8;

public function __construct(
public int $var1 = 1,
#[SkipSerialization]
public int $var2 = 2,
#[SkipSerialization]
protected int $var3 = 3,
) {}
},
'expected' => ['var1', 'var4', 'var6',]
],
];
}

Expand Down

0 comments on commit dce9f54

Please sign in to comment.