Passed
Pull Request — master (#165)
by Sergei
02:53
created

LikeHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 7
c 0
b 0
f 0
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilterClass() 0 3 1
A match() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
6
7
use InvalidArgumentException;
8
use Yiisoft\Arrays\ArrayHelper;
9
use Yiisoft\Data\Reader\Filter\Like;
10
11
use Yiisoft\Data\Reader\FilterInterface;
12
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
13
14
use function is_string;
15
use function stripos;
16
17
/**
18
 * `Like` iterable filter handler ensures that the field value is like-match to a given value.
19
 */
20
final class LikeHandler implements IterableFilterHandlerInterface
21
{
22 143
    public function getFilterClass(): string
23
    {
24 143
        return Like::class;
25
    }
26
27 7
    public function match(object|array $item, FilterInterface $filter, array $iterableFilterHandlers): bool
28
    {
29 7
        if (!$filter instanceof Like) {
30 1
            throw new InvalidArgumentException('Incorrect filter.');
31
        }
32
33 6
        $itemValue = ArrayHelper::getValue($item, $filter->field);
34 6
        $argumentValue = $filter->value;
35
36 6
        return is_string($itemValue) && stripos($itemValue, $argumentValue) !== false;
37
    }
38
}
39