respect-validation/library/Rules/Attribute.php
Henrique Moody c30603759e
Apply "SlevomatCodingStandard.TypeHints.TypeHintDeclaration"
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-02-09 14:09:28 +01:00

54 lines
1.3 KiB
PHP

<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use ReflectionException;
use ReflectionProperty;
use Respect\Validation\Validatable;
/**
* Validates an object attribute, event private ones.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class Attribute extends AbstractRelated
{
public function __construct(string $reference, Validatable $validator = null, bool $mandatory = true)
{
parent::__construct($reference, $validator, $mandatory);
}
/**
* {@inheritdoc}
*
* @throws ReflectionException
*/
public function getReferenceValue($input)
{
$propertyMirror = new ReflectionProperty($input, $this->reference);
$propertyMirror->setAccessible(true);
return $propertyMirror->getValue($input);
}
/**
* {@inheritdoc}
*/
public function hasReference($input): bool
{
return is_object($input) && property_exists($input, $this->reference);
}
}