|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the prooph/php-ddd-cargo-sample package. |
|
4
|
|
|
* (c) Alexander Miertsch <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
declare(strict_types = 1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Codeliner\CargoBackend\Model\Cargo; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Itinerary |
|
17
|
|
|
* |
|
18
|
|
|
* The Itinerary is a ValueObject that describes a route of a cargo. |
|
19
|
|
|
* It is composed of one or more Legs. |
|
20
|
|
|
* Each Leg describes a part of the entire route, f.e. from one port to another. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Alexander Miertsch <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class Itinerary |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Leg[] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $legs; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Leg[] $legs |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(array $legs) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->legs = $legs; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return Leg[] Immutable list of Legs |
|
41
|
|
|
*/ |
|
42
|
|
|
public function legs() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->legs; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Itinerary $other |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public function sameValueAs(Itinerary $other): bool |
|
52
|
|
|
{ |
|
53
|
|
|
//We use doctrine's ArrayCollection only to ease comparison |
|
54
|
|
|
//If Legs would be stored in an ArrayCollection hole the time |
|
55
|
|
|
//Itinerary itself would not be immutable, |
|
56
|
|
|
//cause a client could call $itinerary->legs()->add($anotherLeg); |
|
57
|
|
|
//Keeping ValueObjects immutable is a rule of thumb |
|
58
|
|
|
$myLegs = new ArrayCollection($this->legs()); |
|
59
|
|
|
$otherLegs = new ArrayCollection($other->legs()); |
|
60
|
|
|
|
|
61
|
|
|
if ($myLegs->count() !== $otherLegs->count()) { |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $myLegs->forAll(function($index, Leg $leg) use ($otherLegs) { |
|
66
|
|
|
return $otherLegs->exists(function($otherIndex, Leg $otherLeg) use ($leg) { |
|
67
|
|
|
return $otherLeg->sameValueAs($leg); |
|
68
|
|
|
}); |
|
69
|
|
|
}); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* surrogate key, required by doctrine |
|
74
|
|
|
*/ |
|
75
|
|
|
private $id; |
|
76
|
|
|
} |
|
77
|
|
|
|