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