Skip to content

Commit 11bcd82

Browse files
author
Pia Sinzig
committed
SHODEVS2-122 add second test case (child component with properties)
1 parent 43a0e94 commit 11bcd82

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

src/Compiler.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,32 @@ public function convertNode(DOMNode $node): DOMNode
7373

7474
if (in_array($node->nodeName, array_keys($this->components))) {
7575
$currentComponent = $this->components[$node->nodeName];
76-
$include = $this->document->createTextNode('{% include "'.$currentComponent->getPath().'" %}');
76+
if ($node->hasAttributes()) {
77+
/** @var DOMAttr $attribute */
78+
foreach ($node->attributes as $attribute) {
79+
if (strpos($attribute->name, 'v-bind:') === 0 || strpos($attribute->name, ':') === 0) {
80+
$currentComponent->addProperty($attribute->name, $attribute->value, true);
81+
} else {
82+
$currentComponent->addProperty($attribute->name, $attribute->value, false);
83+
}
84+
}
85+
}
86+
$props = [];
87+
if (count($currentComponent->getProperties()) > 0) {
88+
foreach ($currentComponent->getProperties() as $property) {
89+
if ($property->isBinding()){
90+
$propName = substr($property->getName(), 1); //delete ':'
91+
$props[] = $propName.': '.$property->getValue();
92+
} else {
93+
$props[] = $property->getName().': "'.$property->getValue().'"';
94+
}
95+
}
96+
}
97+
$propsString = '';
98+
if (count($props) > 0) {
99+
$propsString = 'with { '.implode(', ', $props).' } ';
100+
}
101+
$include = $this->document->createTextNode('{% include "'.$currentComponent->getPath().'" '.$propsString.'%}');
77102
$node->parentNode->insertBefore($include, $node);
78103
$node->parentNode->removeChild($node);
79104
}

src/Component.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class Component
1919
*/
2020
protected $path;
2121

22+
/** @var Property[] */
23+
protected $properties = [];
24+
2225
public function __construct(string $name = '', string $path = '')
2326
{
2427
$this->name = $name;
@@ -33,6 +36,14 @@ public function getPath(){
3336
return $this->path;
3437
}
3538

39+
/**
40+
* @return Property[]
41+
*/
42+
public function getProperties(): array
43+
{
44+
return $this->properties;
45+
}
46+
3647
/**
3748
* @param string $fileName
3849
*
@@ -51,8 +62,13 @@ public function loadFile(string $fileName): self
5162
return $this;
5263
}
5364

54-
public function registerComponents($name, $path)
65+
public function registerComponents(string $name, string $path)
5566
{
5667
$this->components[$name] = $path;
5768
}
69+
70+
public function addProperty(string $name, string $value, bool $isBinding = false) {
71+
$this->properties[] = new Property($name, $value, $isBinding);
72+
}
73+
5874
}

src/Property.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Paneon\VueToTwig;
4+
5+
class Property
6+
{
7+
/**
8+
* @var string
9+
*/
10+
protected $name;
11+
/**
12+
* @var string
13+
*/
14+
protected $value;
15+
/**
16+
* @var bool
17+
*/
18+
protected $isBinding;
19+
20+
public function __construct(string $name, string $value, bool $isBinding)
21+
{
22+
$this->name = $name;
23+
$this->value = $value;
24+
$this->isBinding = $isBinding;
25+
}
26+
27+
/**
28+
* @return string
29+
*/
30+
public function getName(): string
31+
{
32+
return $this->name;
33+
}
34+
35+
/**
36+
* @return string
37+
*/
38+
public function getValue(): string
39+
{
40+
return $this->value;
41+
}
42+
43+
/**
44+
* @return bool
45+
*/
46+
public function isBinding(): bool
47+
{
48+
return $this->isBinding;
49+
}
50+
51+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
{% include "/templates/ChildComponent.twig" with { product: testProduct, string: "Test" } %}
3+
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<template>
2+
<div>
3+
<ChildComponent :product="testProduct" string="Test" />
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
name: 'ComponentWithPropVue',
10+
};
11+
</script>

0 commit comments

Comments
 (0)