2011-02-20 19:54:11 +01: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);
|
|
|
|
|
2011-02-20 19:54:11 +01:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2018-06-17 02:26:38 +02:00
|
|
|
use function end;
|
|
|
|
use function is_array;
|
|
|
|
use function mb_strlen;
|
|
|
|
use function mb_strripos;
|
|
|
|
use function mb_strrpos;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates only if the value is at the end of the input.
|
|
|
|
*
|
2023-02-13 05:59:06 +01:00
|
|
|
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
2018-06-17 02:26:38 +02:00
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
* @author Hugo Hamon <hugo.hamon@sensiolabs.com>
|
|
|
|
* @author William Espindola <oi@williamespindola.com.br>
|
|
|
|
*/
|
|
|
|
final class EndsWith extends AbstractRule
|
2011-02-20 19:54:11 +01:00
|
|
|
{
|
2018-06-17 02:26:38 +02:00
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
private $endValue;
|
2018-06-17 02:26:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
private $identical;
|
2011-02-20 19:54:11 +01:00
|
|
|
|
2018-06-17 02:26:38 +02:00
|
|
|
/**
|
|
|
|
* @param mixed $endValue
|
|
|
|
*/
|
|
|
|
public function __construct($endValue, bool $identical = false)
|
2011-02-20 19:54:11 +01:00
|
|
|
{
|
|
|
|
$this->endValue = $endValue;
|
|
|
|
$this->identical = $identical;
|
|
|
|
}
|
|
|
|
|
2018-06-17 02:26:38 +02:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-06-17 02:26:38 +02:00
|
|
|
*/
|
2018-01-28 15:41:31 +01:00
|
|
|
public function validate($input): bool
|
2011-02-20 19:54:11 +01:00
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
if ($this->identical) {
|
2011-02-20 19:54:11 +01:00
|
|
|
return $this->validateIdentical($input);
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2015-03-28 12:43:27 +01:00
|
|
|
|
|
|
|
return $this->validateEquals($input);
|
2011-02-20 19:54:11 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $input
|
|
|
|
*/
|
2018-06-17 02:26:38 +02:00
|
|
|
private function validateEquals($input): bool
|
2011-02-20 19:54:11 +01:00
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
if (is_array($input)) {
|
2011-02-20 19:54:11 +01:00
|
|
|
return end($input) == $this->endValue;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2015-03-28 12:43:27 +01:00
|
|
|
|
2021-02-06 17:42:11 +01:00
|
|
|
return mb_strripos($input, $this->endValue) === mb_strlen($input) - mb_strlen($this->endValue);
|
2011-02-20 19:54:11 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $input
|
|
|
|
*/
|
2018-06-17 02:26:38 +02:00
|
|
|
private function validateIdentical($input): bool
|
2011-02-20 19:54:11 +01:00
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
if (is_array($input)) {
|
2011-02-20 19:54:11 +01:00
|
|
|
return end($input) === $this->endValue;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2015-03-28 12:43:27 +01:00
|
|
|
|
2021-02-06 17:42:11 +01:00
|
|
|
return mb_strrpos($input, $this->endValue) === mb_strlen($input) - mb_strlen($this->endValue);
|
2011-02-20 19:54:11 +01:00
|
|
|
}
|
|
|
|
}
|