HighSpeaker/vendor/workerman/validation/library/Rules/EndsWith.php

83 lines
1.7 KiB
PHP
Raw Normal View History

2022-12-24 22:10:40 +08:00
<?php
/*
2024-01-31 22:15:08 +08:00
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
2022-12-24 22:10:40 +08:00
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
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.
*
2024-01-31 22:15:08 +08:00
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
2022-12-24 22:10:40 +08: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
{
/**
* @var mixed
*/
private $endValue;
/**
* @var bool
*/
private $identical;
/**
* @param mixed $endValue
*/
public function __construct($endValue, bool $identical = false)
{
$this->endValue = $endValue;
$this->identical = $identical;
}
/**
* {@inheritDoc}
*/
public function validate($input): bool
{
if ($this->identical) {
return $this->validateIdentical($input);
}
return $this->validateEquals($input);
}
/**
* @param mixed $input
*/
private function validateEquals($input): bool
{
if (is_array($input)) {
return end($input) == $this->endValue;
}
2024-01-31 22:15:08 +08:00
return mb_strripos($input, $this->endValue) === mb_strlen($input) - mb_strlen($this->endValue);
2022-12-24 22:10:40 +08:00
}
/**
* @param mixed $input
*/
private function validateIdentical($input): bool
{
if (is_array($input)) {
return end($input) === $this->endValue;
}
2024-01-31 22:15:08 +08:00
return mb_strrpos($input, $this->endValue) === mb_strlen($input) - mb_strlen($this->endValue);
2022-12-24 22:10:40 +08:00
}
}