Passed
Push — dev ( 4ef148...eba0dc )
by Dispositif
07:17
created

ArrayProcessTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 76
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A arrayKeysToLower() 0 8 2
B completeFantasyOrder() 0 29 7
A deleteEmptyValueArray() 0 6 2
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 : Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\Utils;
11
12
trait ArrayProcessTrait
13
{
14
    /**
15
     * Set all array keys to lower case.
16
     *
17
     * @param array $array
18
     *
19
     * @return array
20
     */
21
    private static function arrayKeysToLower(array $array): array
22
    {
23
        $res = [];
24
        foreach ($array as $key => $val) {
25
            $res[mb_strtolower($key)] = $val;
26
        }
27
28
        return $res;
29
    }
30
31
    /**
32
     * Delete keys with empty string value "".
33
     *
34
     * @param array $myArray
35
     *
36
     * @return array
37
     */
38
    public function deleteEmptyValueArray(array $myArray): array
39
    {
40
        return array_filter(
41
            $myArray,
42
            function ($value) {
43
                return !is_null($value) && '' !== $value;
44
            }
45
        );
46
    }
47
48
    /**
49
     * Generate the complete list of parameters from the fantasy order list
50
     * with additionnal parameters inserted in the place of the cleanOrder list.
51
     * Génère la liste complète de paramètres possibles d'après l'ordre
52
     * fantaisiste (humain) et l'ordre officiel (documentation).
53
     *
54
     * @param array $fantasyOrder
55
     * @param array $cleanOrder
56
     *
57
     * @return array
58
     */
59
    public function completeFantasyOrder(array $fantasyOrder, array $cleanOrder): array
60
    {
61
        $lastFantasy = null;
62
        $firstParameters = [];
63
        $before = [];
64
65
        // Fait lien entre param et sa place par rapport à l'ordre fantaisiste
66
        foreach ($cleanOrder as $param) {
67
            if (in_array($param, $fantasyOrder)) {
68
                $lastFantasy = $param;
69
                continue;
70
            }
71
            if (is_null($lastFantasy) && !in_array($param, $fantasyOrder)) {
72
                $firstParameters[] = $param;
73
                continue;
74
            }
75
            $before[$param] = $lastFantasy;
76
        }
77
78
        $result = $firstParameters;
79
        foreach ($fantasyOrder as $param) {
80
            $result[] = $param;
81
            // Des paramètres additionnels sont placés après ?
82
            if (in_array($param, $before)) {
83
                $result = array_merge($result, array_keys($before, $param));
84
            }
85
        }
86
87
        return $result;
88
    }
89
}
90