mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
Merge remote-tracking branch 'tmp/fix-person-last-address' into drupol-fix-person-addresses
This commit is contained in:
@@ -22,15 +22,17 @@ namespace Chill\PersonBundle\Entity;
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use ArrayIterator;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Entity\Country;
|
||||
use Chill\PersonBundle\Entity\MaritalStatus;
|
||||
use Chill\MainBundle\Entity\HasCenterInterface;
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use DateTime;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
/**
|
||||
@@ -1008,32 +1010,28 @@ class Person implements HasCenterInterface
|
||||
/**
|
||||
* By default, the addresses are ordered by date, descending (the most
|
||||
* recent first)
|
||||
*
|
||||
* @return \Chill\MainBundle\Entity\Address[]
|
||||
*/
|
||||
public function getAddresses()
|
||||
public function getAddresses(): ArrayCollection
|
||||
{
|
||||
return $this->addresses;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime|null $date
|
||||
* @return null
|
||||
*/
|
||||
public function getLastAddress(\DateTime $date = null)
|
||||
public function getLastAddress(DateTime $from = null)
|
||||
{
|
||||
if ($date === null) {
|
||||
$date = new \DateTime('now');
|
||||
}
|
||||
$from ??= new DateTime('now');
|
||||
|
||||
$addresses = $this->getAddresses();
|
||||
/** @var ArrayIterator $addressesIterator */
|
||||
$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