Skip to content

Commit 3ad52bf

Browse files
author
Wazabii
committed
Coding standards
1 parent 8f81765 commit 3ad52bf

File tree

5 files changed

+1159
-1105
lines changed

5 files changed

+1159
-1105
lines changed

Dom/Document.php

Lines changed: 186 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,186 @@
1-
<?php
2-
/**
3-
* @Package: PHPFuse - DOM Main class
4-
* @Author: Daniel Ronkainen
5-
* @Licence: The MIT License (MIT), Copyright © Daniel Ronkainen
6-
Don't delete this comment, its part of the license.
7-
*/
8-
9-
namespace PHPFuse\Output\Dom;
10-
11-
class Document {
12-
13-
public const TAG_NO_ENDING = [
14-
"meta", "link", "img", "br", "hr", "input", "keygen", "param", "source", "track", "embed"
15-
];
16-
17-
protected $elements;
18-
private $html;
19-
private $el;
20-
private static $inst;
21-
22-
/**
23-
* Init DOM instance
24-
* @param string $key DOM access key
25-
* @return new self
26-
*/
27-
static function dom(string $key) {
28-
if(empty(self::$inst[$key])) self::$inst[$key] = new self();
29-
return self::$inst[$key];
30-
}
31-
32-
/**
33-
* Create and bind tag to a key so it can be overwritten
34-
* @param string $tag HTML tag (without brackets)
35-
* @param string $key Bind tag to key
36-
* @param bool|boolean $prepend Prepend instead of append
37-
* @return self
38-
*/
39-
function bindTag(string $tag, string $key, bool $prepend = false) {
40-
if($prepend) {
41-
$this->el = $this->createPrepend($tag, NULL, $key);
42-
} else {
43-
$this->el = $this->create($tag, NULL, $key);
44-
}
45-
return $this->el;
46-
}
47-
48-
49-
50-
/**
51-
* Create (append) element
52-
* @param string $element HTML tag (without brackets)
53-
* @param string $value add value to tag
54-
* @return self
55-
*/
56-
function create($element, $value = NULL, ?string $bind = NULL) {
57-
$inst = new Element($element, $value);
58-
59-
if(!is_null($bind)) {
60-
$this->elements[$bind] = $inst;
61-
} else {
62-
$this->elements[] = $inst;
63-
}
64-
65-
return $inst;
66-
}
67-
68-
/**
69-
* Prepend element first
70-
* @param string $element HTML tag (without brackets)
71-
* @param string $value add value to tag
72-
* @return self
73-
*/
74-
function createPrepend(string $element, ?string $value = NULL, ?string $bind = NULL) {
75-
$inst = new Element($element, $value);
76-
if(is_null($this->elements)) $this->elements = array();
77-
78-
if(!is_null($bind)) {
79-
$new[$bind] = $inst;
80-
$this->elements = array_merge($new, $this->elements);
81-
} else {
82-
$this->elements = array_merge([$inst], $this->elements);
83-
}
84-
85-
return $inst;
86-
}
87-
88-
/**
89-
* Get one element from key
90-
* @return Response\Dom\Element
91-
*/
92-
function getElement($k) {
93-
return ($this->elements[$k] ?? NULL);
94-
}
95-
96-
/**
97-
* Get all elements
98-
* @return array
99-
*/
100-
function getElements() {
101-
return $this->elements;
102-
}
103-
104-
function getTag(string $key) {
105-
return ($this->el[$key] ?? NULL);
106-
}
107-
108-
/**
109-
* Execute and get Dom/document
110-
* @param callable|null $call Can be used to manipulate element within feed
111-
* @return string
112-
*/
113-
function execute(?callable $call = NULL) {
114-
$this->html = "";
115-
if(is_null($this->elements) && ($inst = $this->withElement())) $this->elements[] = $inst;
116-
if(is_array($this->elements)) {
117-
$this->build($this->elements, $call);
118-
}
119-
return $this->html;
120-
}
121-
122-
/**
123-
* Get get Dom/document (Will only trigger execute once per instance)
124-
* @return string
125-
*/
126-
function get() {
127-
if(is_null($this->html)) $this->execute();
128-
return $this->html;
129-
}
130-
131-
132-
function __toString() {
133-
return $this->get();
134-
}
135-
136-
137-
/**
138-
* Build document
139-
* @param array $arr elements
140-
* @param callable|null $call Can be used to manipulate element within feed
141-
*/
142-
private function build(array $arr, ?callable $call = NULL) {
143-
foreach($arr as $k => $a) {
144-
$hasNoEnding = in_array($a->getEl(), $this::TAG_NO_ENDING);
145-
if(!is_null($call)) $call($a, $k, $hasNoEnding);
146-
147-
if(!$a->hideTagValid()) $this->html .= "\t<".$a->getEl().$a->buildAttr().">";
148-
if(!$hasNoEnding) $this->html .= $a->getValue();
149-
if(isset($a->elements)) {
150-
$this->build($a->elements, $call);
151-
}
152-
if(!$hasNoEnding && !$a->hideTagValid()) $this->html .= "</".$a->getEl().">\n";
153-
if($hasNoEnding && !$a->hideTagValid()) $this->html .= "\n";
154-
}
155-
}
156-
157-
}
1+
<?php
2+
3+
/**
4+
* @Package: PHPFuse - DOM Main class
5+
* @Author: Daniel Ronkainen
6+
* @Licence: The MIT License (MIT), Copyright © Daniel Ronkainen
7+
Don't delete this comment, its part of the license.
8+
*/
9+
10+
namespace PHPFuse\Output\Dom;
11+
12+
class Document
13+
{
14+
public const TAG_NO_ENDING = [
15+
"meta", "link", "img", "br", "hr", "input", "keygen", "param", "source", "track", "embed"
16+
];
17+
18+
protected $elements;
19+
private $html;
20+
private $el;
21+
private static $inst;
22+
23+
/**
24+
* Init DOM instance
25+
* @param string $key DOM access key
26+
* @return new self
27+
*/
28+
public static function dom(string $key)
29+
{
30+
if (empty(self::$inst[$key])) {
31+
self::$inst[$key] = new self();
32+
}
33+
return self::$inst[$key];
34+
}
35+
36+
/**
37+
* Create and bind tag to a key so it can be overwritten
38+
* @param string $tag HTML tag (without brackets)
39+
* @param string $key Bind tag to key
40+
* @param bool|boolean $prepend Prepend instead of append
41+
* @return self
42+
*/
43+
public function bindTag(string $tag, string $key, bool $prepend = false)
44+
{
45+
if ($prepend) {
46+
$this->el = $this->createPrepend($tag, null, $key);
47+
} else {
48+
$this->el = $this->create($tag, null, $key);
49+
}
50+
return $this->el;
51+
}
52+
53+
54+
55+
/**
56+
* Create (append) element
57+
* @param string $element HTML tag (without brackets)
58+
* @param string $value add value to tag
59+
* @return self
60+
*/
61+
public function create($element, $value = null, ?string $bind = null)
62+
{
63+
$inst = new Element($element, $value);
64+
65+
if (!is_null($bind)) {
66+
$this->elements[$bind] = $inst;
67+
} else {
68+
$this->elements[] = $inst;
69+
}
70+
71+
return $inst;
72+
}
73+
74+
/**
75+
* Prepend element first
76+
* @param string $element HTML tag (without brackets)
77+
* @param string $value add value to tag
78+
* @return self
79+
*/
80+
public function createPrepend(string $element, ?string $value = null, ?string $bind = null)
81+
{
82+
$inst = new Element($element, $value);
83+
if (is_null($this->elements)) {
84+
$this->elements = array();
85+
}
86+
87+
if (!is_null($bind)) {
88+
$new[$bind] = $inst;
89+
$this->elements = array_merge($new, $this->elements);
90+
} else {
91+
$this->elements = array_merge([$inst], $this->elements);
92+
}
93+
94+
return $inst;
95+
}
96+
97+
/**
98+
* Get one element from key
99+
* @return Response\Dom\Element
100+
*/
101+
public function getElement($k)
102+
{
103+
return ($this->elements[$k] ?? null);
104+
}
105+
106+
/**
107+
* Get all elements
108+
* @return array
109+
*/
110+
public function getElements()
111+
{
112+
return $this->elements;
113+
}
114+
115+
public function getTag(string $key)
116+
{
117+
return ($this->el[$key] ?? null);
118+
}
119+
120+
/**
121+
* Execute and get Dom/document
122+
* @param callable|null $call Can be used to manipulate element within feed
123+
* @return string
124+
*/
125+
public function execute(?callable $call = null)
126+
{
127+
$this->html = "";
128+
if (is_null($this->elements) && ($inst = $this->withElement())) {
129+
$this->elements[] = $inst;
130+
}
131+
if (is_array($this->elements)) {
132+
$this->build($this->elements, $call);
133+
}
134+
return $this->html;
135+
}
136+
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()
151+
{
152+
return $this->get();
153+
}
154+
155+
156+
/**
157+
* Build document
158+
* @param array $arr elements
159+
* @param callable|null $call Can be used to manipulate element within feed
160+
*/
161+
private function build(array $arr, ?callable $call = null)
162+
{
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+
}
168+
169+
if (!$a->hideTagValid()) {
170+
$this->html .= "\t<".$a->getEl().$a->buildAttr().">";
171+
}
172+
if (!$hasNoEnding) {
173+
$this->html .= $a->getValue();
174+
}
175+
if (isset($a->elements)) {
176+
$this->build($a->elements, $call);
177+
}
178+
if (!$hasNoEnding && !$a->hideTagValid()) {
179+
$this->html .= "</".$a->getEl().">\n";
180+
}
181+
if ($hasNoEnding && !$a->hideTagValid()) {
182+
$this->html .= "\n";
183+
}
184+
}
185+
}
186+
}

0 commit comments

Comments
 (0)