-
Notifications
You must be signed in to change notification settings - Fork 11
/
ErrorResponseDictionary.php
70 lines (61 loc) · 1.95 KB
/
ErrorResponseDictionary.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace MABI;
class ErrorResponseDictionary {
/**
* @var ErrorResponse[]
*/
protected $errorResponses = array();
/**
* Should be in the format (all required):
* static $SAMPLE_ERROR_KEY = array(
* 'message' => 'sample error message',
* 'httpcode' => '401',
* 'code' => 1); // optional
*/
function __construct() {
$rClass = new \ReflectionClass(get_called_class());
$rProperties = $rClass->getProperties(\ReflectionProperty::IS_STATIC);
$defaultProps = $rClass->getDefaultProperties();
foreach ($rProperties as $rProperty) {
$ignoreAnnotation = ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'ignore');
if (!empty($ignoreAnnotation) || !is_array($defaultProps[$rProperty->getName()]) ||
empty($defaultProps[$rProperty->getName()])
) {
continue;
}
$propertyKeys = array_keys($defaultProps[$rProperty->getName()]);
$key = $propertyKeys[0];
$errorResponseArray = $defaultProps[$rProperty->getName()][$key];
if (empty($errorResponseArray['message']) || empty($errorResponseArray['httpcode'])) {
continue;
}
$this->errorResponses[$key] = ErrorResponse::FromArray($errorResponseArray);
}
}
/**
* Creates or overrides error responses with an initialization array in mass
*
* @param $initArray
*/
public function overrideErrorResponses(ErrorResponseDictionary $overridingDictionary) {
foreach ($overridingDictionary->errorResponses as $key => $errorResponse) {
$this->errorResponses[$key] = $errorResponse;
}
}
public function overrideErrorResponse($key, ErrorResponse $errorResponse) {
$this->errorResponses[$key] = $errorResponse;
}
/**
* todo: docs
*
* @param $key
*
* @return ErrorResponse|null
*/
public function getErrorResponse($key) {
if (empty($this->errorResponses[$key])) {
return NULL;
}
return $this->errorResponses[$key];
}
}