Completed
Pull Request — master (#311)
by De Cramer
06:47
created

CompatibleFetcher::getCompatibleData()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 4
1
<?php
2
3
4
namespace eXpansion\Framework\Core\Helpers;
5
use eXpansion\Bundle\Maps\Model\Map;
6
use oliverde8\AssociativeArraySimplified\AssociativeArray;
7
8
9
/**
10
 * Class CompatibleFetcher
11
 *
12
 * @package eXpansion\Framework\Core\Helpers;
13
 * @author  oliver de Cramer <[email protected]>
14
 */
15
class CompatibleFetcher
16
{
17
    /** For compatibility with every title/mode/script */
18
    const COMPATIBLE_ALL = "ALL";
19
20
    /**
21
     * Get a compatible data.
22
     *
23
     * @param $haystack
24
     * @param $title
25
     * @param $mode
26
     * @param $script
27
     *
28
     * @return mixed|null
29
     */
30
    public function getCompatibleData($haystack, $title, $mode, $script)
31
    {
32
        // List of choices order by importance.
33
        $choices = [
34
            [$title, $mode, $script],
35
            [$title, $mode, self::COMPATIBLE_ALL],
36
            [$title, self::COMPATIBLE_ALL, self::COMPATIBLE_ALL],
37
            [self::COMPATIBLE_ALL, self::COMPATIBLE_ALL, self::COMPATIBLE_ALL],
38
            // that are common to all titles.
39
            [self::COMPATIBLE_ALL, $mode, $script],
40
            [self::COMPATIBLE_ALL, $mode, self::COMPATIBLE_ALL],
41
            [self::COMPATIBLE_ALL, self::COMPATIBLE_ALL, self::COMPATIBLE_ALL],
42
        ];
43
44
        foreach ($choices as $choice) {
45
            $data = AssociativeArray::getFromKey($haystack, $choice);
46
47
            if (!is_null($data)) {
48
                return $data;
49
            }
50
        }
51
52
        return null;
53
    }
54
}