Skip to content

Commit 1c1d7b1

Browse files
author
Wazabii
committed
Code structure
1 parent 4068b97 commit 1c1d7b1

File tree

4 files changed

+88
-69
lines changed

4 files changed

+88
-69
lines changed

Dom/Document.php

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,41 @@
1212
class Document
1313
{
1414
public const TAG_NO_ENDING = [
15-
"meta", "link", "img", "br", "hr", "input", "keygen", "param", "source", "track", "embed"
15+
"meta",
16+
"link",
17+
"img",
18+
"br",
19+
"hr",
20+
"input",
21+
"keygen",
22+
"param",
23+
"source",
24+
"track",
25+
"embed"
1626
];
1727

1828
protected $elements;
1929
private $html;
20-
private $el;
30+
private $elem;
2131
private static $inst;
2232

33+
public function __toString()
34+
{
35+
return $this->get();
36+
}
37+
38+
/**
39+
* Get get Dom/document (Will only trigger execute once per instance)
40+
* @return string
41+
*/
42+
public function get()
43+
{
44+
if (is_null($this->html)) {
45+
$this->execute();
46+
}
47+
return $this->html;
48+
}
49+
2350
/**
2451
* Init DOM instance
2552
* @param string $key DOM access key
@@ -43,15 +70,13 @@ public static function dom(string $key)
4370
public function bindTag(string $tag, string $key, bool $prepend = false)
4471
{
4572
if ($prepend) {
46-
$this->el = $this->createPrepend($tag, null, $key);
73+
$this->elem = $this->createPrepend($tag, null, $key);
4774
} else {
48-
$this->el = $this->create($tag, null, $key);
75+
$this->elem = $this->create($tag, null, $key);
4976
}
50-
return $this->el;
77+
return $this->elem;
5178
}
5279

53-
54-
5580
/**
5681
* Create (append) element
5782
* @param string $element HTML tag (without brackets)
@@ -98,9 +123,9 @@ public function createPrepend(string $element, ?string $value = null, ?string $b
98123
* Get one element from key
99124
* @return Response\Dom\Element
100125
*/
101-
public function getElement($k)
126+
public function getElement(string $key)
102127
{
103-
return ($this->elements[$k] ?? null);
128+
return ($this->elements[$key] ?? null);
104129
}
105130

106131
/**
@@ -112,7 +137,12 @@ public function getElements()
112137
return $this->elements;
113138
}
114139

115-
public function getTag(string $key)
140+
/**
141+
* Get html tag
142+
* @param string $key
143+
* @return string|null
144+
*/
145+
public function getTag(string $key): ?string
116146
{
117147
return ($this->el[$key] ?? null);
118148
}
@@ -125,62 +155,57 @@ public function getTag(string $key)
125155
public function execute(?callable $call = null)
126156
{
127157
$this->html = "";
128-
if (is_null($this->elements) && ($inst = $this->withElement())) {
129-
$this->elements[] = $inst;
158+
if (is_null($this->elements)) {
159+
if (method_exists($this, "withElement")) {
160+
$inst = $this->withElement();
161+
$this->elements[] = $inst;
162+
}
130163
}
131164
if (is_array($this->elements)) {
132165
$this->build($this->elements, $call);
133166
}
134167
return $this->html;
135168
}
136169

137-
/**
138-
* Get get Dom/document (Will only trigger execute once per instance)
139-
* @return string
140-
*/
141-
public function get()
142-
{
143-
if (is_null($this->html)) {
144-
$this->execute();
145-
}
146-
return $this->html;
147-
}
148-
149-
150-
public function __toString()
170+
protected function elemHasEnding(string $elem): bool
151171
{
152-
return $this->get();
172+
return (bool)(in_array($elem, $this::TAG_NO_ENDING));
153173
}
154174

155-
156175
/**
157176
* Build document
158177
* @param array $arr elements
159178
* @param callable|null $call Can be used to manipulate element within feed
179+
* @return void
160180
*/
161-
private function build(array $arr, ?callable $call = null)
181+
private function build(array $arr, ?callable $call = null): void
162182
{
163-
foreach ($arr as $k => $a) {
164-
$hasNoEnding = in_array($a->getEl(), $this::TAG_NO_ENDING);
165-
if (!is_null($call)) {
166-
$call($a, $k, $hasNoEnding);
167-
}
183+
foreach ($arr as $key => $elemObj) {
184+
$hasNoEnding = $this->elemHasEnding($elemObj->getEl());
185+
$this->buildCallable($elemObj, $key, $hasNoEnding, $call);
168186

169-
if (!$a->hideTagValid()) {
170-
$this->html .= "\t<".$a->getEl().$a->buildAttr().">";
187+
if (!$elemObj->hideTagValid()) {
188+
$this->html .= "\t<" . $elemObj->getEl() . $elemObj->buildAttr() . ">";
171189
}
172190
if (!$hasNoEnding) {
173-
$this->html .= $a->getValue();
191+
$this->html .= $elemObj->getValue();
174192
}
175-
if (isset($a->elements)) {
176-
$this->build($a->elements, $call);
193+
if (isset($elemObj->elements)) {
194+
$this->build($elemObj->elements, $call);
177195
}
178-
if (!$hasNoEnding && !$a->hideTagValid()) {
179-
$this->html .= "</".$a->getEl().">\n";
196+
if (!$hasNoEnding && !$elemObj->hideTagValid()) {
197+
$this->html .= "</" . $elemObj->getEl() . ">\n";
180198
}
181-
if ($hasNoEnding && !$a->hideTagValid()) {
199+
if ($hasNoEnding && !$elemObj->hideTagValid()) {
182200
$this->html .= "\n";
183201
}
184202
}
185203
}
204+
205+
private function buildCallable($elemObj, $key, $hasNoEnding, ?callable $call): void
206+
{
207+
if (!is_null($call)) {
208+
$call($elemObj, $key, $hasNoEnding);
209+
}
210+
}
186211
}

Dom/Element.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,28 @@
1414

1515
class Element extends Document
1616
{
17-
private $inst;
18-
private $el;
17+
private $elem;
1918
private $attr = array();
2019
private $snippet;
2120
private $value;
22-
private $node;
2321
private $hideEmptyTag = false;
22+
//private $node;
23+
//private $inst;
2424

25-
26-
public function __construct(string $el, ?string $value, bool $snippet = false)
25+
public function __construct(string $elem, ?string $value, bool $snippet = false)
2726
{
28-
//this->inst = $inst;
29-
$this->el = $el;
27+
$this->elem = $elem;
3028
$this->value = $value;
3129
$this->snippet = $snippet;
3230
}
3331

3432
/**
3533
* Overwrite the current element
36-
* @param string $el HTML Tag name
34+
* @param string $elem HTML Tag name
3735
*/
38-
public function setElement(string $el): self
36+
public function setElement(string $elem): self
3937
{
40-
$this->el = $el;
38+
$this->elem = $elem;
4139
return $this;
4240
}
4341

@@ -110,7 +108,7 @@ public function attrAddTo(string $key, string $value, string $sep = " "): self
110108
}
111109

112110
/**
113-
* Set el value <elem>[VALUE]</elem>
111+
* Set elem value <elem>[VALUE]</elem>
114112
* @param self
115113
*/
116114
public function setValue(?string $value): self
@@ -120,7 +118,7 @@ public function setValue(?string $value): self
120118
}
121119

122120
/**
123-
* Set el value
121+
* Set elem value
124122
* @param string
125123
*/
126124
public function getValue(): string
@@ -129,12 +127,12 @@ public function getValue(): string
129127
}
130128

131129
/**
132-
* Get el/HTML tag
130+
* Get elem/HTML tag
133131
* @return string
134132
*/
135133
public function getEl(): string
136134
{
137-
return (string)$this->el;
135+
return (string)$this->elem;
138136
}
139137

140138
/**
@@ -161,7 +159,7 @@ protected function buildAttr(): string
161159
*/
162160
public function withElement()
163161
{
164-
if (!is_null($this->el)) {
162+
if (!is_null($this->elem)) {
165163
return clone $this;
166164
}
167165
return false;

Json.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
declare(strict_types=1);
54

65
namespace PHPFuse\Output;
@@ -9,8 +8,7 @@
98

109
class Json implements JsonInterface
1110
{
12-
13-
const ERROR_MESSAGES = [
11+
public const ERROR_MESSAGES = [
1412
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
1513
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
1614
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
@@ -254,7 +252,7 @@ final protected static function encodeData(array $json, $flag = JSON_UNESCAPED_U
254252
}
255253
return null;
256254
}
257-
255+
258256
/**
259257
* Get last json error
260258
* @return int

SwiftRender.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use PHPFuse\Container\Interfaces\ContainerInterface;
1414
use Exception;
1515
use BadMethodCallException;
16-
1716
use PHPFuse\DTO\Format\Str;
1817
use PHPFuse\DTO\Traverse;
1918
use PHPFuse\Output\Dom\Document;
@@ -51,16 +50,16 @@ public function __construct()
5150
* @param string $a [description]
5251
* @return ContainerInterface
5352
*/
54-
public function __call($method, $args)
53+
public function __call($method, $args): ContainerInterface
5554
{
5655
if (!is_null($this->container)) {
5756
if ($this->container->has($method)) {
5857
return $this->container->get($method, $args);
59-
} else {
60-
throw new BadMethodCallException('The method "'.$method.'" does not exist in the Container '.
61-
'or the Class "'.static::class.'"!', 1);
6258
}
6359
}
60+
61+
throw new BadMethodCallException('The method "' . $method . '" does not exist in the Container ' .
62+
'or the Class "' . static::class . '"!', 1);
6463
}
6564

6665
/**
@@ -392,7 +391,7 @@ private function buildView(): void
392391
*/
393392
private function build(string|callable $file, array $args = array()): callable
394393
{
395-
394+
396395
$this->arguments = $args;
397396
$func = function ($argsFromFile) use ($file, $args) {
398397

@@ -410,13 +409,12 @@ private function build(string|callable $file, array $args = array()): callable
410409
}
411410
$obj = Traverse::value($args);
412411
include($filePath);
413-
414412
} else {
415413
throw new Exception("Could not require template file add {$this->get}: {$dir}{$file}.", 1);
416414
}
417415
}
418416
} else {
419-
throw new Exception("You need to call @".str_replace("_", "", $this->get).
417+
throw new Exception("You need to call @" . str_replace("_", "", $this->get) .
420418
"DIR and specify dir path for {$file}.", 1);
421419
}
422420
};

0 commit comments

Comments
 (0)