Conditions | 3 |
Paths | 10 |
Total Lines | 21 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Tests | 13 |
CRAP Score | 3 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
32 | 17 | public function validate($value, Constraint $constraint) |
|
33 | { |
||
34 | // valueがnullの場合は "Template is not defined"のエラーが投げられるので, 空文字でチェックする. |
||
35 | 17 | if (is_null($value)) { |
|
36 | 1 | $value = ''; |
|
37 | } |
||
38 | |||
39 | 17 | $realLoader = $this->twig->getLoader(); |
|
40 | try { |
||
41 | 17 | $temporaryLoader = new ArrayLoader(['' => $value]); |
|
42 | 17 | $this->twig->setLoader($temporaryLoader); |
|
43 | 17 | $nodeTree = $this->twig->parse($this->twig->tokenize(new Source($value, ''))); |
|
44 | 16 | $this->twig->compile($nodeTree); |
|
45 | |||
46 | 1 | } catch (Error $e) { |
|
47 | 1 | $this->context->buildViolation($constraint->message) |
|
48 | 1 | ->setParameter('{{ error }}', $e->getMessage()) |
|
49 | 1 | ->addViolation(); |
|
50 | } |
||
51 | 17 | $this->twig->setLoader($realLoader); |
|
52 | } |
||
53 | } |
||
54 |