Browse Source

[TWIG] Add `instanceof` test

Use with:
{% if var is instanceof(Namespace\Class) %}
remotes/upstream/experimental
Hugo Sales 8 months ago
parent
commit
1107d8257d
Signed by: someonewithpc <hugo@hsal.es> GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 14 additions and 1 deletions
  1. +8
    -0
      src/Twig/Extension.php
  2. +6
    -1
      src/Twig/Runtime.php

+ 8
- 0
src/Twig/Extension.php View File

@@ -32,6 +32,7 @@ namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Twig\TwigTest;

class Extension extends AbstractExtension
{
@@ -42,6 +43,13 @@ class Extension extends AbstractExtension
];
}

public function getTests()
{
return [
new TwigTest('instanceof', [Runtime::class, 'isInstanceOf']),
];
}

/**
* get twig functions
*


+ 6
- 1
src/Twig/Runtime.php View File

@@ -119,11 +119,16 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface
public function isFirefox(): bool
{
$re_has_chrome = '/.*(?i)\bchrome\b.*/m';
$re_has_gecko = '/.*(?i)\bgecko\b.*/m';
$re_has_gecko = '/.*(?i)\bgecko\b.*/m';
return (preg_match(pattern: $re_has_chrome, subject: $this->request->headers->get('User-Agent')) !== 1)
&& (preg_match(pattern: $re_has_gecko, subject: $this->request->headers->get('User-Agent')) === 1);
}

public function isInstanceOf($var, string $instance): bool
{
return $var instanceof $instance;
}

// ----------------------------------------------------------

/**


Loading…
Cancel
Save