Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builtin handlers #33

Merged
merged 2 commits into from
Feb 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Handler/ContentHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Thunder\Shortcode\Handler;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class ContentHandler
{
/**
* [content]text to display[/content]
*
* @param ShortcodeInterface $shortcode
*
* @return null|string
*/
public function __invoke(ShortcodeInterface $shortcode)
{
return $shortcode->getContent();
}
}
47 changes: 47 additions & 0 deletions src/Handler/DeclareHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace Thunder\Shortcode\Handler;

use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class DeclareHandler
{
/** @var HandlerContainer */
private $handlers;
private $delimiter;

public function __construct(HandlerContainer $container, $delimiter = '%')
{
$this->handlers = $container;
$this->delimiter = $delimiter;
}

/**
* [declare name]Your name is %value%[/declare]
* [name value="Thomas" /]
*
* @param ShortcodeInterface $shortcode
*/
public function __invoke(ShortcodeInterface $shortcode)
{
$args = $shortcode->getParameters();
if(empty($args)) {
return;
}
$keys = array_keys($args);
$name = array_shift($keys);
$content = $shortcode->getContent();
$delimiter = $this->delimiter;

$this->handlers->add($name, function(ShortcodeInterface $shortcode) use($content, $delimiter) {
$args = $shortcode->getParameters();
$keys = array_map(function($key) use($delimiter) { return $delimiter.$key.$delimiter; }, array_keys($args));
$values = array_values($args);

return str_replace($keys, $values, $content);
});
}
}
27 changes: 27 additions & 0 deletions src/Handler/EmailHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Thunder\Shortcode\Handler;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class EmailHandler
{
/**
* [email="example@example.org"]Contact me![/email]
* [email="example@example.org" /]
* [email]example@example.org[/email]
*
* @param ShortcodeInterface $shortcode
*
* @return string
*/
public function __invoke(ShortcodeInterface $shortcode)
{
$email = $shortcode->getBbCode() ?: $shortcode->getContent();
$content = $shortcode->getContent() === null ? $email : $shortcode->getContent();

return '<a href="mailto:'.$email.'">'.$content.'</a>';
}
}
23 changes: 23 additions & 0 deletions src/Handler/NameHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Thunder\Shortcode\Handler;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class NameHandler
{
/**
* [name /]
* [name]content is ignored[/name]
*
* @param ShortcodeInterface $shortcode
*
* @return string
*/
public function __invoke(ShortcodeInterface $shortcode)
{
return $shortcode->getName();
}
}
22 changes: 22 additions & 0 deletions src/Handler/NullHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Thunder\Shortcode\Handler;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class NullHandler
{
/**
* Special shortcode to discard any input and return empty text
*
* @param ShortcodeInterface $shortcode
*
* @return null
*/
public function __invoke(ShortcodeInterface $shortcode)
{
return null;
}
}
34 changes: 34 additions & 0 deletions src/Handler/PlaceholderHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Thunder\Shortcode\Handler;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class PlaceholderHandler
{
private $delimiter;

public function __construct($delimiter = '%')
{
$this->delimiter = $delimiter;
}

/**
* [placeholder value=18]You age is %value%[/placeholder]
*
* @param ShortcodeInterface $shortcode
*
* @return mixed
*/
public function __invoke(ShortcodeInterface $shortcode)
{
$args = $shortcode->getParameters();
$delimiter = $this->delimiter;
$keys = array_map(function($key) use($delimiter) { return $delimiter.$key.$delimiter; }, array_keys($args));
$values = array_values($args);

return str_replace($keys, $values, $shortcode->getContent());
}
}
38 changes: 38 additions & 0 deletions src/Handler/SerializerHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace Thunder\Shortcode\Handler;

use Thunder\Shortcode\Serializer\SerializerInterface;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class SerializerHandler
{
/** @var SerializerInterface */
private $serializer;

function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}

/**
* [text arg=val /]
* [text arg=val]content[/text]
* [json arg=val /]
* [json arg=val]content[/json]
* [xml arg=val /]
* [xml arg=val]content[/xml]
* [yaml arg=val /]
* [yaml arg=val]content[/yaml]
*
* @param ShortcodeInterface $shortcode
*
* @return string
*/
public function __invoke(ShortcodeInterface $shortcode)
{
return $this->serializer->serialize($shortcode);
}
}
26 changes: 26 additions & 0 deletions src/Handler/UrlHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Thunder\Shortcode\Handler;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class UrlHandler
{
/**
* [url="http://example.org"]Click![/url]
* [url="http://example.org" /]
* [url]http://example.org[/url]
*
* @param ShortcodeInterface $shortcode
*
* @return string
*/
public function __invoke(ShortcodeInterface $shortcode)
{
$url = $shortcode->getBbCode() ?: $shortcode->getContent();

return '<a href="'.$url.'">'.$shortcode->getContent().'</a>';
}
}
37 changes: 37 additions & 0 deletions src/Handler/WrapHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace Thunder\Shortcode\Handler;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class WrapHandler
{
private $before;
private $after;

public function __construct($before, $after)
{
$this->before = $before;
$this->after = $after;
}

public static function createBold()
{
return new self('<b>', '</b>');
}

/**
* [b]content[b]
* [strong]content[/strong]
*
* @param ShortcodeInterface $shortcode
*
* @return string
*/
public function __invoke(ShortcodeInterface $shortcode)
{
return $this->before.$shortcode->getContent().$this->after;
}
}
57 changes: 57 additions & 0 deletions tests/ProcessorTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
<?php
namespace Thunder\Shortcode\Tests;

use Thunder\Shortcode\Handler\ContentHandler;
use Thunder\Shortcode\Handler\DeclareHandler;
use Thunder\Shortcode\Handler\EmailHandler;
use Thunder\Shortcode\Handler\NameHandler;
use Thunder\Shortcode\Handler\NullHandler;
use Thunder\Shortcode\Handler\PlaceholderHandler;
use Thunder\Shortcode\Handler\SerializerHandler;
use Thunder\Shortcode\Handler\UrlHandler;
use Thunder\Shortcode\Handler\WrapHandler;
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Parser\RegexParser;
use Thunder\Shortcode\Parser\RegularParser;
use Thunder\Shortcode\Processor\Processor;
use Thunder\Shortcode\Shortcode\ParsedShortcodeInterface;
use Thunder\Shortcode\Serializer\JsonSerializer;
use Thunder\Shortcode\Serializer\TextSerializer;
use Thunder\Shortcode\Shortcode\ProcessedShortcode;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Thunder\Shortcode\Tests\Fake\ReverseShortcode;
Expand Down Expand Up @@ -139,6 +150,52 @@ public function testProcessorShortcodePositions()
$this->assertSame('1231567', $processor->process('[p][p][p][n][p][p][p]'), 'pppnppp');
}

/**
* @dataProvider provideBuiltInTests
*/
public function testBuiltInHandlers($text, $result)
{
$handlers = new HandlerContainer();
$handlers
->add('content', new ContentHandler())
->add('name', new NameHandler())
->add('null', new NullHandler())
->add('json', new SerializerHandler(new JsonSerializer()))
->add('text', new SerializerHandler(new TextSerializer()))
->add('placeholder', new PlaceholderHandler())
->add('b', new WrapHandler('<b>', '</b>'))
->add('bb', WrapHandler::createBold())
->add('declare', new DeclareHandler($handlers))
->add('url', new UrlHandler())
->add('email', new EmailHandler());
$processor = new Processor(new RegexParser(), $handlers);

$this->assertSame($result, $processor->process($text));
}

public function provideBuiltInTests()
{
return array(
array('[declare date]%year%.%month%.%day%[/declare][date year=2015 month=08 day=26]', '2015.08.26'),
array('[declare sample]%param%[/declare][invalid param=value]', '[invalid param=value]'),
array('[declare]%param%[/declare][invalid param=value]', '[invalid param=value]'),
array('[url]http://kowalczyk.cc[/url]', '<a href="http://kowalczyk.cc">http://kowalczyk.cc</a>'),
array('[url="http://kowalczyk.cc"]Visit![/url]', '<a href="http://kowalczyk.cc">Visit!</a>'),
array('[email]tomasz@kowalczyk.cc[/email]', '<a href="mailto:tomasz@kowalczyk.cc">tomasz@kowalczyk.cc</a>'),
array('[email="tomasz@kowalczyk.cc"]Send![/email]', '<a href="mailto:tomasz@kowalczyk.cc">Send!</a>'),
array('[email="tomasz@kowalczyk.cc" /]', '<a href="mailto:tomasz@kowalczyk.cc">tomasz@kowalczyk.cc</a>'),
array('[b]text[/b]', '<b>text</b>'),
array('[bb]text[/bb]', '<b>text</b>'),
array('[json arg=val]value[/json]', '{"name":"json","parameters":{"arg":"val"},"content":"value","bbCode":null}'),
array('[text arg=val]value[/text]', '[text arg=val]value[/text]'),
array('[null arg=val]value[/null]', ''),
array('[name /]', 'name'),
array('[content]cnt[/content]', 'cnt'),
array('[placeholder param=val]%param%[/placeholder]', 'val'),
array('[placeholder param=val]%param%[/placeholder]', 'val'),
);
}

public function testProcessorDeclare()
{
$handlers = new HandlerContainer();
Expand Down