/** * @param Itinerary $other * @return bool */ public function sameValueAs(Itinerary $other) : bool { //We use doctrine's ArrayCollection only to ease comparison //If Legs would be stored in an ArrayCollection hole the time //Itinerary itself would not be immutable, //cause a client could call $itinerary->legs()->add($anotherLeg); //Keeping ValueObjects immutable is a rule of thumb $myLegs = new ArrayCollection($this->legs()); $otherLegs = new ArrayCollection($other->legs()); if ($myLegs->count() !== $otherLegs->count()) { return false; } return $myLegs->forAll(function ($index, Leg $leg) use($otherLegs) { return $otherLegs->exists(function ($otherIndex, Leg $otherLeg) use($leg) { return $otherLeg->sameValueAs($leg); }); }); }
/** * @test */ public function it_is_not_same_value_as_itinerary_with_other_list_of_legs() { $legs = [LegFixture::get(LegFixture::HONGKONG_NEWYORK), LegFixture::get(LegFixture::NEWYORK_HAMBURG)]; $itinerary = new Itinerary($legs); $otherLegs = [LegFixture::get(LegFixture::HONGKONG_HAMBURG), LegFixture::get(LegFixture::HAMBURG_ROTTERDAM)]; $otherItinerary = new Itinerary($otherLegs); $this->assertFalse($itinerary->sameValueAs($otherItinerary)); }
/** * @param Itinerary $anItinerary * @return RouteCandidateDto */ public function toDto(Itinerary $anItinerary) { $legs = array(); foreach ($anItinerary->legs() as $leg) { $legDto = new LegDto(); $legDto->setLoadLocation($leg->loadLocation()); $legDto->setUnloadLocation($leg->unloadLocation()); $legDto->setLoadTime($leg->loadTime()->format(\DateTime::ATOM)); $legDto->setUnloadTime($leg->unloadTime()->format(\DateTime::ATOM)); $legs[] = $legDto; } $routeCandidate = new RouteCandidateDto(); $routeCandidate->setLegs($legs); return $routeCandidate; }