FinderUtils   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkMandatoryRules() 0 17 5
A buildRegex() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebThumbnailer\Utils;
6
7
/**
8
 * Utility class used by Finders.
9
 */
10
class FinderUtils
11
{
12
    /**
13
     * Format a regex for PHP, with delimiters and flags.
14
     *
15
     * Using brackets delimiter, can't search bracket parameter.
16
     * Fine for WebThumnailer.
17
     *
18
     * @param string $regex regex to format
19
     * @param string $flags regex flags
20
     *
21
     * @return string Formatted regex.
22
     */
23
    public static function buildRegex(string $regex, string $flags): string
24
    {
25
        return '{' . $regex . '}' . $flags;
26
    }
27
28
    /**
29
     * Make sure that given rules contain all mandatory fields.
30
     * Support nested arrays.
31
     *
32
     * @param mixed[] $rules         List of loaded rules.
33
     * @param mixed[] $mandatoryKeys List of mandatory rules expected.
34
     *
35
     * @return bool if all mandatory rules are provided, false otherwise.
36
     */
37
    public static function checkMandatoryRules(array $rules, array $mandatoryKeys): bool
38
    {
39
        foreach ($mandatoryKeys as $key => $value) {
40
            if (is_array($value)) {
41
                if (isset($rules[$key])) {
42
                    return static::checkMandatoryRules($rules[$key], $value);
43
                } else {
44
                    return false;
45
                }
46
            } else {
47
                if (! isset($rules[$value])) {
48
                    return false;
49
                }
50
            }
51
        }
52
53
        return true;
54
    }
55
}
56