Skip to content

Commit 1d6f197

Browse files
authored
Merge pull request #15 from 8fold/custom-properties-in-doctype
Custom properties in doctype
2 parents e2d3156 + 28f1dac commit 1d6f197

File tree

6 files changed

+100
-50
lines changed

6 files changed

+100
-50
lines changed

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<description>PHP-FIG PSR-12 standards with modifications to: control structure (empty space before closing brace).</description>
44

55
<file>src</file>
6+
<file>tests</file>
67

78
<arg name="colors" />
89

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Eightfold\XMLBuilder\Callables;
5+
6+
class PropertyArrayToString
7+
{
8+
public static function convert(string ...$properties): string
9+
{
10+
$instance = new self();
11+
return $instance(...$properties);
12+
}
13+
14+
public function __invoke(string ...$properties): string
15+
{
16+
$b = [];
17+
foreach ($properties as $property) {
18+
$property = explode(' ', $property, 2);
19+
$b[] = $property[0] . '="' . $property[1] . '"';
20+
21+
}
22+
23+
return ' ' . implode(' ', $b);
24+
}
25+
}

src/Document.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Stringable;
88

9+
use Eightfold\XMLBuilder\Callables\PropertyArrayToString;
10+
911
use Eightfold\XMLBuilder\Contracts\Buildable;
1012
use Eightfold\XMLBuilder\Contracts\Contentable;
1113

@@ -47,18 +49,39 @@ public function setEncoding(string $encoding): self
4749
return $this;
4850
}
4951

52+
public function removeEncoding(): self
53+
{
54+
$this->encoding = '';
55+
return $this;
56+
}
57+
5058
public function setStandalone(bool $standalone = true): self
5159
{
5260
$this->standalone = ($standalone) ? 'yes' : 'no';
5361
return $this;
5462
}
5563

64+
public function removeStandalone(): self
65+
{
66+
$this->standalone = '';
67+
return $this;
68+
}
69+
5670
public function __toString(): string
5771
{
72+
$declarationProps = [];
73+
$declarationProps[] = 'version ' . $this->version;
74+
75+
if (strlen($this->encoding) > 0) {
76+
$declarationProps[] = 'encoding ' . $this->encoding;
77+
}
78+
79+
if (strlen($this->standalone) > 0) {
80+
$declarationProps[] = 'standalone ' . $this->standalone;
81+
}
82+
5883
$doctype =
59-
'<?xml version = "' . $this->version .
60-
'" encoding = "' . $this->encoding .
61-
'" standalone = "' . $this->standalone . '" ?>'
84+
'<?xml' . PropertyArrayToString::convert(...$declarationProps) . ' ?>'
6285
. "\n";
6386
return $doctype . $this->opening() . implode('', $this->content)
6487
. $this->closing();

src/Implementations/Properties.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Eightfold\XMLBuilder\Implementations;
66

7+
use Eightfold\XMLBuilder\Callables\PropertyArrayToString;
8+
79
trait Properties
810
{
911
/**
@@ -28,18 +30,11 @@ public function prop(string $prop): static
2830

2931
protected function propertiesString(): string
3032
{
31-
if (count($this->properties()) === 0) {
33+
$props = $this->properties();
34+
if (count($props) === 0) {
3235
return '';
3336
}
34-
35-
$b = [];
36-
foreach ($this->properties() as $property) {
37-
$property = explode(' ', $property, 2);
38-
$b[] = $property[0] . '="' . $property[1] . '"';
39-
40-
}
41-
42-
return ' ' . implode(' ', $b);
37+
return PropertyArrayToString::convert(...$props);
4338
}
4439

4540
/**

tests/DocumentBaselineTest.php

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,23 @@ class DocumentBaselineTest extends TestCase
1616
/**
1717
* @test
1818
*/
19-
public function document_can_modify_doctype_properties(): void
19+
public function document_can_remove_optional_declaration_props(): void
2020
{
2121
$this->assertSame(
22-
'<?xml version = "1.1" encoding = "UTF-16" standalone = "no" ?>' . "\n" .
22+
'<?xml version="1.0" ?>' . "\n" . '<root></root>',
23+
(string) Document::create('root')
24+
->removeEncoding()
25+
->removeStandalone()
26+
);
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function document_can_modify_declaration_properties(): void
33+
{
34+
$this->assertSame(
35+
'<?xml version="1.1" encoding="UTF-16" standalone="no" ?>' . "\n" .
2336
'<root></root>',
2437
(string) Document::create('root')
2538
->setVersion(1.1)
@@ -28,7 +41,7 @@ public function document_can_modify_doctype_properties(): void
2841
);
2942

3043
$this->assertSame(
31-
'<?xml version = "1.1" encoding = "UTF-16" standalone = "no" ?>' . "\n" .
44+
'<?xml version="1.1" encoding="UTF-16" standalone="no" ?>' . "\n" .
3245
'<root></root>',
3346
(string) Document::create('root')
3447
->setVersion('1.1')
@@ -37,7 +50,7 @@ public function document_can_modify_doctype_properties(): void
3750
);
3851

3952
$this->assertSame(
40-
'<?xml version = "1.0" encoding = "UTF-16" standalone = "no" ?>' . "\n" .
53+
'<?xml version="1.0" encoding="UTF-16" standalone="no" ?>' . "\n" .
4154
'<root></root>',
4255
(string) Document::create('root')
4356
->setVersion(1)
@@ -51,9 +64,9 @@ public function document_can_modify_doctype_properties(): void
5164
*/
5265
public function document_is_stringable(): void
5366
{
54-
$this->assertEquals(
55-
(string) Document::create('root')->props('id 6', 'property hello'),
56-
'<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>'."\n".'<root id="6" property="hello"></root>'
67+
$this->assertSame(
68+
'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n".'<root id="6" property="hello"></root>',
69+
(string) Document::create('root')->props('id 6', 'property hello')
5770
);
5871
}
5972

@@ -62,17 +75,17 @@ public function document_is_stringable(): void
6275
*/
6376
public function document_can_have_comment(): void
6477
{
65-
$this->assertEquals(
78+
$this->assertSame(
79+
'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>' .
80+
"\n" .
81+
'<root>' . "\n" .
82+
'<!-- comment -->' . "\n" .
83+
'<tag /></root>',
6684
Document::create(
6785
'root',
6886
Comment::create('comment'),
6987
Element::create('tag')->omitEndTag()
70-
)->build(),
71-
'<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>' .
72-
"\n" .
73-
'<root>' . "\n" .
74-
'<!-- comment -->' . "\n" .
75-
'<tag /></root>'
88+
)->build()
7689
);
7790
}
7891

@@ -81,9 +94,9 @@ public function document_can_have_comment(): void
8194
*/
8295
public function document_can_have_properties(): void
8396
{
84-
$this->assertEquals(
85-
Document::create('root')->props('id 6', 'property hello')->build(),
86-
'<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>'."\n".'<root id="6" property="hello"></root>'
97+
$this->assertSame(
98+
'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n".'<root id="6" property="hello"></root>',
99+
Document::create('root')->props('id 6', 'property hello')->build()
87100
);
88101
}
89102

@@ -92,14 +105,14 @@ public function document_can_have_properties(): void
92105
*/
93106
public function document_can_use_shorthand(): void
94107
{
95-
$this->assertEquals(
108+
$this->assertSame(
109+
'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n".'<root><child><grandchild></grandchild><![CDATA[String]]></child></root>',
96110
Document::root(
97111
Element::child(
98112
Element::grandchild(),
99113
Cdata::create('String')
100114
)
101-
)->build(),
102-
'<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>'."\n".'<root><child><grandchild></grandchild><![CDATA[String]]></child></root>'
115+
)->build()
103116
);
104117
}
105118

@@ -108,14 +121,15 @@ public function document_can_use_shorthand(): void
108121
*/
109122
public function document_can_accept_content(): void
110123
{
111-
$this->assertEquals(
112-
Document::create('root',
124+
$this->assertSame(
125+
'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n".'<root><child><grandchild></grandchild><![CDATA[String]]></child></root>',
126+
Document::create(
127+
'root',
113128
Element::create('child',
114129
Element::create('grandchild'),
115130
Cdata::create('String')
116131
)
117-
)->build(),
118-
'<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>'."\n".'<root><child><grandchild></grandchild><![CDATA[String]]></child></root>'
132+
)->build()
119133
);
120134
}
121135

@@ -124,17 +138,9 @@ public function document_can_accept_content(): void
124138
*/
125139
public function document_can_initialized_statically(): void
126140
{
127-
$this->assertEquals(
128-
Document::create('tag')->build(),
129-
'<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>'."\n".'<tag></tag>'
141+
$this->assertSame(
142+
'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n".'<tag></tag>',
143+
Document::create('tag')->build()
130144
);
131145
}
132-
133-
/**
134-
*@test
135-
*/
136-
public function document_exists(): void
137-
{
138-
$this->assertTrue(class_exists(Document::class));
139-
}
140146
}

tests/PerformanceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function document_is_speedy(): void
4343
$end = hrtime(true);
4444

4545
$expected = <<<doc
46-
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
46+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
4747
<root><item>Hello, 0!</item><item>Hello, 1!</item><item>Hello, 2!</item>
4848
<!-- 3 -->
4949
<item>Hello, 3!</item>
@@ -95,7 +95,7 @@ public function document_is_small(): void
9595
$end = memory_get_usage();
9696

9797
$expected = <<<doc
98-
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
98+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
9999
<root><item>Hello, 0!</item><item>Hello, 1!</item><item>Hello, 2!</item>
100100
<!-- 3 -->
101101
<item>Hello, 3!</item>

0 commit comments

Comments
 (0)