vendor/contao-community-alliance/events-contao-bindings/src/Subscribers/DateSubscriber.php line 71

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of contao-community-alliance/events-contao-bindings
  4.  *
  5.  * (c) 2014-2018 The Contao Community Alliance
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  *
  10.  * This project is provided in good faith and hope to be usable by anyone.
  11.  *
  12.  * @package    ContaoCommunityAlliance\Contao\Bindings
  13.  * @subpackage Subscribers
  14.  * @author     Christian Schiffler <c.schiffler@cyberspectrum.de>
  15.  * @author     Sven Baumann <baumann.sv@gmail.com>
  16.  * @copyright  2018 The Contao Community Alliance.
  17.  * @license    https://github.com/contao-community-alliance/events-contao-bindings/blob/master/LICENSE LGPL-3.0
  18.  * @filesource
  19.  */
  20. namespace ContaoCommunityAlliance\Contao\Bindings\Subscribers;
  21. use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
  22. use Contao\Date;
  23. use ContaoCommunityAlliance\Contao\Bindings\ContaoEvents;
  24. use ContaoCommunityAlliance\Contao\Bindings\Events\Date\ParseDateEvent;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. /**
  27.  * Subscriber for the System class in Contao.
  28.  */
  29. class DateSubscriber implements EventSubscriberInterface
  30. {
  31.     /**
  32.      * The contao framework.
  33.      *
  34.      * @var ContaoFrameworkInterface
  35.      */
  36.     protected $framework;
  37.     /**
  38.      * DateSubscriber constructor.
  39.      *
  40.      * @param ContaoFrameworkInterface $framework The contao framework.
  41.      */
  42.     public function __construct(ContaoFrameworkInterface $framework)
  43.     {
  44.         $this->framework $framework;
  45.     }
  46.     /**
  47.      * Returns an array of event names this subscriber wants to listen to.
  48.      *
  49.      * @return array
  50.      */
  51.     public static function getSubscribedEvents()
  52.     {
  53.         return [
  54.             ContaoEvents::DATE_PARSE => 'handleParseDate',
  55.         ];
  56.     }
  57.     /**
  58.      * Handle the date parsing.
  59.      *
  60.      * @param ParseDateEvent $event The event.
  61.      *
  62.      * @return void
  63.      */
  64.     public function handleParseDate($event)
  65.     {
  66.         if ($event->getResult() === null) {
  67.             /** @var Date $dateAdapter */
  68.             $dateAdapter $this->framework->getAdapter(Date::class);
  69.             $event->setResult($dateAdapter->parse($event->getFormat(), $event->getTimestamp()));
  70.         }
  71.     }
  72. }