1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Twig\Node\Expression\Binary; 13 14use Twig\Compiler; 15use Twig\Error\SyntaxError; 16use Twig\Node\CoercesChildrenToStringInterface; 17use Twig\Node\Expression\AbstractExpression; 18use Twig\Node\Expression\ConstantExpression; 19use Twig\Node\Expression\ReturnBoolInterface; 20use Twig\Node\Node; 21 22class MatchesBinary extends AbstractBinary implements ReturnBoolInterface, CoercesChildrenToStringInterface 23{ 24 public function __construct(Node $left, Node $right, int $lineno) 25 { 26 if (!$left instanceof AbstractExpression) { 27 trigger_deprecation('twig/twig', '3.24', 'Passing a "%s" instance to "%s()" first argument is deprecated, pass an "AbstractExpression" instance instead.', $left::class, __METHOD__); 28 } 29 if (!$right instanceof AbstractExpression) { 30 trigger_deprecation('twig/twig', '3.24', 'Passing a "%s" instance to "%s()" second argument is deprecated, pass an "AbstractExpression" instance instead.', $right::class, __METHOD__); 31 } 32 33 if ($right instanceof ConstantExpression) { 34 $regexp = $right->getAttribute('value'); 35 set_error_handler(static fn ($t, $m) => throw new SyntaxError(\sprintf('Regexp "%s" passed to "matches" is not valid: %s.', $regexp, substr($m, 14)), $lineno)); 36 try { 37 preg_match($regexp, ''); 38 } finally { 39 restore_error_handler(); 40 } 41 } 42 43 parent::__construct($left, $right, $lineno); 44 } 45 46 public function compile(Compiler $compiler): void 47 { 48 $compiler 49 ->raw('CoreExtension::matches(') 50 ->subcompile($this->getNode('right')) 51 ->raw(', ') 52 ->subcompile($this->getNode('left')) 53 ->raw(')') 54 ; 55 } 56 57 public function operator(Compiler $compiler): Compiler 58 { 59 return $compiler->raw(''); 60 } 61 62 public function getStringCoercedChildNames(): array 63 { 64 return ['left', 'right']; 65 } 66} 67