2010-09-27 23:02:30 +02:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2015-06-08 17:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2010-09-27 23:02:30 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2018-03-16 16:29:21 +01:00
|
|
|
use ReflectionException;
|
2010-12-06 20:31:50 +01:00
|
|
|
use ReflectionProperty;
|
2011-02-07 11:52:18 +01:00
|
|
|
use Respect\Validation\Validatable;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2019-02-17 19:41:55 +01:00
|
|
|
use function is_object;
|
|
|
|
use function property_exists;
|
2010-09-27 23:02:30 +02:00
|
|
|
|
2018-03-16 16:29:21 +01:00
|
|
|
/**
|
2023-02-17 15:58:23 +01:00
|
|
|
* Validates an object attribute, even private ones.
|
2018-03-16 16:29:21 +01:00
|
|
|
*
|
2023-02-13 05:59:06 +01:00
|
|
|
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
2018-03-16 16:29:21 +01:00
|
|
|
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
*/
|
|
|
|
final class Attribute extends AbstractRelated
|
2010-09-27 23:02:30 +02:00
|
|
|
{
|
2019-05-05 16:22:44 +02:00
|
|
|
public function __construct(string $reference, ?Validatable $rule = null, bool $mandatory = true)
|
2010-12-06 22:00:28 +01:00
|
|
|
{
|
2019-05-05 16:22:44 +02:00
|
|
|
parent::__construct($reference, $rule, $mandatory);
|
2010-12-06 22:00:28 +01:00
|
|
|
}
|
|
|
|
|
2018-03-16 16:29:21 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-03-16 16:29:21 +01:00
|
|
|
*
|
|
|
|
* @throws ReflectionException
|
|
|
|
*/
|
2011-02-10 21:49:48 +01:00
|
|
|
public function getReferenceValue($input)
|
2010-11-30 17:21:33 +01:00
|
|
|
{
|
2019-05-05 16:22:44 +02:00
|
|
|
$propertyMirror = new ReflectionProperty($input, (string) $this->getReference());
|
2023-02-17 15:58:23 +01:00
|
|
|
if ($propertyMirror->isInitialized($input) === false) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2010-11-30 17:24:44 +01:00
|
|
|
return $propertyMirror->getValue($input);
|
2010-11-30 17:21:33 +01:00
|
|
|
}
|
|
|
|
|
2018-03-16 16:29:21 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-03-16 16:29:21 +01:00
|
|
|
*/
|
|
|
|
public function hasReference($input): bool
|
2011-02-07 11:52:18 +01:00
|
|
|
{
|
2019-05-05 16:22:44 +02:00
|
|
|
return is_object($input) && property_exists($input, (string) $this->getReference());
|
2011-02-07 11:52:18 +01:00
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|