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 | 8 | protected static function setupFieldUris() |
|||
15 | { |
||||
16 | 8 | if (!empty(self::$dictionaryFieldURIs) && !empty(self::$dictionaryFieldURIs)) { |
|||
17 | 7 | return; |
|||
18 | } |
||||
19 | |||||
20 | 1 | self::$unIndexedFieldURIs = self::getFieldUrisFromClass(UnindexedFieldURIType::class); |
|||
21 | 1 | self::$dictionaryFieldURIs = self::getFieldUrisFromClass(DictionaryURIType::class); |
|||
22 | } |
||||
23 | |||||
24 | 1 | public static function getDictionaryURIFields() |
|||
25 | { |
||||
26 | 1 | self::setupFieldUris(); |
|||
27 | 1 | return self::$dictionaryFieldURIs; |
|||
28 | } |
||||
29 | |||||
30 | 2 | 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 | 2 | $reflection = new \ReflectionClass($className); |
|||
34 | 2 | $constants = $reflection->getConstants(); |
|||
35 | 2 | $constantsFound = array(); |
|||
36 | |||||
37 | //Loop through all URI's to list them in an array |
||||
38 | 2 | foreach ($constants as $constant) { |
|||
39 | 2 | $exploded = explode(":", strtolower($constant)); |
|||
40 | 2 | if (count($exploded) == 1) { |
|||
41 | 2 | $exploded = ['item', $exploded[0]]; |
|||
42 | } |
||||
43 | |||||
44 | //It starts in order ['contacts', 'physicaladdress', 'city], we want it in |
||||
45 | //['physicaladdress', 'contacts', 'city] |
||||
46 | 2 | $temp = $exploded[0]; |
|||
47 | 2 | $exploded[0] = $exploded[1]; |
|||
48 | 2 | $exploded[1] = $temp; |
|||
49 | |||||
50 | 2 | $depth = count($exploded) - 1; |
|||
51 | 2 | $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 | 2 | foreach ($exploded as $count => $key) { |
|||
57 | 2 | if ($count < $depth && !isset($current[$key])) { |
|||
58 | 2 | $current[$key] = array(); |
|||
59 | } |
||||
60 | |||||
61 | 2 | if ($count < $depth) { |
|||
62 | 2 | $current = &$current[$key]; |
|||
63 | 2 | continue; |
|||
64 | } |
||||
65 | |||||
66 | 2 | $current[$key] = $constant; |
|||
67 | } |
||||
68 | } |
||||
69 | |||||
70 | 2 | return $constantsFound; |
|||
71 | } |
||||
72 | |||||
73 | 8 | public static function getFieldUriByName($fieldName, $preference = 'item') |
|||
74 | { |
||||
75 | 8 | self::setupFieldUris(); |
|||
76 | 8 | $fieldName = strtolower($fieldName); |
|||
77 | 8 | $preference = strtolower($preference); |
|||
78 | |||||
79 | 8 | if (!isset(self::$unIndexedFieldURIs[$fieldName])) { |
|||
80 | return false; |
||||
81 | } |
||||
82 | |||||
83 | 8 | if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) { |
|||
84 | 4 | $preference = 'item'; |
|||
85 | } |
||||
86 | |||||
87 | 8 | if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) { |
|||
88 | throw new ExchangeException("Could not find uri $preference:$fieldName"); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
89 | } |
||||
90 | |||||
91 | 8 | return self::$unIndexedFieldURIs[$fieldName][$preference]; |
|||
92 | } |
||||
93 | |||||
94 | 1 | public static function getIndexedFieldUriByName($fieldName, $preference = 'item', $entryKey = false) |
|||
95 | { |
||||
96 | 1 | self::setupFieldUris(); |
|||
97 | 1 | $fieldName = strtolower($fieldName); |
|||
98 | 1 | $preference = strtolower($preference); |
|||
99 | 1 | $entryKey = strtolower($entryKey); |
|||
100 | |||||
101 | 1 | if (!isset(self::$dictionaryFieldURIs[$fieldName])) { |
|||
102 | return false; |
||||
103 | } |
||||
104 | |||||
105 | 1 | if (!isset(self::$dictionaryFieldURIs[$fieldName][$preference])) { |
|||
106 | throw new ExchangeException("Could not find uri $preference:$fieldName"); |
||||
0 ignored issues
–
show
'Could not find uri '.$preference.':'.$fieldName of type string is incompatible with the type garethp\ews\API\Message\ResponseMessageType expected by parameter $response of garethp\ews\API\Exceptio...xception::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
107 | } |
||||
108 | |||||
109 | 1 | $fieldUri = self::$dictionaryFieldURIs[$fieldName][$preference]; |
|||
110 | 1 | if (!is_array($fieldUri)) { |
|||
111 | 1 | return $fieldUri; |
|||
112 | } |
||||
113 | |||||
114 | 1 | if (!$entryKey || !isset($fieldUri[$entryKey])) { |
|||
115 | throw new ExchangeException("Could not find FieldURI"); |
||||
116 | } |
||||
117 | |||||
118 | 1 | return $fieldUri[$entryKey]; |
|||
119 | } |
||||
120 | } |
||||
121 |