|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Messaging; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\Common\ArrayList; |
|
28
|
|
|
use Brickoo\Component\Common\Assert; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Implements a generic message which can be used or extended by any component. |
|
32
|
|
|
* @author Celestino Diaz <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
class GenericMessage implements Message { |
|
35
|
|
|
|
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
private $name; |
|
38
|
|
|
|
|
39
|
|
|
/** @var array */ |
|
40
|
|
|
private $params; |
|
41
|
|
|
|
|
42
|
|
|
/** @var object */ |
|
43
|
|
|
private $sender; |
|
44
|
|
|
|
|
45
|
|
|
/** @var boolean */ |
|
46
|
|
|
private $stopped; |
|
47
|
|
|
|
|
48
|
|
|
/** @var \Brickoo\Component\Common\ArrayList */ |
|
49
|
|
|
private $responseList; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $name the message name |
|
53
|
|
|
* @param null|object $sender the sender object |
|
54
|
|
|
* @param array $parameters the parameters to add to the message |
|
55
|
|
|
* @throws \InvalidArgumentException |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function __construct($name, $sender = null, array $parameters = []) { |
|
58
|
1 |
|
Assert::isString($name); |
|
59
|
|
|
|
|
60
|
1 |
|
if ($sender !== null) { |
|
61
|
1 |
|
Assert::isObject($sender); |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
$this->name = $name; |
|
65
|
1 |
|
$this->sender = $sender; |
|
66
|
1 |
|
$this->params = $parameters; |
|
67
|
1 |
|
$this->stopped = false; |
|
68
|
1 |
|
$this->responseList = new ArrayList(); |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** {@inheritDoc} */ |
|
72
|
1 |
|
public function getSender() { |
|
73
|
1 |
|
return $this->sender; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** {@inheritDoc} */ |
|
77
|
1 |
|
public function stop() { |
|
78
|
1 |
|
$this->stopped = true; |
|
79
|
1 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** {@inheritDoc} */ |
|
83
|
1 |
|
public function isStopped() { |
|
84
|
1 |
|
return ($this->stopped === true); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** {@inheritDoc} */ |
|
88
|
1 |
|
public function getName() { |
|
89
|
1 |
|
return $this->name; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** {@inheritDoc} */ |
|
93
|
1 |
|
public function getParams() { |
|
94
|
1 |
|
return $this->params; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** {@inheritDoc} */ |
|
98
|
1 |
|
public function setParam($identifier, $value) { |
|
99
|
1 |
|
Assert::isString($identifier); |
|
100
|
1 |
|
$this->params[$identifier] = $value; |
|
101
|
1 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** {@inheritDoc} */ |
|
105
|
2 |
|
public function getParam($identifier, $defaultValue = null) { |
|
106
|
2 |
|
Assert::isString($identifier); |
|
107
|
|
|
|
|
108
|
1 |
|
if (!$this->hasParam($identifier)) { |
|
109
|
1 |
|
return $defaultValue; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
return $this->params[$identifier]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** {@inheritDoc} */ |
|
116
|
2 |
|
public function hasParam($identifier) { |
|
117
|
2 |
|
Assert::isString($identifier); |
|
118
|
1 |
|
return isset($this->params[$identifier]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** {@inheritDoc} */ |
|
122
|
1 |
|
public function hasParams() { |
|
123
|
1 |
|
$containsAllParameters = true; |
|
124
|
1 |
|
if (($arguments = func_get_args())) { |
|
125
|
1 |
|
foreach ($arguments as $argument) { |
|
126
|
1 |
|
if (!$this->hasParam($argument)) { |
|
127
|
1 |
|
$containsAllParameters = false; |
|
128
|
1 |
|
break; |
|
129
|
|
|
} |
|
130
|
1 |
|
} |
|
131
|
1 |
|
} |
|
132
|
1 |
|
return $containsAllParameters; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** {@inheritDoc} */ |
|
136
|
1 |
|
public function getResponseList() { |
|
137
|
1 |
|
return $this->responseList; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** {@inheritDoc} */ |
|
141
|
1 |
|
public function setResponseList(ArrayList $responseList) { |
|
142
|
1 |
|
$this->responseList = $responseList; |
|
143
|
1 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|