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

StartWith::fillParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
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