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

RestrictionFormatter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 12 3
A formatExpression() 0 10 3
A formatFieldURI() 0 16 3
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