Skip to content

Commit a095213

Browse files
author
Wazabii
committed
blocks
1 parent b6b2276 commit a095213

File tree

6 files changed

+211
-36
lines changed

6 files changed

+211
-36
lines changed

Dom/Document.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
namespace MaplePHP\Output\Dom;
1111

12-
class Document
12+
use MaplePHP\Output\Interfaces\DocumentInterface;
13+
use MaplePHP\Output\Interfaces\ElementInterface;
14+
15+
class Document implements DocumentInterface
1316
{
1417
public const TAG_NO_ENDING = [
1518
"meta",
@@ -30,6 +33,10 @@ class Document
3033
private $elem;
3134
private static $inst;
3235

36+
/**
37+
* Will output get
38+
* @return string
39+
*/
3340
public function __toString(): string
3441
{
3542
return $this->get();
@@ -39,7 +46,7 @@ public function __toString(): string
3946
* Get get Dom/document (Will only trigger execute once per instance)
4047
* @return string
4148
*/
42-
public function get()
49+
public function get(): string
4350
{
4451
if (is_null($this->html)) {
4552
$this->execute();
@@ -52,7 +59,7 @@ public function get()
5259
* @param string $key DOM access key
5360
* @return self
5461
*/
55-
public static function dom(string $key)
62+
public static function dom(string $key): self
5663
{
5764
if (empty(self::$inst[$key])) {
5865
self::$inst[$key] = new self();
@@ -65,9 +72,9 @@ public static function dom(string $key)
6572
* @param string $tag HTML tag (without brackets)
6673
* @param string $key Bind tag to key
6774
* @param bool|boolean $prepend Prepend instead of append
68-
* @return self
75+
* @return ElementInterface
6976
*/
70-
public function bindTag(string $tag, string $key, bool $prepend = false)
77+
public function bindTag(string $tag, string $key, bool $prepend = false): ElementInterface
7178
{
7279
if ($prepend) {
7380
$this->elem = $this->createPrepend($tag, null, $key);
@@ -81,9 +88,9 @@ public function bindTag(string $tag, string $key, bool $prepend = false)
8188
* Create (append) element
8289
* @param string $element HTML tag (without brackets)
8390
* @param string $value add value to tag
84-
* @return self
91+
* @return ElementInterface
8592
*/
86-
public function create($element, $value = null, ?string $bind = null)
93+
public function create($element, $value = null, ?string $bind = null): ElementInterface
8794
{
8895
$inst = new Element($element, $value);
8996

@@ -100,9 +107,9 @@ public function create($element, $value = null, ?string $bind = null)
100107
* Prepend element first
101108
* @param string $element HTML tag (without brackets)
102109
* @param string $value add value to tag
103-
* @return self
110+
* @return ElementInterface
104111
*/
105-
public function createPrepend(string $element, ?string $value = null, ?string $bind = null)
112+
public function createPrepend(string $element, ?string $value = null, ?string $bind = null): ElementInterface
106113
{
107114
$inst = new Element($element, $value);
108115
if (is_null($this->elements)) {
@@ -120,9 +127,9 @@ public function createPrepend(string $element, ?string $value = null, ?string $b
120127

121128
/**
122129
* Get one element from key
123-
* @return Element|null
130+
* @return ElementInterface|null
124131
*/
125-
public function getElement(string $key): ?Element
132+
public function getElement(string $key): ?ElementInterface
126133
{
127134
return ($this->elements[$key] ?? null);
128135
}
@@ -131,7 +138,7 @@ public function getElement(string $key): ?Element
131138
* Get all elements
132139
* @return array
133140
*/
134-
public function getElements()
141+
public function getElements(): array
135142
{
136143
return $this->elements;
137144
}
@@ -151,7 +158,7 @@ public function getTag(string $key): ?string
151158
* @param callable|null $call Can be used to manipulate element within feed
152159
* @return string
153160
*/
154-
public function execute(?callable $call = null)
161+
public function execute(?callable $call = null): string
155162
{
156163
$this->html = "";
157164
if (is_null($this->elements)) {

Dom/Element.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

1111
namespace MaplePHP\Output\Dom;
1212

13+
use MaplePHP\Output\Interfaces\ElementInterface;
1314
use BadMethodCallException;
1415

15-
class Element extends Document
16+
class Element extends Document implements ElementInterface
1617
{
1718
private $elem;
1819
private $attr = array();
@@ -91,7 +92,7 @@ protected function hideTagValid(): bool
9192
* @param string $sep
9293
* @return self
9394
*/
94-
public function attrAdd(string $key, string $value, string $sep = " "): self
95+
public function addAttr(string $key, string $value, string $sep = " "): self
9596
{
9697
if (isset($this->attr[$key])) {
9798
$this->attr[$key] .= "{$sep}{$value}";
@@ -101,12 +102,6 @@ public function attrAdd(string $key, string $value, string $sep = " "): self
101102
return $this;
102103
}
103104

104-
// Same as above
105-
public function attrAddTo(string $key, string $value, string $sep = " "): self
106-
{
107-
return $this->attrAdd($key, $value, $sep);
108-
}
109-
110105
/**
111106
* Set elem value <elem>[VALUE]</elem>
112107
* @param string|null null value can be used to auto skip HTML tag
@@ -136,6 +131,20 @@ public function getEl(): string
136131
return $this->elem;
137132
}
138133

134+
/**
135+
* With cloned element or new element if is specifed
136+
* @param string|null $elem
137+
* @return self
138+
*/
139+
public function withElement(?string $elem = null): self
140+
{
141+
$inst = clone $this;
142+
if (!is_null($elem)) {
143+
$inst->elem = $elem;
144+
}
145+
return $inst;
146+
}
147+
139148
/**
140149
* Array attr to string
141150
* @return string
@@ -153,18 +162,4 @@ protected function buildAttr(): string
153162
}
154163
return $attr;
155164
}
156-
157-
/**
158-
* With cloned element or new element if is specifed
159-
* @param string|null $elem
160-
* @return self
161-
*/
162-
public function withElement(?string $elem = null): self
163-
{
164-
$inst = clone $this;
165-
if (!is_null($elem)) {
166-
$inst->elem = $elem;
167-
}
168-
return $inst;
169-
}
170165
}

Interfaces/DocumentInterface.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace MaplePHP\Output\Interfaces;
4+
5+
interface DocumentInterface
6+
{
7+
/**
8+
* Will output get
9+
* @return string
10+
*/
11+
public function __toString(): string;
12+
13+
/**
14+
* Get get Dom/document (Will only trigger execute once per instance)
15+
* @return string
16+
*/
17+
public function get(): string;
18+
19+
/**
20+
* Init DOM instance
21+
* @param string $key DOM access key
22+
* @return self
23+
*/
24+
public static function dom(string $key): self;
25+
26+
/**
27+
* Create and bind tag to a key so it can be overwritten
28+
* @param string $tag HTML tag (without brackets)
29+
* @param string $key Bind tag to key
30+
* @param bool|boolean $prepend Prepend instead of append
31+
* @return ElementInterface
32+
*/
33+
public function bindTag(string $tag, string $key, bool $prepend = false): ElementInterface;
34+
35+
/**
36+
* Create (append) element
37+
* @param string $element HTML tag (without brackets)
38+
* @param string $value add value to tag
39+
* @return ElementInterface
40+
*/
41+
public function create($element, $value = null, ?string $bind = null): ElementInterface;
42+
43+
/**
44+
* Prepend element first
45+
* @param string $element HTML tag (without brackets)
46+
* @param string $value add value to tag
47+
* @return ElementInterface
48+
*/
49+
public function createPrepend(string $element, ?string $value = null, ?string $bind = null): ElementInterface;
50+
51+
/**
52+
* Get one element from key
53+
* @return ElementInterface|null
54+
*/
55+
public function getElement(string $key): ?ElementInterface;
56+
57+
/**
58+
* Get all elements
59+
* @return array
60+
*/
61+
public function getElements(): array;
62+
63+
/**
64+
* Get html tag
65+
* @param string $key
66+
* @return string|null
67+
*/
68+
public function getTag(string $key): ?string;
69+
70+
/**
71+
* Execute and get Dom/document
72+
* @param callable|null $call Can be used to manipulate element within feed
73+
* @return string
74+
*/
75+
public function execute(?callable $call = null): string;
76+
}

Interfaces/ElementInterface.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace MaplePHP\Output\Interfaces;
4+
5+
interface ElementInterface
6+
{
7+
/**
8+
* Overwrite the current element
9+
* @param string $elem HTML Tag name
10+
*/
11+
public function setElement(string $elem): self;
12+
13+
/**
14+
* Set html attribute
15+
* @param string $key attr key
16+
* @param string|null $val attr value
17+
* @return self
18+
*/
19+
public function attr(string $key, ?string $val = null): self;
20+
21+
/**
22+
* Set multiple html attributes
23+
* @param array [key => value]
24+
* @return self
25+
*/
26+
public function attrArr(?array $arr): self;
27+
28+
/**
29+
* Hide html tag if its value is empty
30+
* @param bool $bool
31+
* @return self
32+
*/
33+
public function hideEmptyTag(bool $bool): self;
34+
35+
/**
36+
* Add value to attr
37+
* @param string $key
38+
* @param string $value
39+
* @param string $sep
40+
* @return self
41+
*/
42+
public function addAttr(string $key, string $value, string $sep = " "): self;
43+
44+
/**
45+
* Set elem value <elem>[VALUE]</elem>
46+
* @param string|null null value can be used to auto skip HTML tag
47+
* @return self
48+
*/
49+
public function setValue(?string $value): self;
50+
51+
/**
52+
* Set elem value
53+
* @return string
54+
*/
55+
public function getValue(): string;
56+
57+
/**
58+
* Get elem/HTML tag
59+
* @return string
60+
*/
61+
public function getEl(): string;
62+
63+
/**
64+
* With cloned element or new element if is specifed
65+
* @param string|null $elem
66+
* @return self
67+
*/
68+
public function withElement(?string $elem = null): self;
69+
}

SwiftRender.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* @Author: Daniel Ronkainen
66
* @Licence: Apache-2.0 license, Copyright © Daniel Ronkainen
77
Don't delete this comment, its part of the license.
8-
* @Version: 2.0.1
98
*/
109

1110
namespace MaplePHP\Output;

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "maplephp/swiftrender",
3+
"type": "library",
4+
"description": "PHP SwiftRender is a pure and highly portable PHP template library.",
5+
"keywords": ["output buffer", "template", "engine", "partials", "components", "views"],
6+
"homepage": "https://wazabii.se",
7+
"license": "Apache-2.0",
8+
"authors": [
9+
{
10+
"name": "Daniel Ronkainen",
11+
"email": "[email protected]"
12+
},
13+
{
14+
"name": "MaplePHP",
15+
"homepage": "https://wazabii.se"
16+
}
17+
],
18+
"require": {
19+
"php": ">=8.0",
20+
"maplephp/dto": "^1.0",
21+
"maplephp/container": "^1.0"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"MaplePHP\\Output\\": ""
26+
}
27+
},
28+
"minimum-stability": "dev"
29+
}

0 commit comments

Comments
 (0)