Last active
August 29, 2015 14:15
-
-
Save xthiago/d9935804cbb555bd4720 to your computer and use it in GitHub Desktop.
Add support for instanceof in Twig
This file contains hidden or 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
<?php | |
namespace Xthiago\CoreBundle\Twig; | |
class InstanceofExtension extends \Twig_Extension | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getTests() | |
{ | |
return array( | |
new \Twig_SimpleTest('instanceof', array($this, 'isInstanceOf')) | |
); | |
} | |
public function isInstanceOf($object, $class) | |
{ | |
if (!is_object($object)) | |
return false; | |
$reflectionClass = new \ReflectionClass($class); | |
return $reflectionClass->isInstance($object); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return 'xthiago_instanceof_extension'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment