This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
/
ResolvedComponentData.php
123 lines (108 loc) · 2.77 KB
/
ResolvedComponentData.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Spy\Timeline\ResolveComponent\ValueObject;
use Spy\Timeline\Exception\ResolveComponentDataException;
/**
* This value object guards that the resolved model and identifier are valid.
*/
class ResolvedComponentData
{
/**
* The resolved model string.
*
* @var string
*/
private $model;
/**
* The resolved identifier.
*
* @var mixed
*/
private $identifier;
/**
* The resolved data.
*
* @var null|object
*/
private $data;
/**
* @param string $model The resolved model
* @param mixed $identifier The resolved identifier
* @param object|null $data The resolved data
*/
public function __construct($model, $identifier, $data = null)
{
$this->guardValidModel($model);
$this->guardValidIdentifier($identifier);
$this->model = $model;
$this->identifier = $identifier;
$this->data = $data;
}
/**
* Gets the resolved model.
*
* @return string
*/
public function getModel()
{
return $this->model;
}
/**
* Gets the resolved data.
*
* @return null|object
*/
public function getData()
{
return $this->data;
}
/**
* Gets the resolved identifier.
*
* Because of serializing problems we always return scalars as strings
*
* @return array|string
*/
public function getIdentifier()
{
if (is_scalar($this->identifier)) {
return (string) $this->identifier;
}
return $this->identifier;
}
/**
* Guard valid model.
*
* The model can not be empty and has to be a string.
*
* @param string $model
*
* @throws ResolveComponentDataException When the model is not a string.
*/
private function guardValidModel($model)
{
if (empty($model)) {
throw new ResolveComponentDataException('The resolved model can not be empty');
}
if (!is_string($model)) {
throw new ResolveComponentDataException('The resolved model has to be a string');
}
}
/**
* Guard valid identifier.
*
* The identifier can not be empty (but can be zero) and has to be a scalar or array.
*
* @param string|array $identifier
*
* @throws ResolveComponentDataException
*/
private function guardValidIdentifier($identifier)
{
if (null === $identifier || '' === $identifier) {
throw new ResolveComponentDataException('No resolved identifier given');
}
if (!is_scalar($identifier) && !is_array($identifier)) {
throw new ResolveComponentDataException('Identifier has to be a scalar or an array');
}
}
}