2017-06-29 04:18:02 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2017-06-29 04:18:02 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-06-29 04:18:02 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2019-03-04 14:35:28 +01:00
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2019-03-04 14:35:28 +01:00
|
|
|
use function array_values;
|
2019-02-17 19:41:55 +01:00
|
|
|
use function count;
|
2019-03-04 14:35:28 +01:00
|
|
|
use function is_array;
|
|
|
|
use function is_string;
|
|
|
|
use function sprintf;
|
|
|
|
use function str_split;
|
2019-02-17 19:41:55 +01:00
|
|
|
|
2018-11-25 17:56:04 +01:00
|
|
|
/**
|
2019-03-04 14:35:28 +01:00
|
|
|
* Validates whether the input is sorted in a certain order or not.
|
|
|
|
*
|
2018-11-25 17:56:04 +01:00
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
* @author Mikhail Vyrtsev <reeywhaar@gmail.com>
|
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
final class Sorted extends AbstractRule
|
2017-06-29 04:18:02 +02:00
|
|
|
{
|
2019-03-04 14:35:28 +01:00
|
|
|
public const ASCENDING = 'ASC';
|
|
|
|
public const DESCENDING = 'DESC';
|
2019-02-07 20:46:46 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2019-03-04 14:35:28 +01:00
|
|
|
* @var string
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2019-03-04 14:35:28 +01:00
|
|
|
private $direction;
|
2017-06-29 14:03:50 +02:00
|
|
|
|
2019-03-04 14:35:28 +01:00
|
|
|
public function __construct(string $direction)
|
2017-06-29 04:42:29 +02:00
|
|
|
{
|
2019-03-04 14:35:28 +01:00
|
|
|
if ($direction !== self::ASCENDING && $direction !== self::DESCENDING) {
|
|
|
|
throw new ComponentException(
|
|
|
|
sprintf('Direction should be either "%s" or "%s"', self::ASCENDING, self::DESCENDING)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->direction = $direction;
|
2017-06-29 04:42:29 +02:00
|
|
|
}
|
2017-06-29 04:18:02 +02:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2018-01-28 15:41:31 +01:00
|
|
|
public function validate($input): bool
|
2017-06-29 04:18:02 +02:00
|
|
|
{
|
2019-03-04 14:35:28 +01:00
|
|
|
if (!is_array($input) && !is_string($input)) {
|
|
|
|
return false;
|
2017-11-12 14:21:46 +01:00
|
|
|
}
|
2019-03-04 14:35:28 +01:00
|
|
|
|
|
|
|
$values = $this->getValues($input);
|
|
|
|
$count = count($values);
|
|
|
|
for ($position = 1; $position < $count; ++$position) {
|
|
|
|
if (!$this->isSorted($values[$position], $values[$position - 1])) {
|
2017-06-29 13:34:00 +02:00
|
|
|
return false;
|
2017-11-12 14:21:46 +01:00
|
|
|
}
|
2017-06-29 04:42:29 +02:00
|
|
|
}
|
2017-11-12 14:21:46 +01:00
|
|
|
|
2017-06-29 04:42:29 +02:00
|
|
|
return true;
|
2017-06-29 04:18:02 +02:00
|
|
|
}
|
2019-03-04 14:35:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $current
|
|
|
|
* @param mixed $last
|
|
|
|
*/
|
|
|
|
private function isSorted($current, $last): bool
|
|
|
|
{
|
|
|
|
if ($this->direction === self::ASCENDING) {
|
|
|
|
return $current > $last;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $current < $last;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|mixed[] $input
|
|
|
|
*
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
private function getValues($input): array
|
|
|
|
{
|
|
|
|
if (is_array($input)) {
|
|
|
|
return array_values($input);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str_split($input);
|
|
|
|
}
|
2017-06-29 04:18:02 +02:00
|
|
|
}
|