Skip to content

Commit 6d3137e

Browse files
committed
Use options system to allow future other options
1 parent 3a25196 commit 6d3137e

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

src/JsPhpize/Compiler/Compiler.php

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
class Compiler
2222
{
23+
const DOT_DISABLED = 1;
24+
2325
use DyiadeTrait;
2426
use InterpolationTrait;
2527

@@ -205,12 +207,12 @@ protected function visitDyiade(Dyiade $dyiade, $indent)
205207
return $leftHand . ' ' . $dyiade->operator . ' ' . $rightHand;
206208
}
207209

208-
protected function mapNodesArray($array, $indent, $pattern = null, $dotDisabled = false)
210+
protected function mapNodesArray($array, $indent, $pattern = null, $options = 0)
209211
{
210212
$visitNode = [$this, 'visitNode'];
211213

212-
return array_map(function ($value) use ($visitNode, $indent, $pattern, $dotDisabled) {
213-
$value = $visitNode($value, $indent, $dotDisabled);
214+
return array_map(function ($value) use ($visitNode, $indent, $pattern, $options) {
215+
$value = $visitNode($value, $indent, $options);
214216

215217
if ($pattern) {
216218
$value = sprintf($pattern, $value);
@@ -220,17 +222,23 @@ protected function mapNodesArray($array, $indent, $pattern = null, $dotDisabled
220222
}, $array);
221223
}
222224

223-
protected function visitNodesArray($array, $indent, $glue = '', $pattern = null, $dotDisabled = false)
225+
protected function visitNodesArray($array, $indent, $glue = '', $pattern = null, $options = 0)
224226
{
225-
return implode($glue, $this->mapNodesArray($array, $indent, $pattern, $dotDisabled));
227+
return implode($glue, $this->mapNodesArray($array, $indent, $pattern, $options));
226228
}
227229

228230
protected function visitFunctionCall(FunctionCall $functionCall, $indent)
229231
{
230232
$function = $functionCall->function;
231233
$arguments = $functionCall->arguments;
232234
$applicant = $functionCall->applicant;
233-
$arguments = $this->visitNodesArray($arguments, $indent, ', ', null, $function instanceof Variable && $function->name === 'isset');
235+
$arguments = $this->visitNodesArray(
236+
$arguments,
237+
$indent,
238+
', ',
239+
null,
240+
$function instanceof Variable && $function->name === 'isset' ? static::DOT_DISABLED : 0
241+
);
234242
$dynamicCall = $this->visitNode($function, $indent) . '(' . $arguments . ')';
235243

236244
if ($function instanceof Variable && count($function->children) === 0) {
@@ -286,14 +294,14 @@ protected function visitInstruction(Instruction $group, $indent)
286294
}, $group->instructions));
287295
}
288296

289-
public function visitNode(Node $node, $indent, $dotDisabled = false)
297+
public function visitNode(Node $node, $indent, $options = 0)
290298
{
291299
$method = preg_replace(
292300
'/^(.+\\\\)?([^\\\\]+)$/',
293301
'visit$2',
294302
get_class($node)
295303
);
296-
$php = method_exists($this, $method) ? $this->$method($node, $indent, $dotDisabled) : '';
304+
$php = method_exists($this, $method) ? $this->$method($node, $indent, $options) : '';
297305

298306
if ($node instanceof Value) {
299307
$php = $node->getBefore() . $php . $node->getAfter();
@@ -314,33 +322,41 @@ protected function visitTernary(Ternary $ternary, $indent)
314322
' : ' . $this->visitNode($ternary->falseValue, $indent);
315323
}
316324

317-
protected function handleVariableChildren(DynamicValue $dynamicValue, $indent, $php, $dotDisabled = false)
325+
protected function handleVariableChildren(DynamicValue $dynamicValue, $indent, $php, $options = 0)
318326
{
319327
$children = $dynamicValue->children;
320328

321329
if (count($children)) {
322-
$arguments = $this->mapNodesArray($children, $indent, null, $dotDisabled);
323-
array_unshift($arguments, $php);
324-
$dot = $this->engine->getHelperName('dot');
325-
326-
if ($dotDisabled) {
327-
$lastChild = end($children);
328-
$dotChild = $lastChild instanceof Constant && $lastChild->dotChild;
329-
$lastChild = array_pop($arguments);
330-
}
330+
return $this->wrapVariableChildren($children, $indent, $php, $options);
331+
}
331332

332-
$php = $this->helperWrap($dot, $arguments);
333+
return $php;
334+
}
333335

334-
if ($dotDisabled) {
335-
$pattern = $dotChild ? '%s->{%s}' : '%s[%s]';
336-
$php = sprintf($pattern, $php, $lastChild);
337-
}
336+
protected function wrapVariableChildren($children, $indent, $php, $options)
337+
{
338+
$arguments = $this->mapNodesArray($children, $indent);
339+
array_unshift($arguments, $php);
340+
$dot = $this->engine->getHelperName('dot');
341+
$dotDisabled = $options & static::DOT_DISABLED;
342+
343+
if ($dotDisabled) {
344+
$lastChild = end($children);
345+
$dotChild = $lastChild instanceof Constant && $lastChild->dotChild;
346+
$lastChild = array_pop($arguments);
347+
}
348+
349+
$php = $this->helperWrap($dot, $arguments);
350+
351+
if ($dotDisabled) {
352+
$pattern = $dotChild ? '%s->{%s}' : '%s[%s]';
353+
$php = sprintf($pattern, $php, $lastChild);
338354
}
339355

340356
return $php;
341357
}
342358

343-
protected function visitVariable(Variable $variable, $indent, $dotDisabled = false)
359+
protected function visitVariable(Variable $variable, $indent, $options = 0)
344360
{
345361
$name = $variable->name;
346362
if (in_array($name, ['Math', 'RegExp'])) {
@@ -353,7 +369,7 @@ protected function visitVariable(Variable $variable, $indent, $dotDisabled = fal
353369
$name = '$' . $name;
354370
}
355371

356-
return $this->handleVariableChildren($variable, $indent, $name, $dotDisabled);
372+
return $this->handleVariableChildren($variable, $indent, $name, $options);
357373
}
358374

359375
public function compile(Block $block, $indent = '')

0 commit comments

Comments
 (0)