Completed
Push — master ( d676b1...ecc147 )
by Dispositif
02:12
created

ArrayProcessTrait::completeFantasyOrder()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
c 0
b 0
f 0
nc 12
nop 2
dl 0
loc 29
rs 8.8333
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
     * Delete keys with empty string value "".
16
     *
17
     * @param array $myArray
18
     *
19
     * @return array
20
     */
21
    public function deleteEmptyValueArray(array $myArray): array
22
    {
23
        return array_filter(
24
            $myArray,
25
            function ($value) {
26
                return !is_null($value) && '' !== $value;
27
            }
28
        );
29
    }
30
31
    /**
32
     * Generate the complete list of parameters from the fantasy order list
33
     * with additionnal parameters inserted in the place of the cleanOrder list.
34
     * Génère la liste complète de paramètres possibles d'après l'ordre
35
     * fantaisiste (humain) et l'ordre officiel (documentation).
36
     *
37
     * @param array $fantasyOrder
38
     * @param array $cleanOrder
39
     *
40
     * @return array
41
     */
42
    public function completeFantasyOrder(array $fantasyOrder, array $cleanOrder): array
43
    {
44
        $lastFantasy = null;
45
        $firstParameters = [];
46
        $before = [];
47
48
        // Fait lien entre param et sa place par rapport à l'ordre fantaisiste
49
        foreach ($cleanOrder as $param) {
50
            if (in_array($param, $fantasyOrder)) {
51
                $lastFantasy = $param;
52
                continue;
53
            }
54
            if (is_null($lastFantasy) && !in_array($param, $fantasyOrder)) {
55
                $firstParameters[] = $param;
56
                continue;
57
            }
58
            $before[$param] = $lastFantasy;
59
        }
60
61
        $result = $firstParameters;
62
        foreach ($fantasyOrder as $param) {
63
            $result[] = $param;
64
            // Des paramètres additionnels sont placés après ?
65
            if (in_array($param, $before)) {
66
                $result = array_merge($result, array_keys($before, $param));
67
            }
68
        }
69
70
        return $result;
71
    }
72
}
73