vendor/php-flasher/flasher/Stamp/CreatedAtStamp.php line 14

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the PHPFlasher package.
  4.  * (c) Younes KHOUBZA <younes.khoubza@gmail.com>
  5.  */
  6. namespace Flasher\Prime\Stamp;
  7. use DateTime;
  8. use DateTimeZone;
  9. use Exception;
  10. final class CreatedAtStamp implements StampInterfaceOrderableStampInterfacePresentableStampInterface
  11. {
  12.     /**
  13.      * @var DateTime
  14.      */
  15.     private $createdAt;
  16.     /**
  17.      * @var string
  18.      */
  19.     private $format;
  20.     /**
  21.      * @param string|null $format
  22.      *
  23.      * @throws Exception
  24.      */
  25.     public function __construct(DateTime $createdAt null$format null)
  26.     {
  27.         $this->createdAt $createdAt ?: new DateTime('now', new DateTimeZone('Africa/Casablanca'));
  28.         $this->format $format ?: 'Y-m-d H:i:s';
  29.     }
  30.     /**
  31.      * @return DateTime
  32.      */
  33.     public function getCreatedAt()
  34.     {
  35.         return $this->createdAt;
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function compare($orderable)
  41.     {
  42.         if (!$orderable instanceof self) {
  43.             return 1;
  44.         }
  45.         return $this->createdAt->getTimestamp() - $orderable->createdAt->getTimestamp();
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public function toArray()
  51.     {
  52.         $createdAt $this->getCreatedAt();
  53.         return array('created_at' => $createdAt->format($this->format));
  54.     }
  55. }