Skip to content

Commit

Permalink
Merge pull request #72 from naxvog/ignore-properties-attribute
Browse files Browse the repository at this point in the history
Add attribute to ignore public properties
  • Loading branch information
WendellAdriel committed Oct 30, 2023
2 parents 450e218 + 6d748a5 commit b635e87
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Attributes/IgnoreProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace WendellAdriel\Lift\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class IgnoreProperties
{
/** @var string[] */
public array $ignoredProperties;

public function __construct(
string $ignoredProperty,
string ...$additionalIgnoredProperties
) {
$this->ignoredProperties = [$ignoredProperty, ...$additionalIgnoredProperties];
}
}
7 changes: 7 additions & 0 deletions src/Lift.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Validation\ValidationException;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use ReflectionProperty;
use WendellAdriel\Lift\Attributes\IgnoreProperties;
use WendellAdriel\Lift\Concerns\AttributesGuard;
use WendellAdriel\Lift\Concerns\CastValues;
use WendellAdriel\Lift\Concerns\CustomPrimary;
Expand Down Expand Up @@ -183,6 +185,10 @@ public function setUniqueIds()

protected static function ignoredProperties(): array
{
$reflectionClass = new ReflectionClass(self::class);
$ignoredProperties = collect($reflectionClass->getAttributes(IgnoreProperties::class))
->flatMap(fn (ReflectionAttribute $attribute) => $attribute->getArguments());

return [
'incrementing',
'preventsLazyLoading',
Expand All @@ -193,6 +199,7 @@ protected static function ignoredProperties(): array
'manyMethods',
'timestamps',
'usesUniqueIds',
...$ignoredProperties,
];
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Datasets/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
use WendellAdriel\Lift\Attributes\Events\Dispatches;
use WendellAdriel\Lift\Attributes\Events\Listener;
use WendellAdriel\Lift\Attributes\Events\Observer;
use WendellAdriel\Lift\Attributes\IgnoreProperties;
use WendellAdriel\Lift\Lift;

#[Observer(ProductObserver::class)]
#[Dispatches(ProductSaving::class)]
#[Dispatches(ProductSaved::class, 'saved')]
#[IgnoreProperties('hash', 'hash2')]
#[IgnoreProperties('hash3')]
class Product extends Model
{
use Lift;
Expand All @@ -38,6 +41,12 @@ class Product extends Model
#[Cast('array')]
public ?array $json_column;

public string $hash;

public string $hash2;

public string $hash3;

protected $fillable = [
'name',
'price',
Expand Down
41 changes: 41 additions & 0 deletions tests/Feature/IgnoredPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

use Tests\Datasets\Product;
use Tests\Datasets\User;

it('ignores no additional properties if not set', function () {
$rMethod = new ReflectionMethod(User::class, 'ignoredProperties');
expect($rMethod->invoke(null))
->toMatchArray([
'incrementing',
'preventsLazyLoading',
'exists',
'wasRecentlyCreated',
'snakeAttributes',
'encrypter',
'manyMethods',
'timestamps',
'usesUniqueIds',
]);
});

it('ignores additional properties', function () {
$rMethod = new ReflectionMethod(Product::class, 'ignoredProperties');
expect($rMethod->invoke(null))
->toMatchArray([
'incrementing',
'preventsLazyLoading',
'exists',
'wasRecentlyCreated',
'snakeAttributes',
'encrypter',
'manyMethods',
'timestamps',
'usesUniqueIds',
'hash',
'hash2',
'hash3',
]);
});

0 comments on commit b635e87

Please sign in to comment.