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
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
2023-02-13 05:59:06 +01:00
|
|
|
* (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
2015-06-08 17:09:25 +02:00
|
|
|
*
|
2019-05-23 16:21:34 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE file
|
|
|
|
* that was distributed with this source code.
|
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;
|
|
|
|
|
2019-02-17 19:41:55 +01:00
|
|
|
use function is_array;
|
|
|
|
use function mb_stripos;
|
|
|
|
use function mb_strpos;
|
|
|
|
use function reset;
|
|
|
|
|
2018-11-25 17:56:04 +01:00
|
|
|
/**
|
2019-03-03 22:27:45 +01:00
|
|
|
* Validates whether the input starts with a given value.
|
|
|
|
*
|
2023-02-13 05:59:06 +01:00
|
|
|
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
2018-11-25 17:56:04 +01:00
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
* @author Marcelo Araujo <msaraujo@php.net>
|
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
final class StartsWith extends AbstractRule
|
2011-02-20 19:54:11 +01:00
|
|
|
{
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
private $startValue;
|
2019-02-07 20:46:46 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
private $identical;
|
2011-02-20 19:54:11 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $startValue
|
|
|
|
*/
|
|
|
|
public function __construct($startValue, bool $identical = false)
|
2011-02-20 19:54:11 +01:00
|
|
|
{
|
|
|
|
$this->startValue = $startValue;
|
|
|
|
$this->identical = $identical;
|
|
|
|
}
|
|
|
|
|
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
|
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
|
|
|
}
|
|
|
|
|
2011-04-04 20:07:12 +02:00
|
|
|
return $this->validateEquals($input);
|
2011-02-20 19:54:11 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $input
|
|
|
|
*/
|
|
|
|
protected 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 reset($input) == $this->startValue;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2021-02-06 17:42:11 +01:00
|
|
|
return mb_stripos($input, $this->startValue) === 0;
|
2011-02-20 19:54:11 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $input
|
|
|
|
*/
|
|
|
|
protected 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 reset($input) === $this->startValue;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2021-02-06 17:42:11 +01:00
|
|
|
return mb_strpos($input, $this->startValue) === 0;
|
2011-02-20 19:54:11 +01:00
|
|
|
}
|
|
|
|
}
|