-
Notifications
You must be signed in to change notification settings - Fork 35
/
BbcodeExtension.php
91 lines (80 loc) · 2.1 KB
/
BbcodeExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace FM\BbcodeBundle\Templating;
use Symfony\Component\DependencyInjection\ContainerInterface;
use FM\BbcodeBundle\Decoda\DecodaManager as DecodaManager;
/**
* @author Al Ganiev <helios.ag@gmail.com>
* @copyright 2012-2015 Al Ganiev
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
class BbcodeExtension extends \Twig_Extension
{
/**
* @var DecodaManager
*/
protected $decodaManager;
/**
* @param ContainerInterface $container
*/
public function __construct(DecodaManager $decodaManager)
{
$this->decodaManager = $decodaManager;
}
/**
* (non-PHPdoc).
*
* @see Twig_Extension::getFilters()
*
* @return array
*/
public function getFilters()
{
$options = array('is_safe' => array('html'));
return array(
new \Twig_SimpleFilter('bbcode_filter', array($this, 'filter'), $options),
new \Twig_SimpleFilter('bbcode_clean', array($this, 'clean'), $options),
);
}
/**
* @param $value
* @param $filterSet
*
* @return string
* @return \FM\BbcodeBundle\Decoda\Decoda
*
* @throws \Twig_Error_Runtime
*/
public function filter($value, $filterSet = DecodaManager::DECODA_DEFAULT)
{
if (!is_string($value)) {
throw new \Twig_Error_Runtime('The filter can be applied to strings only.');
}
return $this->decodaManager->get($value, $filterSet)->parse();
}
/**
* Strip tags.
*
* @param $value
* @param $filterSet
*
* @return string
*
* @throws \Twig_Error_Runtime
*/
public function clean($value, $filterSet = DecodaManager::DECODA_DEFAULT)
{
if (!is_string($value)) {
throw new \Twig_Error_Runtime('The filter can be applied to strings only.');
}
return $this->decodaManager->get($value, $filterSet)->strip(true);
}
/**
* (non-PHPdoc).
*
* @see Twig_ExtensionInterface::getName()
*/
public function getName()
{
return 'fm_bbcode';
}
}