deblan.tv/vendor/trinity/src/Trinity/Bundle/ContentManagerBundle/Twig/Extension/ObjectExtension.php
2015-03-02 21:57:49 +01:00

77 lines
1.8 KiB
PHP

<?php
namespace Trinity\Bundle\ContentManagerBundle\Twig\Extension;
use Trinity\Component\Utils\Propel;
class ObjectExtension extends \Twig_Extension
{
public function getName()
{
return 'object_extension';
}
public function getFunctions()
{
return array(
'has_method' => new \Twig_Filter_Method($this, 'hasMethod'),
'is_array' => new \Twig_Filter_Method($this, 'isArray'),
);
}
public function getFilters()
{
return array(
'pp' => new \Twig_Filter_Method($this, 'parseParameters', array('is_safe' => array('html'))),
'getclass' => new \Twig_Filter_Method($this, 'getClass'),
'get_class' => new \Twig_Filter_Method($this, 'getClass'),
);
}
public function hasMethod($object, $method)
{
if (!is_object($object)) {
return false;
}
return method_exists($object, $method);
}
public function isArray($object)
{
return is_array($object);
}
public function parseParameters($text, $object)
{
if (!is_object($object)) {
return $text;
}
preg_match_all('`%([a-zA-Z0-9]+)%`i', $text, $matches, PREG_SET_ORDER);
if (!$matches) {
return $text;
}
foreach ($matches as $match) {
$method = $match[1] !== 'toString' ? Propel::getGetter($match[1]) : '__toString';
if (method_exists($object, $method)) {
$text = str_replace($match[0], call_user_func(array($object, $method)), $text);
}
}
return $text;
}
public function getClass($object)
{
if (is_object($object)) {
return get_class($object);
}
return null;
}
}