Skip to content

Commit f591ece

Browse files
committed
Rename properties of partials to camelCase
1 parent 6ccc2ef commit f591ece

File tree

8 files changed

+59
-50
lines changed

8 files changed

+59
-50
lines changed

src/Compiler.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,21 @@ public function convertNode(DOMNode $node): DOMNode
101101
break;
102102
}
103103

104+
$this->handleFor($node);
105+
$this->stripEventHandlers($node);
106+
//$this->handleRawHtml($node, $data);
107+
104108
if (in_array($node->nodeName, array_keys($this->components))) {
105109
$currentComponent = $this->components[$node->nodeName];
106-
$this->handleIf($node);
107-
$this->handleFor($node);
108110

109111
if ($node->hasAttributes()) {
110112
/** @var DOMAttr $attribute */
111113
foreach ($node->attributes as $attribute) {
112114
if (strpos($attribute->name, 'v-bind:') === 0 || strpos($attribute->name, ':') === 0) {
113115
$name = substr($attribute->name, strpos($attribute->name, ':') + 1);
114-
$currentComponent->addProperty($name, $attribute->value, true);
116+
$value = $this->refactorTemplateString($attribute->value);
117+
118+
$currentComponent->addProperty($name, $value, true);
115119
} else {
116120
$currentComponent->addProperty($attribute->name, '"'.$attribute->value.'"', false);
117121
}
@@ -127,13 +131,10 @@ public function convertNode(DOMNode $node): DOMNode
127131

128132
$node->parentNode->insertBefore($include, $node);
129133
$node->parentNode->removeChild($node);
134+
130135
return $node;
131136
}
132137

133-
$this->stripEventHandlers($node);
134-
$this->handleFor($node);
135-
//$this->handleRawHtml($node, $data);
136-
137138
$this->handleAttributeBinding($node);
138139

139140
foreach (iterator_to_array($node->childNodes) as $childNode) {
@@ -222,7 +223,7 @@ private function handleAttributeBinding(DOMElement $node)
222223

223224
$node->setAttribute($name, $templateStringContent);
224225
} else {
225-
$this->logger->warning('- No Handling for: '.$value);
226+
$this->logger->debug('- No Handling for: '.$value);
226227
}
227228

228229
$this->logger->debug('=> remove original '.$attribute->name);
@@ -422,4 +423,18 @@ protected function addBanner(string $html)
422423

423424
return $html;
424425
}
426+
427+
public function refactorTemplateString($value)
428+
{
429+
if (preg_match('/^`(?P<content>.+)`$/', $value, $matches)) {
430+
$templateStringContent = '"'.$matches['content'].'"';
431+
$value = preg_replace(
432+
'/\$\{(.+)\}/',
433+
'{{ $1 }}',
434+
$templateStringContent
435+
);
436+
}
437+
438+
return $value;
439+
}
425440
}

src/Component.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,27 @@ public function getProperties(): array
4444
return $this->properties;
4545
}
4646

47-
/**
48-
* @param string $fileName
49-
*
50-
* @return Component
51-
*/
52-
public function loadFile(string $fileName): self
53-
{
54-
$this->fileName = $fileName;
55-
56-
@$this->document->loadHTMLFile($this->assetPath . $fileName);
57-
$this->templateElement = $this->document->getElementsByTagName('template')->item(0);
58-
59-
$this->rootElement = $this->getRootNode($this->templateElement);
60-
$this->templateHtml = $this->getInnerHtml($this->templateElement);
61-
62-
return $this;
63-
}
64-
6547
public function registerComponents(string $name, string $path)
6648
{
6749
$this->components[$name] = $path;
6850
}
6951

7052
public function addProperty(string $name, string $value, bool $isBinding = false) {
71-
$this->properties[] = new Property($name, $value, $isBinding);
53+
$this->properties[] = new Property(
54+
$this->kebabToCamelCase($name),
55+
$value,
56+
$isBinding
57+
);
7258
}
7359

60+
public function kebabToCamelCase($string, $capitalizeFirstCharacter = false)
61+
{
62+
$str = str_replace('-', '', ucwords($string, '-'));
63+
64+
if (!$capitalizeFirstCharacter) {
65+
$str = lcfirst($str);
66+
}
67+
68+
return $str;
69+
}
7470
}

src/Utils/TwigBuilder.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,29 @@ public function createIncludePartial(string $partialPath, array $variables = [])
106106
return $this->createBlock('include "'.$partialPath.'"');
107107
}
108108

109+
$serializedProperties = $this->serializeComponentProperties($variables);
110+
111+
return $this->createBlock('include "'.$partialPath.'" with '.$serializedProperties);
112+
}
113+
114+
/**
115+
* @param Property[] $properties
116+
* @return string
117+
*/
118+
public function serializeComponentProperties(array $properties): string
119+
{
109120
$props = [];
110121

111122
/** @var Property $property */
112-
foreach ($variables as $property) {
113-
if($property->getName() !== 'key') {
114-
$value = $this->checkPropertyValue($property->getValue());
115-
$props[] = $property->getName().': '.$value;
123+
foreach ($properties as $property) {
124+
if($property->getName() === 'key') {
125+
continue;
116126
}
117-
}
118127

119-
return $this->createBlock('include "'.$partialPath.'" with { '.implode(', ', $props).' }');
120-
}
121-
122-
public function checkPropertyValue($value)
123-
{
124-
if (preg_match('/^`(?P<content>.+)`$/', $value, $matches)) {
125-
$templateStringContent = '"'.$matches['content'].'"';
126-
$value = preg_replace(
127-
'/\$\{(.+)\}/',
128-
'{{ $1 }}',
129-
$templateStringContent
130-
);
128+
$props[] = '\''.$property->getName().'\''.': '.$property->getValue();
131129
}
132130

133-
return $value;
131+
return '{ '.implode(', ', $props).' }';
134132
}
135133

136134
public function refactorCondition(string $condition): string
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
{% include "/templates/ChildComponent.twig" with { style: "fill:{{ color }};", test: "test" } %}
2+
{% include "/templates/ChildComponent.twig" with { 'style': "fill:{{ color }};", 'test': "test" } %}
33
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
{% for a in listOfProducts %}{% include "/templates/ChildComponent.twig" with { string: "string", product: productA } %}{% endfor %}
2+
{% for a in listOfProducts %}{% include "/templates/ChildComponent.twig" with { 'string': "string", 'product': productA } %}{% endfor %}
33
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
{% if a > 5 %}{% include "/templates/ChildComponent.twig" with { string: "string", product: productA } %}{% endif %}
2+
{% if a > 5 %}{% include "/templates/ChildComponent.twig" with { 'string': "string", 'product': productA } %}{% endif %}
33
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
{% include "/templates/ChildComponent.twig" with { product: testProduct, string: "Test" } %}
2+
{% include "/templates/ChildComponent.twig" with { 'product': testProduct, 'testA': "Hello", 'string': "Test" } %}
33
</div>

tests/fixtures/vue-component/component-with-prop.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div>
3-
<ChildComponent :product="testProduct" string="Test" />
3+
<ChildComponent :product="testProduct" test-a="Hello" string="Test" />
44
</div>
55
</template>
66

0 commit comments

Comments
 (0)