|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Giansalex |
|
5
|
|
|
* Date: 09/08/2017 |
|
6
|
|
|
* Time: 19:23 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Greenter\Xml\Builder; |
|
10
|
|
|
|
|
11
|
|
|
use Greenter\Xml\Exception\ValidationException; |
|
12
|
|
|
use Symfony\Component\Validator\Validation; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class BaseBuilder |
|
16
|
|
|
* @package Greenter\Xml\Builder |
|
17
|
|
|
*/ |
|
18
|
|
|
class BaseBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Directorio de Cache para las template de Documentos. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $dirCache; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Get Content XML from template. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $template |
|
31
|
|
|
* @param object $doc |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
54 |
|
public function render($template, $doc) |
|
35
|
|
|
{ |
|
36
|
54 |
|
$twig = $this->getRender(); |
|
37
|
54 |
|
return $twig->render($template, [ |
|
38
|
|
|
'doc' => $doc |
|
39
|
54 |
|
]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
54 |
|
private function getRender() |
|
43
|
2 |
|
{ |
|
44
|
|
|
//TODO: load render one time. |
|
45
|
54 |
|
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../Templates'); |
|
46
|
54 |
|
$numFilter = new \Twig_SimpleFilter('n_format', function ($number, $decimals = 2) { |
|
47
|
44 |
|
return number_format($number, $decimals, '.', ''); |
|
48
|
54 |
|
}); |
|
49
|
54 |
|
$twig = new \Twig_Environment($loader, array( |
|
50
|
54 |
|
'cache' => $this->dirCache, |
|
51
|
54 |
|
)); |
|
52
|
54 |
|
$twig->addFilter($numFilter); |
|
53
|
|
|
|
|
54
|
54 |
|
return $twig; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Validate Entity. |
|
59
|
|
|
* |
|
60
|
|
|
* @param object $entity |
|
61
|
|
|
* @throws ValidationException |
|
62
|
|
|
*/ |
|
63
|
88 |
|
public function validate($entity) |
|
64
|
|
|
{ |
|
65
|
88 |
|
$validator = Validation::createValidatorBuilder() |
|
66
|
88 |
|
->addMethodMapping('loadValidatorMetadata') |
|
67
|
88 |
|
->getValidator(); |
|
68
|
|
|
|
|
69
|
88 |
|
$errs = $validator->validate($entity); |
|
70
|
88 |
|
if ($errs->count() > 0) { |
|
71
|
34 |
|
throw new ValidationException($errs); |
|
72
|
|
|
} |
|
73
|
54 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set argumentos. |
|
77
|
|
|
* |
|
78
|
|
|
* @param $params |
|
79
|
|
|
* @throws \Exception |
|
80
|
|
|
*/ |
|
81
|
94 |
|
protected function addParameters($params) |
|
82
|
|
|
{ |
|
83
|
94 |
|
if (!$params['cache_dir']) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
94 |
|
$this->dirCache = $params['cache_dir']; |
|
88
|
|
|
} |
|
89
|
|
|
} |