This extention check for instance object.
It check if $object
is a real object before reflect the clas itself.
-
-
Save kl3sk/ced7d082506e11783ac084d698759d6f to your computer and use it in GitHub Desktop.
instanceof twig test - SF 5
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 App\Twig; | |
use Twig\Extension\AbstractExtension; | |
use Twig\TwigTest; | |
class InstanceOfExtension extends AbstractExtension | |
{ | |
public function getTests() | |
{ | |
return array( | |
new TwigTest('instanceof', array($this, 'isInstanceOf')) | |
); | |
} | |
public function isInstanceOf($object, $class) | |
{ | |
if (!is_object($object)) { | |
return false; | |
} | |
$reflectionClass = new \ReflectionClass($class); | |
return $reflectionClass->isInstance($object); | |
} | |
} |
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
{% if object is instanceof('Acme\\Foo\\Entity') %} | |
<p>true</p> | |
{% else %} | |
<p>false</p> | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment