Do not use DateTimeImmutable.

This commit is contained in:
Pol Dellaiera
2021-04-02 10:44:21 +02:00
parent 777fb25860
commit 48e2d2ceab
3 changed files with 64 additions and 50 deletions

View File

@@ -25,7 +25,6 @@ namespace Chill\PersonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
/**
* AccompanyingPeriod
@@ -35,9 +34,11 @@ class AccompanyingPeriod
/** @var integer */
private $id;
private DateTimeImmutable $openingDate;
/** @var \DateTime */
private $openingDate;
private ?DateTimeImmutable $closingDate = null;
/** @var \DateTime */
private $closingDate;
/** @var string */
private $remark = '';
@@ -56,11 +57,11 @@ class AccompanyingPeriod
private $user;
/**
*
* @param \DateTimeImmutable $dateOpening
*
* @param \DateTime $dateOpening
* @uses AccompanyingPeriod::setClosingDate()
*/
public function __construct(\DateTimeImmutable $dateOpening) {
public function __construct(\DateTime $dateOpening) {
$this->setOpeningDate($dateOpening);
}
@@ -74,14 +75,25 @@ class AccompanyingPeriod
return $this->id;
}
public function setOpeningDate(DateTimeImmutable $openingDate): self
/**
* Set openingDate
*
* @param \DateTime $dateOpening
* @return AccompanyingPeriod
*/
public function setOpeningDate($openingDate)
{
$this->openingDate = $openingDate;
return $this;
}
public function getOpeningDate(): DateTimeImmutable
/**
* Get openingDate
*
* @return \DateTime
*/
public function getOpeningDate()
{
return $this->openingDate;
}
@@ -91,14 +103,19 @@ class AccompanyingPeriod
*
* For closing a Person file, you should use Person::setClosed instead.
*/
public function setClosingDate(DateTimeImmutable $closingDate): self
public function setClosingDate($closingDate)
{
$this->closingDate = $closingDate;
return $this;
}
public function getClosingDate(): ?DateTimeImmutable
/**
* Get closingDate
*
* @return \DateTime
*/
public function getClosingDate()
{
return $this->closingDate;
}
@@ -109,7 +126,7 @@ class AccompanyingPeriod
*/
public function isOpen(): bool
{
if ($this->getOpeningDate() > new \DateTimeImmutable('now')) {
if ($this->getOpeningDate() > new \DateTime('now')) {
return false;
}

View File

@@ -22,6 +22,7 @@ namespace Chill\PersonBundle\Entity;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use ArrayIterator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Entity\Country;
use Chill\PersonBundle\Entity\MaritalStatus;
@@ -29,9 +30,7 @@ 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
@@ -128,14 +127,14 @@ class Person implements HasCenterInterface {
*/
private $fullnameCanonical;
public function __construct(\DateTimeImmutable $opening = null) {
public function __construct(\DateTime $opening = null) {
$this->accompanyingPeriods = new ArrayCollection();
$this->spokenLanguages = new ArrayCollection();
$this->addresses = new ArrayCollection();
$this->altNames = new ArrayCollection();
if ($opening === null) {
$opening = new \DateTimeImmutable();
$opening = new \DateTime();
}
$this->open(new AccompanyingPeriod($opening));
@@ -747,18 +746,17 @@ class Person implements HasCenterInterface {
/**
* By default, the addresses are ordered by date, descending (the most
* recent first)
*
* @return ArrayCollection
*/
public function getAddresses(): ArrayCollection
{
return $this->addresses;
}
public function getLastAddress(?DateTimeImmutable $from = null): ?Address
public function getLastAddress(DateTime $from = null)
{
$from ??= new DateTimeImmutable('now');
$from ??= new DateTime('now');
/** @var ArrayIterator $addressesIterator */
$addressesIterator = $this->getAddresses()
->filter(static fn (Address $address): bool => $address->getValidFrom() <= $from)
->getIterator();