|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace AmmitPhp\Ammit\UI\Resolver\Exception; |
|
5
|
|
|
|
|
6
|
|
|
use AmmitPhp\Ammit\UI\Resolver\NormalizableInterface; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractNormalizableCommandResolverException extends \InvalidArgumentException implements NormalizableInterface |
|
9
|
|
|
{ |
|
10
|
|
|
const SOURCE_RAW = null; |
|
11
|
|
|
const SOURCE_ATTRIBUTE = 'pointer'; |
|
12
|
|
|
const SOURCE_PARAMETER = 'parameter'; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
private $propertyPath; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string|null */ |
|
18
|
|
|
private $source; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @inheritDoc |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(string $message, string $propertyPath = null, string $source = null) |
|
24
|
|
|
{ |
|
25
|
1 |
|
parent::__construct($message, 0); |
|
26
|
|
|
|
|
27
|
1 |
|
$this->propertyPath = self::cleanPropertyPath($propertyPath); |
|
28
|
1 |
|
$this->source = $source; |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* From raw value directly injected |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function fromRaw(string $message, string $propertyPath = null): AbstractNormalizableCommandResolverException |
|
35
|
|
|
{ |
|
36
|
|
|
return new static( |
|
37
|
|
|
$message, |
|
38
|
|
|
self::cleanPropertyPath($propertyPath), |
|
39
|
|
|
self::SOURCE_RAW |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* From query string parameter ($_GET) |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function fromParameter(string $message, string $propertyPath = null): AbstractNormalizableCommandResolverException |
|
47
|
|
|
{ |
|
48
|
|
|
return new static( |
|
49
|
|
|
$message, |
|
50
|
|
|
self::cleanPropertyPath($propertyPath), |
|
51
|
|
|
self::SOURCE_PARAMETER |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* From attribute parameter ($_POST) |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function fromAttribute(string $message, string $propertyPath = null): AbstractNormalizableCommandResolverException |
|
59
|
|
|
{ |
|
60
|
|
|
return new static($message, |
|
61
|
|
|
self::cleanPropertyPath($propertyPath), |
|
62
|
|
|
self::SOURCE_ATTRIBUTE |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getPropertyPath(): string |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->propertyPath; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return null|string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getSource() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->source; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
* Normalize the Exception into a ready to be JSON encoded array |
|
82
|
|
|
* Example for Attribute $_POST: |
|
83
|
|
|
* { |
|
84
|
|
|
* "status": "406", |
|
85
|
|
|
* "source": { "pointer": "/data/attributes/firstName" }, |
|
86
|
|
|
* "title": "Invalid Attribute", |
|
87
|
|
|
* "detail": "Array does not contain an element with key firstName" |
|
88
|
|
|
* } |
|
89
|
|
|
* |
|
90
|
|
|
* Example for parameter $_GET |
|
91
|
|
|
* { |
|
92
|
|
|
* "status": "406", |
|
93
|
|
|
* "source": { "parameter": "firstName" }, |
|
94
|
|
|
* "title": "Invalid Query Parameter", |
|
95
|
|
|
* "detail": "Array does not contain an element with key firstName" |
|
96
|
|
|
* } |
|
97
|
|
|
*/ |
|
98
|
|
|
public function normalize(): array |
|
99
|
|
|
{ |
|
100
|
|
|
return [ |
|
101
|
1 |
|
'status' => 406, |
|
102
|
1 |
|
'source' => $this->createSourceNode( |
|
103
|
1 |
|
$this->propertyPath, |
|
104
|
1 |
|
$this->source |
|
105
|
|
|
), |
|
106
|
1 |
|
'title' => $this->createTitleNode($this->source), |
|
107
|
1 |
|
'detail' => $this->getMessage(), |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private function createSourceNode(string $propertyPath, string $source = null): array |
|
112
|
|
|
{ |
|
113
|
1 |
|
if (self::SOURCE_ATTRIBUTE === $source) { |
|
114
|
|
|
return [ |
|
115
|
1 |
|
'pointer' => '/data/attributes/' . $propertyPath |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return [ |
|
120
|
|
|
'parameter' => $propertyPath |
|
121
|
|
|
]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
private function createTitleNode(string $source = null): string |
|
125
|
|
|
{ |
|
126
|
1 |
|
if (self::SOURCE_PARAMETER === $source) { |
|
127
|
|
|
return 'Invalid Query Parameter'; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
if (self::SOURCE_RAW === $source) { |
|
131
|
|
|
return 'Invalid Parameter'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
return 'Invalid Attribute'; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
private static function cleanPropertyPath(string $propertyPath = null): string |
|
138
|
|
|
{ |
|
139
|
1 |
|
if (null === $propertyPath) { |
|
140
|
1 |
|
return $propertyPath = ''; |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
return $propertyPath; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.