Skip to content

Commit 27f09a2

Browse files
committed
new methods in Shortcode, udpated README with usage examples
1 parent 5c8dcc9 commit 27f09a2

3 files changed

Lines changed: 86 additions & 14 deletions

File tree

README.md

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,61 @@ and run `composer install` or `composer update` afterwards. If you're not using
4949

5050
## Usage
5151

52-
Create `Shortcode` class instance, register required shortcodes handlers and pass your strings to be parsed.
52+
**Replacement**
53+
54+
Create `Processor` class instance, register required shortcodes handlers and use `process()` method to dynamically replace found matches using registered callbacks:
5355

5456
```php
57+
use Thunder\Shortcode\Extractor;
5558
use Thunder\Shortcode\Parser;
59+
use Thunder\Shortcode\Processor;
5660
use Thunder\Shortcode\Shortcode;
61+
use Thunder\Shortcode\Serializer\JsonSerializer;
5762

58-
$parser = new Parser();
59-
$parser->addCode('sample', function(Shortcode $s) {
60-
return json_encode(array(
61-
'name' => $s->getName(),
62-
'args' => $s->getParameters(),
63-
'content' => $s->getContent(),
64-
));
63+
$processor = new Processor(new Extractor(), new Parser());
64+
$processor->addHandler('sample', function(Shortcode $s) {
65+
return (new JsonSerializer())->serialize($s);
6566
});
6667

6768
// this will produce JSON encoded parsed data of given shortcode, eg.:
68-
// {"name":"sample","args":{"argument":"value"},"content":"content"}
69-
echo $parser->parse('[sample argument=value]content[/sample]');
69+
// something {"name":"sample","args":{"argument":"value"},"content":"content"} other
70+
echo $parser->parse('something [sample argument=value]content[/sample] other');
71+
```
72+
73+
**Extraction**
74+
75+
Create instance of class `Extractor` and use its `extract()` method to get array of shortcode matches:
76+
77+
```php
78+
use Thunder\Shortcode\Extractor;
79+
80+
$extractor = new Extractor();
81+
$matches = $extractor->extract('something [x] other [random]sth[/random] other');
82+
83+
// array will contain two instances of class Match with match offsets and exact
84+
// strings, like [10, '[x]'] and [20, '[random]sth[/random]']
85+
var_dump($matches);
86+
```
87+
88+
**Parsing**
89+
90+
Create instance of `Parser` class and use its `parse()` method to parse single shortcode string match into `Shortcode` instance with easy access to its name, parameters, and content (null if none present):
91+
92+
```php
93+
use Thunder\Shortcode\Parser;
94+
95+
$parser = new Parser();
96+
$shortcode = $parser->parse('[code arg=value]something[/code]');
97+
98+
// will contain name "code", one argument and "something" as content.
99+
var_dump($shortcode);
70100
```
71101

72-
Edge cases:
102+
## Edge cases
73103

74104
* unsupported shortcodes (no registered handler) will be ignored and left as they are,
75-
* mismatching closing shortcode (`[code]content[/codex]`) will be ignored, opening will be interpreted as self-closing shortcode,
76-
* overlapping shortcodes (`[code]content[inner][/code]content[/inner]`) are not supported and will be interpreted as self-closing, ending fragment will be ignored.
105+
* mismatching closing shortcode (`[code]content[/codex]`) will be ignored, opening tag will be interpreted as self-closing shortcode,
106+
* overlapping shortcodes (`[code]content[inner][/code]content[/inner]`) are not supported and will be interpreted as self-closing, closing tag will be ignored.
77107

78108
## Ideas
79109

src/Shortcode.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ public function getParameters()
3232
return $this->parameters;
3333
}
3434

35+
public function hasParameter($name)
36+
{
37+
return array_key_exists($name, $this->parameters);
38+
}
39+
40+
public function getParameter($name, $default = null)
41+
{
42+
if($this->hasParameter($name))
43+
{
44+
return $this->parameters[$name];
45+
}
46+
if(null !== $default)
47+
{
48+
return $default;
49+
}
50+
51+
$msg = 'Shortcode parameter %s not found and no default value was set!';
52+
throw new \RuntimeException(sprintf($msg, $name));
53+
}
54+
3555
public function getContent()
3656
{
3757
return $this->content;

tests/ShortcodeTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class ShortcodeTest extends \PHPUnit_Framework_TestCase
1717
*
1818
* @dataProvider provideShortcodes
1919
*/
20-
public function testObject($expected, $name, array $args, $content)
20+
public function testShortcode($expected, $name, array $args, $content)
2121
{
2222
$s = new Shortcode($name, $args, $content);
2323
$textSerializer = new TextSerializer();
@@ -37,4 +37,26 @@ public function provideShortcodes()
3737
array('[x arg="val val"]inner[/x]', 'x', array('arg' => 'val val'), 'inner'),
3838
);
3939
}
40+
41+
public function testObject()
42+
{
43+
$shortcode = new Shortcode('random', array(
44+
'arg' => 'value',
45+
'none' => null,
46+
), 'something');
47+
48+
$this->assertTrue($shortcode->hasParameter('arg'));
49+
$this->assertFalse($shortcode->hasParameter('invalid'));
50+
$this->assertSame(null, $shortcode->getParameter('none'));
51+
$this->assertSame('value', $shortcode->getParameter('arg'));
52+
$this->assertSame('', $shortcode->getParameter('invalid', ''));
53+
$this->assertSame(42, $shortcode->getParameter('invalid', 42));
54+
}
55+
56+
public function testExceptionOnMissingParameterWithNoDefaultValue()
57+
{
58+
$shortcode = new Shortcode('name', array(), null);
59+
$this->setExpectedException('RuntimeException');
60+
$shortcode->getParameter('invalid');
61+
}
4062
}

0 commit comments

Comments
 (0)