StartWith   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 28
ccs 5
cts 6
cp 0.8333
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 15 3
A fillParameters() 0 3 1
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 2
        return $this->fillAllowedValuesParameters($params);
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function check($value): bool
32
    {
33 2
        $this->requireParameters(['allowed_values']);
34
35 2
        $this->setAllowedValues($allowedValues = $this->parameter('allowed_values'));
36
37
        if (is_array($value)) {
38 2
            return Arr::first($value) === $allowedValues;
39
        }
40
41
        if (is_string($value)) {
42 2
            return Text::startsWith($value, $allowedValues);
43
        }
44
45
        return false;
46
    }
47
}
48