Repository navigation
Expand file tree
/
Copy pathDocument.php
More file actions
129 lines (108 loc) · 2.68 KB
/
Copy pathDocument.php
File metadata and controls
129 lines (108 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
namespace Eightfold\HtmlBuilder;
use Stringable;
use Eightfold\HtmlBuilder\Element;
use Eightfold\HtmlBuilder\EmptyElement;
class Document implements Stringable
{
/**
* @var array<string>
*/
private array $htmlProps = [];
/**
* @var array<string|Stringable>
*/
private array $head = [];
/**
* @var array<string|Stringable>
*/
private array $body = [];
/**
* @var array<string>
*/
private array $bodyProps = [];
public static function create(
string|Stringable $title,
string $lang = 'en',
string $charset = 'utf-8'
): Document {
return new static($title, $lang, $charset);
}
final private function __construct(
private string|Stringable $title,
private string $lang,
private string $charset
) {
}
public function htmlProps(string ...$props): Document
{
$this->htmlProps = $props;
return $this;
}
public function head(string|Stringable ...$content): Document
{
$this->head = $content;
return $this;
}
public function body(string|Stringable ...$content): Document
{
$this->body = $content;
return $this;
}
public function bodyProps(string ...$props): Document
{
$this->bodyProps = $props;
return $this;
}
public function toString(): string
{
$pageTitle = $this->title();
if (
is_string($pageTitle) and
str_starts_with($pageTitle, '<title>') === false
) {
$pageTitle = Element::title($this->title());
}
$doctype = '<!doctype html>' . "\n";
$html = (string) Element::html(
Element::head(
$pageTitle,
EmptyElement::meta($this->charset()),
...$this->headContent()
),
Element::body(...$this->bodyContent())->props(...$this->bodyProps)
)->props($this->lang(), ...$this->htmlProps);
return $doctype . $html;
}
public function __toString(): string
{
return $this->toString();
}
private function title(): string|Stringable
{
return $this->title;
}
private function lang(): string
{
return 'lang ' . $this->lang;
}
private function charset(): string
{
return 'charset ' . $this->charset;
}
/**
* @return array<string|Stringable>
*/
private function headContent(): array
{
return $this->head;
}
/**
* @return array<string|Stringable>
*/
private function bodyContent(): array
{
return $this->body;
}
}