| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- /*
- * Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
- * SPDX-License-Identifier: MIT
- */
- declare(strict_types=1);
- namespace Respect\Validation\Rules;
- use ArrayAccess;
- use Respect\Validation\Exceptions\ComponentException;
- use function array_key_exists;
- use function array_shift;
- use function explode;
- use function is_array;
- use function is_null;
- use function is_object;
- use function is_scalar;
- use function property_exists;
- use function rtrim;
- use function sprintf;
- /**
- * @author Alexandre Gomes Gaigalas <alganet@gmail.com>
- * @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
- * @author Henrique Moody <henriquemoody@gmail.com>
- * @author Ivan Zinovyev <vanyazin@gmail.com>
- */
- final class KeyNested extends AbstractRelated
- {
- /**
- * {@inheritDoc}
- */
- public function hasReference($input): bool
- {
- try {
- $this->getReferenceValue($input);
- } catch (ComponentException $cex) {
- return false;
- }
- return true;
- }
- /**
- * {@inheritDoc}
- */
- public function getReferenceValue($input)
- {
- if (is_scalar($input)) {
- $message = sprintf('Cannot select the %s in the given data', $this->getReference());
- throw new ComponentException($message);
- }
- $keys = $this->getReferencePieces();
- $value = $input;
- while (!is_null($key = array_shift($keys))) {
- $value = $this->getValue($value, $key);
- }
- return $value;
- }
- /**
- * @return string[]
- */
- private function getReferencePieces(): array
- {
- return explode('.', rtrim((string) $this->getReference(), '.'));
- }
- /**
- * @param mixed[] $array
- * @param mixed $key
- *
- * @return mixed
- */
- private function getValueFromArray(array $array, $key)
- {
- if (!array_key_exists($key, $array)) {
- $message = sprintf('Cannot select the key %s from the given array', $this->getReference());
- throw new ComponentException($message);
- }
- return $array[$key];
- }
- /**
- * @param ArrayAccess<mixed, mixed> $array
- * @param mixed $key
- *
- * @return mixed
- */
- private function getValueFromArrayAccess(ArrayAccess $array, $key)
- {
- if (!$array->offsetExists($key)) {
- $message = sprintf('Cannot select the key %s from the given array', $this->getReference());
- throw new ComponentException($message);
- }
- return $array->offsetGet($key);
- }
- /**
- * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
- *
- *
- * @return mixed
- */
- private function getValueFromObject(object $object, string $property)
- {
- if (empty($property) || !property_exists($object, $property)) {
- $message = sprintf('Cannot select the property %s from the given object', $this->getReference());
- throw new ComponentException($message);
- }
- return $object->{$property};
- }
- /**
- * @param mixed $value
- * @param mixed $key
- *
- * @return mixed
- */
- private function getValue($value, $key)
- {
- if (is_array($value)) {
- return $this->getValueFromArray($value, $key);
- }
- if ($value instanceof ArrayAccess) {
- return $this->getValueFromArrayAccess($value, $key);
- }
- if (is_object($value)) {
- return $this->getValueFromObject($value, $key);
- }
- $message = sprintf('Cannot select the property %s from the given data', $this->getReference());
- throw new ComponentException($message);
- }
- }
|