1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Http/ValidationRules/Design/TwigLint.php
2024-06-14 17:09:44 +10:00

43 lines
1.1 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\ValidationRules\Design;
use Closure;
use App\Services\Template\TemplateService;
use Illuminate\Contracts\Validation\ValidationRule;
class TwigLint implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$ts = new TemplateService();
$twig = $ts->twig;
try {
$twig->parse($twig->tokenize(new \Twig\Source(preg_replace('/<!--.*?-->/s', '', $value), '')));
} catch (\Twig\Error\SyntaxError $e) {
// echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
nlog($e->getMessage());
$fail($e->getMessage());
}
}
}