Skip to content

Commit 0270690

Browse files
committed
Added namespace and more attributes methods in the component class.
1 parent ebd295a commit 0270690

File tree

4 files changed

+48
-29
lines changed

4 files changed

+48
-29
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"autoload": {
1717
"psr-4": {
18-
"": "src/"
18+
"IvoPetkov\\": "src/"
1919
}
2020
}
2121
}

src/HTMLServerComponent.php

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* Free to use under the MIT license.
88
*/
99

10+
namespace IvoPetkov;
11+
1012
/**
1113
* The class that is used to send data to the component
1214
*/
@@ -28,58 +30,75 @@ class HTMLServerComponent
2830
/**
2931
*
3032
* @param string $name
33+
* @param string|null $defaultValue
3134
* @return string|null
3235
*/
33-
function __get($name)
36+
public function getAttribute($name, $defaultValue = null)
3437
{
35-
return $this->getAttribute($name);
38+
$name = strtolower($name);
39+
return isset($this->attributes[$name]) ? (string) $this->attributes[$name] : $defaultValue;
3640
}
3741

3842
/**
3943
*
4044
* @param string $name
4145
* @param string $value
42-
* @return void
4346
*/
44-
function __set($name, $value)
47+
public function setAttribute($name, $value)
4548
{
4649
$this->attributes[strtolower($name)] = $value;
4750
}
4851

4952
/**
5053
*
5154
* @param string $name
52-
* @return boolean
5355
*/
54-
function __isset($name)
56+
public function removeAttribute($name)
5557
{
56-
return isset($this->attributes[strtolower($name)]);
58+
$name = strtolower($name);
59+
if (isset($this->attributes[$name])) {
60+
unset($this->attributes[$name]);
61+
}
5762
}
5863

5964
/**
6065
*
6166
* @param string $name
67+
* @return string|null
6268
*/
63-
function __unset($name)
69+
function __get($name)
6470
{
65-
$name = strtolower($name);
66-
if (isset($this->attributes[$name])) {
67-
unset($this->attributes[$name]);
68-
}
71+
return $this->getAttribute($name);
6972
}
7073

7174
/**
7275
*
7376
* @param string $name
74-
* @param string|null $defaultValue
75-
* @return string|null
77+
* @param string $value
78+
* @return void
7679
*/
77-
public function getAttribute($name, $defaultValue = null)
80+
function __set($name, $value)
7881
{
79-
$name = strtolower($name);
80-
return isset($this->attributes[$name]) ? (string) $this->attributes[$name] : ($defaultValue === null ? null : (string) $defaultValue);
82+
$this->setAttribute($name, $value);
83+
}
84+
85+
/**
86+
*
87+
* @param string $name
88+
* @return boolean
89+
*/
90+
function __isset($name)
91+
{
92+
return isset($this->attributes[strtolower($name)]);
93+
}
94+
95+
/**
96+
*
97+
* @param string $name
98+
*/
99+
function __unset($name)
100+
{
101+
$this->removeAttribute($name);
81102
}
82-
83-
// doto set attbiute
84103

85104
}

src/HTMLServerComponentsCompiler.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* Free to use under the MIT license.
88
*/
99

10+
namespace IvoPetkov;
11+
1012
/**
1113
* The class that processes components
1214
*/
@@ -16,7 +18,7 @@ class HTMLServerComponentsCompiler
1618
/**
1719
*
1820
*/
19-
const VERSION = '0.2.3';
21+
const VERSION = '0.3.0';
2022

2123
/**
2224
*
@@ -41,16 +43,14 @@ function addAlias($alias, $original)
4143
*/
4244
public function process($html)
4345
{
44-
$domDocument = new IvoPetkov\HTML5DOMDocument();
46+
$domDocument = new \IvoPetkov\HTML5DOMDocument();
4547
$domDocument->loadHTML($html);
4648
$componentElements = $domDocument->getElementsByTagName('component');
4749
$componentElementsCount = $componentElements->length;
4850
if ($componentElementsCount > 0) {
4951
for ($i = 0; $i < $componentElementsCount; $i++) {
5052
$component = $componentElements->item(0);
51-
5253
$attributes = $component->getAttributes();
53-
5454
if (isset($attributes['src'])) {
5555
$srcAttributeValue = $attributes['src'];
5656
if (isset($this->aliases[$srcAttributeValue])) {
@@ -115,11 +115,11 @@ public function processFile($file, $attributes = [], $innerHTML = '')
115115
*
116116
* @param array $attributes
117117
* @param string $innerHTML
118-
* @return \HTMLServerComponent
118+
* @return \IvoPetkov\HTMLServerComponent
119119
*/
120120
protected function constructComponent($attributes = [], $innerHTML = '')
121121
{
122-
$component = new HTMLServerComponent();
122+
$component = new \IvoPetkov\HTMLServerComponent();
123123
$component->attributes = $attributes;
124124
$component->innerHTML = $innerHTML;
125125
return $component;
@@ -128,7 +128,7 @@ protected function constructComponent($attributes = [], $innerHTML = '')
128128
/**
129129
*
130130
* @param string $file
131-
* @param HTMLServerComponent $component
131+
* @param \IvoPetkov\HTMLServerComponent $component
132132
* @throws \Exception
133133
* @return string
134134
*/

tests/Test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public function testProccessHTML()
2020
{
2121
$fullFilename = $this->createFile('component1.php', '<html><head><meta custom="value"></head><body>text1</body></html>');
2222

23-
$compiler = new HTMLServerComponentsCompiler();
23+
$compiler = new \IvoPetkov\HTMLServerComponentsCompiler();
2424
$result = $compiler->process('<component src="file:' . $fullFilename . '"/>');
2525
$expectedResult = '<!DOCTYPE html>' . "\n" . '<html><head><meta custom="value"></head><body>text1</body></html>';
2626
$this->assertTrue($result === $expectedResult);
2727

28-
$compiler = new HTMLServerComponentsCompiler();
28+
$compiler = new \IvoPetkov\HTMLServerComponentsCompiler();
2929
$result = $compiler->process('<html><body>'
3030
. 'text0'
3131
. '<component src="file:' . $fullFilename . '"/>'

0 commit comments

Comments
 (0)