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