This repository was archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecTestModel.php
158 lines (129 loc) · 3.85 KB
/
SpecTestModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Handlebars\Tests;
use Handlebars\Program;
use Handlebars\Opcode;
class SpecTestModel
{
public $suiteName;
public $number;
public $name;
public $description;
public $it;
public $template;
public $data;
public $helpers;
public $partials;
public $decorators;
public $exception;
public $message;
public $expected;
public $compileOptions;
public $options;
public $allOptions;
public $globalHelpers;
public $globalPartials;
public $globalDecorators;
public $opcodes;
public $partialOpcodes;
public $globalPartialOpcodes;
public function __construct(array $test)
{
foreach( $test as $k => $v ) {
$this->$k = $v;
}
// Patch data - @todo fix
$this->compileOptions['data'] = true;
}
public function getData()
{
return $this->convertCode($this->data);
}
public function getOptions()
{
return $this->convertCode($this->options);
}
public function getAllOptions()
{
return $this->convertCode(array_merge((array) $this->compileOptions, (array) $this->options));
}
public function getAllHelpers()
{
return $this->convertCode($this->merge($this->globalHelpers, $this->helpers));
}
public function getAllPartials()
{
return $this->convertCode($this->merge($this->globalPartials, $this->partials));
}
public function getAllDecorators()
{
return $this->convertCode($this->merge($this->globalDecorators, $this->decorators));
}
public function getOpcodes()
{
return $this->convertContext($this->opcodes);
}
public function getAllPartialOpcodes()
{
$partials = $this->merge($this->globalPartialOpcodes, $this->partialOpcodes);
foreach( $partials as $k => $v ) {
$partials[$k] = $this->convertContext($v);
}
return $partials;
}
private function merge($a, $b)
{
settype($a, 'array');
settype($b, 'array');
$b += $a;
return $b;
}
private function convertCode($data)
{
if( is_array($data) ) {
foreach( $data as $k => $v ) {
if( !is_array($v) ) {
continue;
}
if( !empty($v['!code']) ) {
$data[$k] = eval('use Handlebars\SafeString; use Handlebars\Utils; return ' . $v['php'] . ';');
} else if( !empty($v['!sparsearray']) ) {
unset($v['!sparsearray']);
$data[$k] = $v;
} else {
$data[$k] = $this->convertCode($data[$k]);
}
}
}
return $data;
}
private function convertOpcode(array $opcode)
{
return new Opcode($opcode['opcode'], $opcode['args']);
}
private function convertContext(array $context)
{
$opcodes = array();
foreach( $context['opcodes'] as $opcode ) {
$opcodes[] = $this->convertOpcode($opcode);
}
$children = array();
foreach( $context['children'] as $k => $v ) {
$children[$k] = $this->convertContext($v);
}
$decorators = null;
if( isset($context['decorators']) ) {
foreach ($context['decorators'] as $k => $v) {
$decorators[$k] = $this->convertContext($v);
}
}
$blockParams = isset($context['blockParams']) ? $context['blockParams'] : null;
$obj = new Program($opcodes, $children, $blockParams);
$obj->decorators = $decorators;
foreach( array('useDepths', 'usePartial', 'useDecorators', 'isSimple', 'options', 'compileOptions') as $k ) {
if( !empty($context[$k]) ) {
$obj->$k = $context[$k];
}
}
return $obj;
}
}