1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2013 Janos Szurovecz |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
7
|
|
|
* the Software without restriction, including without limitation the rights to |
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
10
|
|
|
* so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
13
|
|
|
* copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO message SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21
|
|
|
* SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace predaddy\messagehandling; |
25
|
|
|
|
26
|
|
|
use DateTime; |
27
|
|
|
use precore\lang\NullPointerException; |
28
|
|
|
use precore\lang\Object; |
29
|
|
|
use precore\lang\ObjectInterface; |
30
|
|
|
use precore\util\Objects; |
31
|
|
|
use precore\util\Preconditions; |
32
|
|
|
use precore\util\UUID; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Base class for all types of messages. Contains the message identifier and timestamp. |
36
|
|
|
* |
37
|
|
|
* @author Janos Szurovecz <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
abstract class AbstractMessage extends Object implements Message |
40
|
|
|
{ |
41
|
|
|
protected $id; |
42
|
|
|
protected $created; |
43
|
|
|
|
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
$this->created = new DateTime(); |
47
|
|
|
$this->id = UUID::randomUUID()->toString(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
* @throws NullPointerException if ID is not initialized |
53
|
|
|
*/ |
54
|
|
|
public function identifier() |
55
|
|
|
{ |
56
|
|
|
return Preconditions::checkNotNull($this->id, 'ID is not initialized'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return DateTime |
61
|
|
|
* @throws NullPointerException if created field is not initialized |
62
|
|
|
*/ |
63
|
|
|
public function created() |
64
|
|
|
{ |
65
|
|
|
return clone Preconditions::checkNotNull($this->created, 'created field is not initialized'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
final public function equals(ObjectInterface $object = null) |
69
|
|
|
{ |
70
|
|
|
if ($object === $this) { |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
/* @var $object AbstractMessage */ |
74
|
|
|
return $object !== null |
75
|
|
|
&& $this->getClassName() === $object->getClassName() |
76
|
|
|
&& $this->id === $object->id; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \precore\util\ToStringHelper |
81
|
|
|
*/ |
82
|
|
|
protected function toStringHelper() |
83
|
|
|
{ |
84
|
|
|
return Objects::toStringHelper($this) |
85
|
|
|
->add('id', $this->identifier()) |
86
|
|
|
->add('created', $this->created()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function toString() |
90
|
|
|
{ |
91
|
|
|
return $this->toStringHelper()->toString(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|