|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MewesK\TwigSpreadsheetBundle\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\NodeVisitor\MacroContextNodeVisitor; |
|
6
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\NodeVisitor\SyntaxCheckNodeVisitor; |
|
7
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\TokenParser\AlignmentTokenParser; |
|
8
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\TokenParser\CellTokenParser; |
|
9
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\TokenParser\DocumentTokenParser; |
|
10
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\TokenParser\DrawingTokenParser; |
|
11
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\TokenParser\HeaderFooterTokenParser; |
|
12
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\TokenParser\RowTokenParser; |
|
13
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\TokenParser\SheetTokenParser; |
|
14
|
|
|
use MewesK\TwigSpreadsheetBundle\Wrapper\HeaderFooterWrapper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class TwigSpreadsheetExtension. |
|
18
|
|
|
*/ |
|
19
|
|
|
class TwigSpreadsheetExtension extends \Twig_Extension |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $attributes; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* TwigSpreadsheetExtension constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $attributes |
|
30
|
|
|
*/ |
|
31
|
26 |
|
public function __construct(array $attributes = []) |
|
32
|
|
|
{ |
|
33
|
26 |
|
$this->attributes = $attributes; |
|
34
|
26 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function getAttributes(): array |
|
40
|
|
|
{ |
|
41
|
2 |
|
return $this->attributes; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
8 |
|
public function getFunctions() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
8 |
|
new \Twig_SimpleFunction('xlsmergestyles', [$this, 'mergeStyles']), |
|
51
|
8 |
|
new \Twig_SimpleFunction('xlsrowindex', [$this, 'getRowIndex'], ['needs_context' => true]), |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \InvalidArgumentException |
|
59
|
|
|
*/ |
|
60
|
8 |
|
public function getTokenParsers() |
|
61
|
|
|
{ |
|
62
|
|
|
return [ |
|
63
|
8 |
|
new AlignmentTokenParser([], HeaderFooterWrapper::ALIGNMENT_CENTER), |
|
64
|
8 |
|
new AlignmentTokenParser([], HeaderFooterWrapper::ALIGNMENT_LEFT), |
|
65
|
8 |
|
new AlignmentTokenParser([], HeaderFooterWrapper::ALIGNMENT_RIGHT), |
|
66
|
8 |
|
new CellTokenParser(), |
|
67
|
8 |
|
new DocumentTokenParser($this->attributes), |
|
68
|
8 |
|
new DrawingTokenParser(), |
|
69
|
8 |
|
new HeaderFooterTokenParser([], HeaderFooterWrapper::BASETYPE_FOOTER), |
|
70
|
8 |
|
new HeaderFooterTokenParser([], HeaderFooterWrapper::BASETYPE_HEADER), |
|
71
|
8 |
|
new RowTokenParser(), |
|
72
|
8 |
|
new SheetTokenParser(), |
|
73
|
|
|
]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
8 |
|
public function getNodeVisitors() |
|
80
|
|
|
{ |
|
81
|
|
|
return [ |
|
82
|
8 |
|
new MacroContextNodeVisitor(), |
|
83
|
8 |
|
new SyntaxCheckNodeVisitor(), |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param array $style1 |
|
89
|
|
|
* @param array $style2 |
|
90
|
|
|
* |
|
91
|
|
|
* @throws \Twig_Error_Runtime |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
public function mergeStyles(array $style1, array $style2): array |
|
96
|
|
|
{ |
|
97
|
|
|
if (!\is_array($style1) || !\is_array($style2)) { |
|
98
|
|
|
throw new \Twig_Error_Runtime('The xlsmergestyles function only works with arrays.'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return array_merge_recursive($style1, $style2); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param array $context |
|
106
|
|
|
* @return int|null |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getRowIndex(array $context) { |
|
109
|
|
|
return $context[PhpSpreadsheetWrapper::INSTANCE_KEY]->getSheetRow(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|