Update Person::getLastAddress() based on feedback.

This commit is contained in:
Pol Dellaiera
2021-03-22 12:32:29 +01:00
parent c205bbddd3
commit 03601b9707
3 changed files with 165 additions and 179 deletions

View File

@@ -28,7 +28,10 @@ use Chill\PersonBundle\Entity\MaritalStatus;
use Doctrine\Common\Collections\ArrayCollection;
use Chill\MainBundle\Entity\HasCenterInterface;
use Chill\MainBundle\Entity\Address;
use DateTime;
use DateTimeImmutable;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\VarDumper\VarDumper;
/**
* Person
@@ -42,7 +45,7 @@ class Person implements HasCenterInterface {
/** @var string The person's last name */
private $lastName;
/**
*
* @var \Doctrine\Common\Collections\Collection
@@ -119,20 +122,20 @@ class Person implements HasCenterInterface {
* @var \Doctrine\Common\Collections\Collection
*/
private $addresses;
/**
* @var string
*/
private $fullnameCanonical;
public function __construct(\DateTime $opening = null) {
public function __construct(\DateTimeImmutable $opening = null) {
$this->accompanyingPeriods = new ArrayCollection();
$this->spokenLanguages = new ArrayCollection();
$this->addresses = new ArrayCollection();
$this->altNames = new ArrayCollection();
if ($opening === null) {
$opening = new \DateTime();
$opening = new \DateTimeImmutable();
}
$this->open(new AccompanyingPeriod($opening));
@@ -331,7 +334,7 @@ class Person implements HasCenterInterface {
{
return $this->lastName;
}
public function getAltNames(): \Doctrine\Common\Collections\Collection
{
return $this->altNames;
@@ -340,7 +343,7 @@ class Person implements HasCenterInterface {
public function setAltNames(\Doctrine\Common\Collections\Collection $altNames)
{
$this->altNames = $altNames;
return $this;
}
@@ -350,20 +353,20 @@ class Person implements HasCenterInterface {
$this->altNames->add($altName);
$altName->setPerson($this);
}
return $this;
}
public function removeAltName(PersonAltName $altName)
public function removeAltName(PersonAltName $altName)
{
if ($this->altNames->contains($altName)) {
$altName->setPerson(null);
$this->altNames->removeElement($altName);
}
return $this;
}
/**
* Set birthdate
*
@@ -745,27 +748,28 @@ class Person implements HasCenterInterface {
* By default, the addresses are ordered by date, descending (the most
* recent first)
*
* @return \Chill\MainBundle\Entity\Address[]
* @return ArrayCollection
*/
public function getAddresses()
public function getAddresses(): ArrayCollection
{
return $this->addresses;
}
public function getLastAddress(\DateTime $date = null)
public function getLastAddress(?DateTimeImmutable $from = null): ?Address
{
if ($date === null) {
$date = new \DateTime('now');
}
$from ??= new DateTimeImmutable('now');
$addresses = $this->getAddresses();
$addressesIterator = $this->getAddresses()
->filter(static fn (Address $address): bool => $address->getValidFrom() <= $from)
->getIterator();
if ($addresses == null) {
$addressesIterator->uasort(
static fn (Address $left, Address $right): int => $right->getValidFrom() <=> $left->getValidFrom()
);
return null;
}
return $addresses->first();
return [] === ($addresses = iterator_to_array($addressesIterator)) ?
null :
current($addresses);
}
/**