Skip to content

Commit a4bf93a

Browse files
author
Wazabii
committed
Data types
1 parent d6925fc commit a4bf93a

File tree

5 files changed

+83
-61
lines changed

5 files changed

+83
-61
lines changed

Dom/Document.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public function createPrepend(string $element, ?string $value = null, ?string $b
121121

122122
/**
123123
* Get one element from key
124-
* @return Element
124+
* @return Element|null
125125
*/
126-
public function getElement(string $key): Element
126+
public function getElement(string $key): ?Element
127127
{
128128
return ($this->elements[$key] ?? null);
129129
}
@@ -166,12 +166,7 @@ public function execute(?callable $call = null)
166166
}
167167
return $this->html;
168168
}
169-
170-
protected function elemHasEnding(string $elem): bool
171-
{
172-
return (bool)(in_array($elem, $this::TAG_NO_ENDING));
173-
}
174-
169+
175170
/**
176171
* Build document
177172
* @param array $arr elements
@@ -208,4 +203,14 @@ private function buildCallable($elemObj, $key, $hasNoEnding, ?callable $call): v
208203
$call($elemObj, $key, $hasNoEnding);
209204
}
210205
}
206+
207+
/**
208+
* Validate if element has ending
209+
* @param string $elem
210+
* @return bool
211+
*/
212+
final protected function elemHasEnding(string $elem): bool
213+
{
214+
return (in_array($elem, $this::TAG_NO_ENDING));
215+
}
211216
}

Dom/Element.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function setElement(string $elem): self
4545
* @param string|null $val attr value
4646
* @return self
4747
*/
48-
public function attr(string $key, ?string $val = null)
48+
public function attr(string $key, ?string $val = null): self
4949
{
5050
$this->attr[$key] = $val;
5151
return $this;
@@ -56,7 +56,7 @@ public function attr(string $key, ?string $val = null)
5656
* @param array [key => value]
5757
* @return self
5858
*/
59-
public function attrArr(?array $arr)
59+
public function attrArr(?array $arr): self
6060
{
6161
if (is_array($arr)) {
6262
$this->attr = array_merge($this->attr, $arr);
@@ -81,7 +81,7 @@ public function hideEmptyTag(bool $bool): self
8181
*/
8282
protected function hideTagValid(): bool
8383
{
84-
return (bool)(($this->hideEmptyTag && !$this->value));
84+
return (($this->hideEmptyTag && !$this->value));
8585
}
8686

8787
/**
@@ -109,7 +109,8 @@ public function attrAddTo(string $key, string $value, string $sep = " "): self
109109

110110
/**
111111
* Set elem value <elem>[VALUE]</elem>
112-
* @param self
112+
* @param string|null null value can be used to auto skip HTML tag
113+
* @return self
113114
*/
114115
public function setValue(?string $value): self
115116
{
@@ -119,7 +120,7 @@ public function setValue(?string $value): self
119120

120121
/**
121122
* Set elem value
122-
* @param string
123+
* @return string
123124
*/
124125
public function getValue(): string
125126
{
@@ -132,7 +133,7 @@ public function getValue(): string
132133
*/
133134
public function getEl(): string
134135
{
135-
return (string)$this->elem;
136+
return $this->elem;
136137
}
137138

138139
/**
@@ -154,14 +155,16 @@ protected function buildAttr(): string
154155
}
155156

156157
/**
157-
* Clone/Static
158-
* @return static|false
158+
* With cloned element or new element if is specifed
159+
* @param string|null $elem
160+
* @return self
159161
*/
160-
public function withElement()
162+
public function withElement(?string $elem = null): self
161163
{
162-
if (!is_null($this->elem)) {
163-
return clone $this;
164+
$inst = clone $this;
165+
if (!is_null($elem)) {
166+
$inst->elem = $elem;
164167
}
165-
return false;
168+
return $inst;
166169
}
167170
}

Interfaces/JsonInterface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ public function get(?string $key = null);
4343
* Convert json array to json string
4444
* @param int $options Bitmask
4545
* @param int $depth Set the maximum depth. Must be greater than zero
46-
* @return json/bool (bool if could not load json data)
46+
* @return string|null (bool if could not load json data)
4747
*/
48-
public function encode(int $options = JSON_UNESCAPED_UNICODE, int $depth = 512): string;
48+
public function encode(int $options = JSON_UNESCAPED_UNICODE, int $depth = 512): string|null;
4949

5050
/**
5151
* Decode json data
52-
* @param string $json Json data
53-
* @param boolean $assoc When TRUE, returned objects will be converted into associative arrays.
54-
* @return array/bool Resturns as array or false if error occoured.
52+
* @param string $json Json data
53+
* @param boolean $assoc When TRUE, returned objects will be converted into associative arrays.
54+
* @return object|array|false Resturns as array or false if error occoured.
5555
*/
56-
public function decode($json, $assoc = true): object;
56+
public function decode($json, $assoc = true): object|array|false;
5757

5858
/**
5959
* Validate output

Json.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct()
3232

3333
public function __toString(): string
3434
{
35-
return $this->encode();
35+
return (string)$this->encode();
3636
}
3737

3838
/**
@@ -89,7 +89,7 @@ public function add(string $key, $value): self
8989
* Merge string to json array
9090
* @param string $key Set array key
9191
* @param mixed $value Set array value
92-
* @return self
92+
* @return array
9393
*/
9494
public function item(...$args): array
9595
{
@@ -100,30 +100,36 @@ public function item(...$args): array
100100
$args = $args[0];
101101
}
102102
}
103-
$argumnets = (!is_null($key)) ? [[$key => $args]] : [...$args];
103+
$argumnets = (!is_null($key)) ? [[$key => $args]] : $args;
104104
$this->data = array_merge($this->data, $argumnets);
105105
return reset($argumnets);
106106
}
107107

108108
/**
109109
* Merge string to json array
110-
* @param string $key Set array key
111-
* @param mixed $value Set array value
110+
* @param string|array $key Set array key
111+
* @param array $args Set array value
112112
* @return self
113113
*/
114-
public function field($key, $args): self
114+
public function field(string|array $key, array $args): self
115115
{
116-
if (is_array($key)) {
116+
if (is_array($key) && count($key) > 0) {
117117
$key = key($key);
118+
119+
if (!is_string($key)) {
120+
throw new \Exception("The key need to be string value", 1);
121+
}
122+
118123
$this->fields = array_merge($this->fields, [$key => [
119124
"type" => $key,
120125
...$args
121126
]]);
122127
} else {
128+
if (!is_string($key)) {
129+
throw new \Exception("The key need to be string value", 1);
130+
}
123131
$this->fields = array_merge($this->fields, [$key => $args]);
124132
}
125-
126-
127133
return $this;
128134
}
129135

@@ -185,20 +191,20 @@ public function output(string $key): ?string
185191
* Convert json array to json string
186192
* @param int $options Bitmask
187193
* @param int $depth Set the maximum depth. Must be greater than zero
188-
* @return json/bool (bool if could not load json data)
194+
* @return string|null (bool if could not load json data)
189195
*/
190-
public function encode(int $options = JSON_UNESCAPED_UNICODE, int $depth = 512): string
196+
public function encode(int $options = JSON_UNESCAPED_UNICODE, int $depth = 512): ?string
191197
{
192198
return self::encodeData($this->data, $options, $depth);
193199
}
194200

195201
/**
196202
* Decode json data
197-
* @param string $json Json data
198-
* @param boolean $assoc When TRUE, returned objects will be converted into associative arrays.
199-
* @return array/bool Resturns as array or false if error occoured.
203+
* @param string $json Json data
204+
* @param boolean $assoc When TRUE, returned objects will be converted into associative arrays.
205+
* @return object|array|false Resturns as array or false if error occoured.
200206
*/
201-
public function decode($json, $assoc = true): object
207+
public function decode($json, $assoc = true): object|array|false
202208
{
203209
if ($array = json_decode($json, $assoc)) {
204210
return $array;
@@ -247,7 +253,7 @@ private function select(string $key)
247253
*/
248254
final protected static function encodeData(array $json, int $flag = JSON_UNESCAPED_UNICODE, int $depth = 512): ?string
249255
{
250-
if (is_array($json) && count($json) > 0 && ($encode = json_encode($json, $flag, $depth))) {
256+
if (count($json) > 0 && ($encode = json_encode($json, $flag, $depth))) {
251257
return $encode;
252258
}
253259
return null;

SwiftRender.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public function getContainer(): ContainerInterface
8282

8383
/**
8484
* Set a default file ending (".php" is pre defined)
85-
* @param self
85+
* @param string $ending
86+
* @return self
8687
*/
8788
public function setFileEnding(string $ending): self
8889
{
@@ -93,7 +94,8 @@ public function setFileEnding(string $ending): self
9394
/**
9495
* Customize template file.
9596
* (Call this if you want to bind for multiple file to same partial)
96-
* @param self
97+
* @param string $file
98+
* @return self
9799
*/
98100
public function setFile(string $file): self
99101
{
@@ -151,7 +153,7 @@ public function setPartialDir(string $dir): self
151153

152154
/**
153155
* Create a index view
154-
* @param string $file Filename
156+
* @param string|callable $file Filename
155157
* @return self
156158
*/
157159
public function setIndex(string|callable $file): self
@@ -178,7 +180,7 @@ public function setBuffer(string $output): self
178180

179181
/**
180182
* Create a Main view
181-
* @param string $file Filename
183+
* @param string|callable $file Filename
182184
* @param array $args Pass on argummets to template
183185
* @return self
184186
*/
@@ -207,9 +209,9 @@ public function withView(string $file, array $args = array()): self
207209

208210
/**
209211
* Create a partial view
210-
* @param string $keyA Filename/key
211-
* @param array $keyB Args/filename
212-
* @param array $keyC Args
212+
* @param string $keyA Filename/key
213+
* @param string|array $keyB Args/filename
214+
* @param array $keyC Args
213215
*/
214216
public function setPartial(string $keyA, string|array $keyB = array(), array $keyC = array()): self
215217
{
@@ -257,10 +259,11 @@ public function bindToBody(string $key, array $bindArr, array $args = array()):
257259

258260
/**
259261
* IF find in specified Bind Array the it will return the view
260-
* @param string|int|float $find
261-
* @return [type] [description]
262+
* @param string|int $find
263+
* @param bool $overwrite
264+
* @return void
262265
*/
263-
public function findBind($find, bool $overwrite = false): void
266+
public function findBind(string|int $find, bool $overwrite = false): void
264267
{
265268
if (!is_null($this->bindArr) && ($overwrite || is_null($this->bindView))) {
266269
foreach ($this->bindArr as $get => $arr) {
@@ -275,7 +278,7 @@ public function findBind($find, bool $overwrite = false): void
275278

276279
/**
277280
* Prepare index for return
278-
* @param boolean $args Overwrite arguments
281+
* @param array|null $args Overwrite arguments
279282
* @return self
280283
*/
281284
public function index(?array $args = null): self
@@ -380,7 +383,7 @@ private function buildView(): void
380383
* Build and Contain template and data until it's executed,
381384
* this means that code is prepared and will not take any extra memory if view would not be called.
382385
* So you can if you want prepare a bunch of partial views and just call the the ones you want
383-
* @param string $file the filename
386+
* @param string|callable $file the filename
384387
* @param array $args Pass arguments to template
385388
* @return callable
386389
*/
@@ -398,7 +401,7 @@ private function build(string|callable $file, array $args = array()): callable
398401
}
399402
} else {
400403
$filePath = "{$dir}{$file}.{$this->ending}";
401-
if (is_string($filePath) && is_file($filePath)) {
404+
if (is_file($filePath)) {
402405
if (is_array($argsFromFile) && count($argsFromFile) > 0) {
403406
$args = $argsFromFile;
404407
}
@@ -424,7 +427,7 @@ private function build(string|callable $file, array $args = array()): callable
424427
*/
425428
public function partialExists($key): bool
426429
{
427-
return (bool)isset($this->partial[$key]);
430+
return isset($this->partial[$key]);
428431
}
429432

430433
/**
@@ -434,7 +437,7 @@ public function partialExists($key): bool
434437
*/
435438
public function exists(string $key): bool
436439
{
437-
return (bool)(in_array($key, $this::VIEWS) && isset($this->{$key}));
440+
return (in_array($key, $this::VIEWS) && isset($this->{$key}));
438441
}
439442

440443
/**
@@ -444,7 +447,7 @@ public function exists(string $key): bool
444447
*/
445448
private function existAtGet(string $key): bool
446449
{
447-
return (bool)(isset($this->{$key}) && $this->get === $key);
450+
return (isset($this->{$key}) && $this->get === $key);
448451
}
449452

450453
/**
@@ -464,20 +467,25 @@ public function dom(string $key): Document
464467
{
465468
return Document::dom($key);
466469
}
470+
467471
public function createTag(string $element, string $value, ?array $attr = null)
468472
{
469473
$inst = new Document();
470-
$elem = $inst->create($element, $value)->attrArr($attr);
474+
$elem = $inst->create($element, $value);
475+
if (!($elem instanceof Element)) {
476+
throw new \Exception("Could not find connection to Element instance", 1);
477+
}
478+
$elem = $elem->attrArr($attr);
471479
return $elem;
472480
}
473481

474482
public function isDoc($elem): bool
475483
{
476-
return (bool)($elem instanceof Document || $elem instanceof Element);
484+
return ($elem instanceof Document || $elem instanceof Element);
477485
}
478486

479487
public function isEl($elem): bool
480488
{
481-
return (bool)($elem instanceof Element);
489+
return ($elem instanceof Element);
482490
}
483491
}

0 commit comments

Comments
 (0)