thunderer/Shortcode

View on GitHub
src/EventHandler/ReplaceJoinEventHandler.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
namespace Thunder\Shortcode\EventHandler;

use Thunder\Shortcode\Event\ReplaceShortcodesEvent;

/**
 * @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
 */
final class ReplaceJoinEventHandler
{
    /** @var string[] */
    private $names = array();

    public function __construct(array $names)
    {
        foreach($names as $name) {
            if(false === is_string($name)) {
                throw new \InvalidArgumentException('Expected array of strings!');
            }

            $this->names[] = $name;
        }
    }

    public function __invoke(ReplaceShortcodesEvent $event)
    {
        $shortcode = $event->getShortcode();
        if($shortcode && in_array($shortcode->getName(), $this->names)) {
            $replaces = array();
            foreach($event->getReplacements() as $r) {
                $replaces[] = $r->getReplacement();
            }

            $event->setResult(implode('', $replaces));
        }
    }
}