-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from thunderer/builtin-handlers
Builtin handlers
- Loading branch information
Showing
10 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters