Completed
Push — master ( 9eb3ea...4b6fbd )
by Gareth
02:08
created

RestrictionFormatter::formatExpression()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace garethp\ews;
4
5
class RestrictionFormatter
6
{
7
    public static function format($preference, $restrictions)
8
    {
9
        foreach ($restrictions as $restrictionType => $query) {
10
            if (!in_array($restrictionType, ['And', 'Not', 'Or'])) {
11
                $restrictions[$restrictionType] = self::formatExpression($preference, $query);
12
            } else {
13
                $restrictions[$restrictionType] = self::format($preference, $query);
14
            }
15
        }
16
17
        return $restrictions;
18
    }
19
20
    private static function formatExpression($preference, $expression)
21
    {
22
        if (count($expression) > 1 && is_array(current($expression))) {
23
            return array_map(function ($subExpression) use ($preference) {
24
                return self::formatExpression($preference, $subExpression)[0];
25
            }, $expression);
26
        }
27
28
        return self::formatFieldURI($preference, $expression);
29
    }
30
31
    private static function formatFieldURI($preference, $expression)
32
    {
33
        $formattedRestrictionType = [];
34
35
        foreach ($expression as $key => $value) {
36
            if ($value === false) {
37
                $value = 'false';
38
            }
39
            $formattedRestrictionType[] = array(
40
                'FieldURI' => array('FieldURI' => API\FieldURIManager::getFieldUriByName($key, $preference)),
41
                'FieldURIOrConstant' => array('Constant' => array('Value' => (string)$value))
42
            );
43
        }
44
45
        return $formattedRestrictionType;
46
    }
47
}
48