Passed
Push — main ( 926316...d9c688 )
by Dimitri
02:57
created

StartWith::check()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 15
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Rules;
13
14
use BlitzPHP\Utilities\Iterable\Arr;
15
use BlitzPHP\Utilities\String\Text;
16
use Rakit\Validation\Rule;
17
18
class StartWith extends AbstractRule
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23
    public function fillParameters(array $params): Rule
24
    {
25 4
        return $this->fillAllowedValuesParameters($params);
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function check($value): bool
32
    {
33 4
        $this->requireParameters(['allowed_values']);
34
35 4
        $this->setAllowedValues($allowedValues = $this->parameter('allowed_values'));
36
37
        if (is_array($value)) {
38 4
            return Arr::first($value) === $allowedValues;
39
        }
40
41
        if (is_string($value)) {
42 4
            return Text::startsWith($value, $allowedValues);
43
        }
44
45 2
        return false;
46
    }
47
}
48