vendor/netzmacht/contao-toolkit/src/Dca/Formatter/Subscriber/CreateFormatterSubscriber.php line 82

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao toolkit.
  4.  *
  5.  * @package    contao-toolkit
  6.  * @author     David Molineus <david.molineus@netzmacht.de>
  7.  * @copyright  2015-2020 netzmacht David Molineus.
  8.  * @license    LGPL-3.0-or-later https://github.com/netzmacht/contao-toolkit/blob/master/LICENSE
  9.  * @filesource
  10.  */
  11. declare(strict_types=1);
  12. namespace Netzmacht\Contao\Toolkit\Dca\Formatter\Subscriber;
  13. use Netzmacht\Contao\Toolkit\Dca\Formatter\Event\CreateFormatterEvent;
  14. use Netzmacht\Contao\Toolkit\Dca\Formatter\Value\ValueFormatter;
  15. /**
  16.  * Class CreateFormatterSubscriber handles the create formatter event.
  17.  *
  18.  * @package Netzmacht\Contao\Toolkit\Dca\Formatter\Subscriber
  19.  */
  20. final class CreateFormatterSubscriber
  21. {
  22.     /**
  23.      * List of supported value formatter.
  24.      *
  25.      * @var array|ValueFormatter[]
  26.      */
  27.     private $formatter;
  28.     /**
  29.      * Value formatter pre filters.
  30.      *
  31.      * @var array|ValueFormatter[]
  32.      */
  33.     private $preFilters;
  34.     /**
  35.      * Value formatter post filters.
  36.      *
  37.      * @var array|ValueFormatter[]
  38.      */
  39.     private $postFilters;
  40.     /**
  41.      * Value formatter.
  42.      *
  43.      * @var ValueFormatter
  44.      */
  45.     private $optionsFormatter;
  46.     /**
  47.      * CreateFormatterSubscriber constructor.
  48.      *
  49.      * @param array|ValueFormatter[] $formatter        Value formatter.
  50.      * @param array|ValueFormatter[] $preFilters       Pre filters.
  51.      * @param array|ValueFormatter[] $postFilters      Post filters.
  52.      * @param ValueFormatter         $optionsFormatter Options formatter.
  53.      */
  54.     public function __construct(
  55.         array $formatter,
  56.         array $preFilters,
  57.         array $postFilters,
  58.         ValueFormatter $optionsFormatter
  59.     ) {
  60.         $this->formatter        $formatter;
  61.         $this->preFilters       $preFilters;
  62.         $this->postFilters      $postFilters;
  63.         $this->optionsFormatter $optionsFormatter;
  64.     }
  65.     /**
  66.      * Handle the create formatter event.
  67.      *
  68.      * @param CreateFormatterEvent $event The handled event.
  69.      *
  70.      * @return void
  71.      */
  72.     public function handle(CreateFormatterEvent $event): void
  73.     {
  74.         $event->addFormatter($this->formatter);
  75.         $event->addPreFilters($this->preFilters);
  76.         $event->addPostFilters($this->postFilters);
  77.         if (!$event->getOptionsFormatter()) {
  78.             $event->setOptionsFormatter($this->optionsFormatter);
  79.         }
  80.     }
  81. }