FilterTrait   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 66
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 15

1 Method

Rating   Name   Duplication   Size   Complexity  
C filter() 0 54 15
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * The Imaging Source Download System PHP Wrapper
6
 *
7
 * PHP wrapper for The Imaging Source Download System Web API. Authored and supported by The Imaging Source Europe GmbH.
8
 *
9
 * @link      http://dl-gui.theimagingsource.com to learn more about The Imaging Source Download System
10
 * @link      https://github.com/jonathanmaron/theimagingsource-tisd-sdk for the canonical source repository
11
 * @license   https://github.com/jonathanmaron/theimagingsource-tisd-sdk/blob/master/LICENSE.md
12
 * @copyright © 2022 The Imaging Source Europe GmbH
13
 */
14
15
namespace Tisd\Sdk;
16
17
/**
18
 * Trait FilterTrait
19
 *
20
 * @package Tisd\Sdk
21
 */
22
trait FilterTrait
23
{
24
    /**
25
     * Filter the packages by key
26
     *
27
     * @param array        $packages
28
     * @param string       $key
29
     * @param array|string $value
30
     * @param bool         $fuzzy
31
     *
32
     * @return array
33
     */
34 7
    protected function filter(array $packages, string $key, array|string $value, bool $fuzzy = false): array
35
    {
36
        // this approach is faster than recursively.
37
38 7
        foreach ($packages['children'] as $categoryId => $categories) {
39
40 7
            foreach ($categories['children'] as $sectionId => $sections) {
41
42 7
                foreach ($sections['children'] as $packageId => $package) {
43
44 7
                    if (!isset($package[$key])) {
45 1
                        continue;
46
                    }
47
48 6
                    if ($fuzzy) {
49 1
                        if (is_string($value)) {
50
                            // Fuzzy match: Package value is a string; passed value is a string
51
                            // Package value must start with passed value
52 1
                            $string = substr($package[$key], 0, strlen($value));
53 1
                            if (0 === strcasecmp($string, $value)) {
54 1
                                continue;
55
                            }
56
                        }
57 5
                    } elseif (is_array($value)) {
58
                        // Passed value is an array; package value is a string
59 1
                        if (in_array($package[$key], $value, true)) {
60 1
                            continue;
61
                        }
62 4
                    } elseif (is_array($package[$key])) {
63
                        // Package value is an array; passed value is a string
64 1
                        if (in_array($value, $package[$key], true)) {
65 1
                            continue;
66
                        }
67
                    } else {
68
                        // Package value is a string; passed value is a string
69 3
                        if ($value == $package[$key]) {
70 3
                            continue;
71
                        }
72
                    }
73
74 6
                    unset($packages['children'][$categoryId]['children'][$sectionId]['children'][$packageId]);
75
                }
76
77 7
                if (0 === count($packages['children'][$categoryId]['children'][$sectionId]['children'])) {
78 6
                    unset($packages['children'][$categoryId]['children'][$sectionId]);
79
                }
80
            }
81
82 7
            if (0 === count($packages['children'][$categoryId]['children'])) {
83 6
                unset($packages['children'][$categoryId]);
84
            }
85
        }
86
87 7
        return $packages;
88
    }
89
}
90