mirror of
https://github.com/Respect/Validation.git
synced 2024-06-03 06:12:17 +02:00
Check if property is initialized before getting its value
This commit also removed the use of "setAccessible", since it's not neccessary after PHP 8.1. Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
6e3ed94076
commit
8d7d783698
|
@ -17,6 +17,7 @@ Fixes:
|
||||||
- `KeySet` now reports which extra keys are causing the rule to fail.
|
- `KeySet` now reports which extra keys are causing the rule to fail.
|
||||||
- Ensure empty strings are never a valid currency code
|
- Ensure empty strings are never a valid currency code
|
||||||
- Do not hide messages on EachException
|
- Do not hide messages on EachException
|
||||||
|
- Dot not throw exception when validating an uninitialized property
|
||||||
|
|
||||||
Changes:
|
Changes:
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ use function is_object;
|
||||||
use function property_exists;
|
use function property_exists;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates an object attribute, event private ones.
|
* Validates an object attribute, even private ones.
|
||||||
*
|
*
|
||||||
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||||
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
|
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
|
||||||
|
@ -38,7 +38,9 @@ final class Attribute extends AbstractRelated
|
||||||
public function getReferenceValue($input)
|
public function getReferenceValue($input)
|
||||||
{
|
{
|
||||||
$propertyMirror = new ReflectionProperty($input, (string) $this->getReference());
|
$propertyMirror = new ReflectionProperty($input, (string) $this->getReference());
|
||||||
$propertyMirror->setAccessible(true);
|
if ($propertyMirror->isInitialized($input) === false) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return $propertyMirror->getValue($input);
|
return $propertyMirror->getValue($input);
|
||||||
}
|
}
|
||||||
|
|
17
tests/library/Stubs/WithUninitialized.php
Normal file
17
tests/library/Stubs/WithUninitialized.php
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Respect\Validation\Test\Stubs;
|
||||||
|
|
||||||
|
final class WithUninitialized
|
||||||
|
{
|
||||||
|
public string $initialized = 'foo';
|
||||||
|
|
||||||
|
public string $uninitialized;
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ namespace Respect\Validation\Rules;
|
||||||
|
|
||||||
use Respect\Validation\Test\RuleTestCase;
|
use Respect\Validation\Test\RuleTestCase;
|
||||||
use Respect\Validation\Test\Stubs\WithProperties;
|
use Respect\Validation\Test\Stubs\WithProperties;
|
||||||
|
use Respect\Validation\Test\Stubs\WithUninitialized;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group rule
|
* @group rule
|
||||||
|
@ -47,6 +48,10 @@ final class AttributeTest extends RuleTestCase
|
||||||
new Attribute('public', new AlwaysValid()),
|
new Attribute('public', new AlwaysValid()),
|
||||||
new WithProperties(),
|
new WithProperties(),
|
||||||
],
|
],
|
||||||
|
'attribute is present but uninitialized' => [
|
||||||
|
new Attribute('uninitialized'),
|
||||||
|
new WithUninitialized(),
|
||||||
|
],
|
||||||
'non mandatory attribute is not present' => [
|
'non mandatory attribute is not present' => [
|
||||||
new Attribute('nonexistent', null, false),
|
new Attribute('nonexistent', null, false),
|
||||||
new WithProperties(),
|
new WithProperties(),
|
||||||
|
@ -55,6 +60,10 @@ final class AttributeTest extends RuleTestCase
|
||||||
new Attribute('nonexistent', new AlwaysValid(), false),
|
new Attribute('nonexistent', new AlwaysValid(), false),
|
||||||
new WithProperties(),
|
new WithProperties(),
|
||||||
],
|
],
|
||||||
|
'attribute is present but uninitialized with extra validator' => [
|
||||||
|
new Attribute('uninitialized', new AlwaysValid()),
|
||||||
|
new WithUninitialized(),
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue