|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TonicForHealth\PagerDutyClient\RepresentProcessor; |
|
4
|
|
|
|
|
5
|
|
|
use TonicForHealth\PagerDutyClient\Entity\PagerDutyEntityInterface; |
|
6
|
|
|
use TonicForHealth\PagerDutyClient\RepresentProcessor\Exception\RepresentProcessorException; |
|
7
|
|
|
use TonicForHealth\PagerDutyClient\RepresentProcessor\Representation\RepresentationInterface; |
|
8
|
|
|
use TonicForHealth\PagerDutyClient\RepresentProcessor\Representation\RepresentationJSONInterface; |
|
9
|
|
|
use TonicForHealth\PagerDutyClient\RepresentProcessor\Representation\RepresentationRESTInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class RepresentProcessor. |
|
13
|
|
|
*/ |
|
14
|
|
|
class RepresentProcessor implements RepresentProcessorInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $representation = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param RepresentationInterface $representation |
|
23
|
|
|
* @param string $className |
|
24
|
|
|
* @param string $type |
|
25
|
|
|
*/ |
|
26
|
4 |
|
public function addRepresentation( |
|
27
|
|
|
RepresentationInterface $representation, |
|
28
|
|
|
$className = null, |
|
29
|
|
|
$type = self::REPRESENT_TYPE_JSON |
|
30
|
|
|
) { |
|
31
|
4 |
|
if (null === $className) { |
|
32
|
4 |
|
$className = $representation::getEntityClassName(); |
|
33
|
|
|
} |
|
34
|
4 |
|
$this->representation[$type][$className] = $representation; |
|
35
|
4 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param PagerDutyEntityInterface $pagerDutyEntity |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
* |
|
42
|
|
|
* @throws RepresentProcessorException |
|
43
|
|
|
*/ |
|
44
|
4 |
|
public function representJson(PagerDutyEntityInterface $pagerDutyEntity) |
|
45
|
|
|
{ |
|
46
|
|
|
/** @var RepresentationJSONInterface $representation */ |
|
47
|
4 |
|
$representation = $this->getRepresentation(get_class($pagerDutyEntity), self::REPRESENT_TYPE_JSON); |
|
48
|
|
|
|
|
49
|
4 |
|
return $representation->persistRepresentJSON($pagerDutyEntity); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param PagerDutyEntityInterface $pagerDutyEntity |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
* |
|
57
|
|
|
* @throws RepresentProcessorException |
|
58
|
|
|
*/ |
|
59
|
4 |
|
public function getRESTResourcePath(PagerDutyEntityInterface $pagerDutyEntity) |
|
60
|
|
|
{ |
|
61
|
|
|
/** @var RepresentationRESTInterface $representation */ |
|
62
|
4 |
|
$representation = $this->getRepresentation(get_class($pagerDutyEntity), self::REPRESENT_TYPE_JSON); |
|
63
|
|
|
|
|
64
|
4 |
|
return $representation->getRESTResourcePath($pagerDutyEntity); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $className |
|
69
|
|
|
* @param string $type |
|
70
|
|
|
* |
|
71
|
|
|
* @return RepresentationInterface |
|
72
|
|
|
* |
|
73
|
|
|
* @throws RepresentProcessorException |
|
74
|
|
|
*/ |
|
75
|
4 |
|
protected function getRepresentation($className, $type) |
|
76
|
|
|
{ |
|
77
|
4 |
|
if (!isset($this->representation[$type][$className])) { |
|
78
|
|
|
throw RepresentProcessorException::representationDoesNotExist($className, $type); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
return $this->representation[$type][$className]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: