RestrictionFormatter::formatFieldURI()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

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