Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [ '8.1' ]
php: [ '8.1', '8.2', '8.3', '8.4' ]

name: PHP ${{ matrix.php }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea/
cache/
*.cache
.phpbench/
/tests/coverage
/coverage.xml

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"license": "MIT",
"require": {
"php": "^8.1",
"respect/validation": "^2.0"
"respect/validation": "^2.0",
"attributes-php/options": "^0.1"
},
"autoload": {
"psr-4": {
Expand All @@ -26,13 +27,13 @@
]
},
"require-dev": {
"laravel/pint": "*",
"laravel/pint": "1.20.0",
"pestphp/pest": "^2.36",
"phpbench/phpbench": "^1.4"
},
"scripts": {
"lint": "./vendor/bin/pint --config pint.json",
"benchmark": "./vendor/bin/phpbench run tests/Benchmark/Benchmark.php",
"benchmark": "./vendor/bin/phpbench run tests/Benchmark/Base.php --tag=base --quiet && ./vendor/bin/phpbench run tests/Benchmark/Benchmark.php --report=aggregate --ref=base",
"test:lint": "./vendor/bin/pint --test",
"test:coverage": "./vendor/bin/pest --coverage --parallel",
"test:unit": "./vendor/bin/pest --colors=always --exclude-group=integration --parallel",
Expand Down
57 changes: 50 additions & 7 deletions composer.lock

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

7 changes: 0 additions & 7 deletions src/Exceptions/InvalidOptionException.php

This file was deleted.

23 changes: 0 additions & 23 deletions src/Options/Alias.php

This file was deleted.

78 changes: 0 additions & 78 deletions src/Options/AliasGenerator.php

This file was deleted.

24 changes: 23 additions & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace Attributes\Validation;

use Attributes\Options;
use Attributes\Options\Exceptions\InvalidOptionException;
use Attributes\Validation\Exceptions\ContextPropertyException;
use Attributes\Validation\Exceptions\ContinueValidationException;
use Attributes\Validation\Exceptions\StopValidationException;
use Attributes\Validation\Exceptions\ValidationException;
use Attributes\Validation\Options as Options;
use Attributes\Validation\Validators\AttributesValidator;
use Attributes\Validation\Validators\ChainValidator;
use Attributes\Validation\Validators\PropertyValidator;
Expand Down Expand Up @@ -55,6 +56,7 @@ public function __construct(?PropertyValidator $validator = null, bool $stopFirs
* @throws ValidationException - If validation fails
* @throws ContextPropertyException - If unable to retrieve a given context property
* @throws ReflectionException
* @throws InvalidOptionException
*/
public function validate(array $data, string|object $model): object
{
Expand All @@ -74,6 +76,10 @@ public function validate(array $data, string|object $model): object
$this->context->set(ErrorHolder::class, $errorInfo, override: true);
$defaultAliasGenerator = $this->getDefaultAliasGenerator($reflectionClass);
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
if (! $this->isToValidate($reflectionProperty)) {
continue;
}

$propertyName = $reflectionProperty->getName();
$aliasName = $this->getAliasName($reflectionProperty, $defaultAliasGenerator);
$this->context->push('internal.currentProperty', $propertyName);
Expand Down Expand Up @@ -125,6 +131,7 @@ private function getDefaultPropertyValidator(): PropertyValidator
* Retrieves the default alias generator for a given class
*
* @throws ContextPropertyException
* @throws InvalidOptionException
*/
private function getDefaultAliasGenerator(ReflectionClass $reflectionClass): callable
{
Expand Down Expand Up @@ -160,4 +167,19 @@ private function getAliasName(ReflectionProperty $reflectionProperty, callable $

return $defaultAliasGenerator($propertyName);
}

/**
* Checks if a given property is to be ignored
*/
private function isToValidate(ReflectionProperty $reflectionProperty): bool
{
$allAttributes = $reflectionProperty->getAttributes(Options\Ignore::class);
foreach ($allAttributes as $attribute) {
$instance = $attribute->newInstance();

return ! $instance->ignoreValidation();
}

return true;
}
}
Loading