Completed
Push — master ( 9e9528...25f4f6 )
by Gareth
04:01
created

FieldURIManager::getDictionaryURIFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace garethp\ews\API;
4
5
use garethp\ews\API\Enumeration\DictionaryURIType;
6
use garethp\ews\API\Enumeration\UnindexedFieldURIType;
7
use garethp\ews\API\Exception\ExchangeException;
8
9
class FieldURIManager
10
{
11
    protected static $unIndexedFieldURIs = [];
12
    protected static $dictionaryFieldURIs = [];
13
14
    protected static function setupFieldUris()
15
    {
16
        if (!empty(self::$dictionaryFieldURIs) && !empty(self::$dictionaryFieldURIs)) {
17
            return;
18
        }
19
20
        self::$unIndexedFieldURIs = self::getFieldUrisFromClass(UnindexedFieldURIType::class);
21
        self::$dictionaryFieldURIs = self::getFieldUrisFromClass(DictionaryURIType::class);
22
    }
23
24
    public static function getDictionaryURIFields()
25
    {
26
        self::setupFieldUris();
27
        return self::$dictionaryFieldURIs;
28
    }
29
30
    protected static function getFieldUrisFromClass($className)
31
    {
32
        //So, since we have to pass in URI's of everything we update, we need to fetch them
33
        $reflection = new \ReflectionClass($className);
34
        $constants = $reflection->getConstants();
35
        $constantsFound = array();
36
37
        //Loop through all URI's to list them in an array
38
        foreach ($constants as $constant) {
39
            $exploded = explode(":", strtolower($constant));
40
            if (count($exploded) == 1) {
41
                $exploded = ['item', $exploded[0]];
42
            }
43
44
            //It starts in order ['contacts', 'physicaladdress', 'city], we want it in
45
            //['physicaladdress', 'contacts', 'city]
46
            $temp = $exploded[0];
47
            $exploded[0] = $exploded[1];
48
            $exploded[1] = $temp;
49
50
            $depth = count($exploded) - 1;
51
            $current = &$constantsFound;
52
53
            //Use the exploded array as a way to create a multidimensional array. For example,
54
            //['contacts', 'physicaladdress', 'city'] becomes
55
            //['contacts' => ['physicaladdress' => ['city' => 'contacts:PhysicalAddress:City']]]
56
            foreach ($exploded as $count => $key) {
57
                if ($count < $depth && !isset($current[$key])) {
58
                    $current[$key] = array();
59
                }
60
61
                if ($count < $depth) {
62
                    $current = &$current[$key];
63
                    continue;
64
                }
65
66
                $current[$key] = $constant;
67
            }
68
        }
69
70
        return $constantsFound;
71
    }
72
73
    public static function getFieldUriByName($fieldName, $preference = 'item')
74
    {
75
        self::setupFieldUris();
76
        $fieldName = strtolower($fieldName);
77
        $preference = strtolower($preference);
78
79
        if (!isset(self::$unIndexedFieldURIs[$fieldName])) {
80
            return false;
81
        }
82
83
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
84
            $preference = 'item';
85
        }
86
87
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
88
            throw new ExchangeException("Could not find uri $preference:$fieldName");
89
        }
90
91
        return self::$unIndexedFieldURIs[$fieldName][$preference];
92
    }
93
94
    public static function getIndexedFieldUriByName($fieldName, $preference = 'item', $entryKey = false)
95
    {
96
        self::setupFieldUris();
97
        $fieldName = strtolower($fieldName);
98
        $preference = strtolower($preference);
99
        $entryKey = strtolower($entryKey);
100
101
        if (!isset(self::$dictionaryFieldURIs[$fieldName])) {
102
            return false;
103
        }
104
105
        if (!isset(self::$dictionaryFieldURIs[$fieldName][$preference])) {
106
            throw new ExchangeException("Could not find uri $preference:$fieldName");
107
        }
108
109
        $fieldUri = self::$dictionaryFieldURIs[$fieldName][$preference];
110
        if (!is_array($fieldUri)) {
111
            return $fieldUri;
112
        }
113
114
        if (!$entryKey || !isset($fieldUri[$entryKey])) {
115
            throw new ExchangeException("Could not find FieldURI");
116
        }
117
118
        return $fieldUri[$entryKey];
119
    }
120
}
121