mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-06 23:04:58 +00:00
Update Person::getLastAddress() based on feedback.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user