* * 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 * @author Emmerson Siqueira * @author Henrique Moody */ 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); } }