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.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the "LICENSE.md"
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2011-02-20 19:54:11 +01:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
|
|
|
class StartsWith extends AbstractRule
|
|
|
|
{
|
|
|
|
public $startValue;
|
|
|
|
public $identical;
|
|
|
|
|
2015-01-08 03:15:35 +01:00
|
|
|
public function __construct($startValue, $identical = false)
|
2011-02-20 19:54:11 +01:00
|
|
|
{
|
|
|
|
$this->startValue = $startValue;
|
|
|
|
$this->identical = $identical;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
protected function validateEquals($input)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2012-04-08 02:37:37 +02:00
|
|
|
return 0 === mb_stripos($input, $this->startValue, 0, mb_detect_encoding($input));
|
2011-02-20 19:54:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function validateIdentical($input)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2012-04-08 02:37:37 +02:00
|
|
|
return 0 === mb_strpos($input, $this->startValue, 0, mb_detect_encoding($input));
|
2011-02-20 19:54:11 +01:00
|
|
|
}
|
|
|
|
}
|