Skip to content

Commit e2d3156

Browse files
authored
Merge pull request #14 from 8fold/custom-properties-in-doctype
feature: Users can set version, encoding, and standalone
2 parents 7e6c4ad + bd2feb5 commit e2d3156

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

src/Document.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,46 @@ class Document implements Buildable, Contentable
1919
use BuildableImp;
2020
use ContentableImp;
2121

22+
private string $version = '1.0';
23+
24+
private string $encoding = 'UTF-8';
25+
26+
private string $standalone = 'yes';
27+
2228
final private function __construct(
2329
private string $name,
2430
string|Stringable ...$content
2531
) {
2632
$this->content = $content;
2733
}
2834

35+
public function setVersion(string|float|int $version): self
36+
{
37+
if (is_int($version)) {
38+
$version = strval($version) . '.0';
39+
}
40+
$this->version = strval($version);
41+
return $this;
42+
}
43+
44+
public function setEncoding(string $encoding): self
45+
{
46+
$this->encoding = $encoding;
47+
return $this;
48+
}
49+
50+
public function setStandalone(bool $standalone = true): self
51+
{
52+
$this->standalone = ($standalone) ? 'yes' : 'no';
53+
return $this;
54+
}
55+
2956
public function __toString(): string
3057
{
3158
$doctype =
32-
'<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>'
59+
'<?xml version = "' . $this->version .
60+
'" encoding = "' . $this->encoding .
61+
'" standalone = "' . $this->standalone . '" ?>'
3362
. "\n";
3463
return $doctype . $this->opening() . implode('', $this->content)
3564
. $this->closing();

tests/DocumentBaselineTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,39 @@
1313

1414
class DocumentBaselineTest extends TestCase
1515
{
16+
/**
17+
* @test
18+
*/
19+
public function document_can_modify_doctype_properties(): void
20+
{
21+
$this->assertSame(
22+
'<?xml version = "1.1" encoding = "UTF-16" standalone = "no" ?>' . "\n" .
23+
'<root></root>',
24+
(string) Document::create('root')
25+
->setVersion(1.1)
26+
->setEncoding('UTF-16')
27+
->setStandalone(false)
28+
);
29+
30+
$this->assertSame(
31+
'<?xml version = "1.1" encoding = "UTF-16" standalone = "no" ?>' . "\n" .
32+
'<root></root>',
33+
(string) Document::create('root')
34+
->setVersion('1.1')
35+
->setEncoding('UTF-16')
36+
->setStandalone(false)
37+
);
38+
39+
$this->assertSame(
40+
'<?xml version = "1.0" encoding = "UTF-16" standalone = "no" ?>' . "\n" .
41+
'<root></root>',
42+
(string) Document::create('root')
43+
->setVersion(1)
44+
->setEncoding('UTF-16')
45+
->setStandalone(false)
46+
);
47+
}
48+
1649
/**
1750
*@test
1851
*/

tests/PerformanceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function document_is_speedy(): void
6262
$elapsed = $end - $start;
6363
$ms = $elapsed/1e+6;
6464

65-
$this->assertLessThan(0.1, $ms);
65+
$this->assertLessThan(0.2, $ms);
6666
}
6767

6868
/**

0 commit comments

Comments
 (0)