Skip to content

Commit 4068b97

Browse files
author
Wazabii
committed
Code improvements
1 parent 3ad52bf commit 4068b97

File tree

2 files changed

+82
-92
lines changed

2 files changed

+82
-92
lines changed

Json.php

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99

1010
class Json implements JsonInterface
1111
{
12+
13+
const ERROR_MESSAGES = [
14+
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
15+
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
16+
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
17+
JSON_ERROR_SYNTAX => 'Syntax error',
18+
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
19+
JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded',
20+
JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded',
21+
JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given',
22+
JSON_ERROR_INVALID_PROPERTY_NAME => 'A property name that cannot be encoded was given',
23+
JSON_ERROR_UTF16 => 'Malformed UTF-16 characters, possibly incorrectly encoded'
24+
];
25+
26+
1227
public $data = array("status" => 0, "error" => 0);
1328
public $fields = array();
1429

@@ -59,7 +74,6 @@ public function mergeTo(string $key, array $array): self
5974
return $this;
6075
}
6176

62-
6377
/**
6478
* Merge string to json array
6579
* @param string $key Set array key
@@ -101,8 +115,6 @@ public function item(...$args): array
101115
*/
102116
public function field($key, $args): self
103117
{
104-
105-
106118
if (is_array($key)) {
107119
$key = key($key);
108120
$this->fields = array_merge($this->fields, [$key => [
@@ -202,38 +214,30 @@ public function decode($json, $assoc = true): object
202214
*/
203215
public function validate(): void
204216
{
205-
switch (self::error()) {
206-
case JSON_ERROR_DEPTH:
207-
throw new \Exception('The maximum stack depth has been exceeded', self::error());
208-
break;
209-
case JSON_ERROR_STATE_MISMATCH:
210-
throw new \Exception('Invalid or malformed JSON', self::error());
211-
break;
212-
case JSON_ERROR_CTRL_CHAR:
213-
throw new \Exception('Control character error, possibly incorrectly encoded', self::error());
214-
break;
215-
case JSON_ERROR_SYNTAX:
216-
throw new \Exception('Syntax error', self::error());
217-
break;
218-
case JSON_ERROR_UTF8:
219-
throw new \Exception('Malformed UTF-8 characters, possibly incorrectly encoded', self::error());
220-
break;
221-
case JSON_ERROR_RECURSION:
222-
throw new \Exception('One or more recursive references in the value to be encoded', self::error());
223-
break;
224-
case JSON_ERROR_INF_OR_NAN:
225-
throw new \Exception('One or more NAN or INF values in the value to be encoded', self::error());
226-
break;
227-
case JSON_ERROR_UNSUPPORTED_TYPE:
228-
throw new \Exception('A value of a type that cannot be encoded was given', self::error());
229-
break;
230-
case JSON_ERROR_INVALID_PROPERTY_NAME:
231-
throw new \Exception('A property name that cannot be encoded was given', self::error());
232-
break;
233-
case JSON_ERROR_UTF16:
234-
throw new \Exception('Malformed UTF-16 characters, possibly incorrectly encoded', self::error());
235-
break;
217+
$error = (static::ERROR_MESSAGES[self::error()] ?? null);
218+
if (!is_null($error)) {
219+
throw new \Exception($error, self::error());
236220
}
221+
throw new \Exception('An unexpected Json error has occurred', self::error());
222+
}
223+
224+
/**
225+
* Travers slect data
226+
* @param string $key
227+
* @return mixed
228+
*/
229+
private function select(string $key)
230+
{
231+
$set = $this->data;
232+
$exp = explode(",", $key);
233+
foreach ($exp as $key) {
234+
if (isset($set[$key])) {
235+
$set = $set[$key];
236+
} else {
237+
return null;
238+
}
239+
}
240+
return $set;
237241
}
238242

239243
/**
@@ -243,39 +247,20 @@ public function validate(): void
243247
* @param int $depth read php.net
244248
* @return string|null
245249
*/
246-
public static function encodeData(array $json, $flag = JSON_UNESCAPED_UNICODE, int $depth = 512): ?string
250+
final protected static function encodeData(array $json, $flag = JSON_UNESCAPED_UNICODE, int $depth = 512): ?string
247251
{
248252
if (is_array($json) && count($json) > 0 && ($encode = json_encode($json, $flag, $depth))) {
249253
return $encode;
250254
}
251255
return null;
252256
}
253-
257+
254258
/**
255259
* Get last json error
256260
* @return int
257261
*/
258-
public static function error(): int
262+
final protected static function error(): int
259263
{
260264
return json_last_error();
261265
}
262-
263-
/**
264-
* Travers slect data
265-
* @param string $key
266-
* @return mixed
267-
*/
268-
private function select(string $key)
269-
{
270-
$set = $this->data;
271-
$exp = explode(",", $key);
272-
foreach ($exp as $key) {
273-
if (isset($set[$key])) {
274-
$set = $set[$key];
275-
} else {
276-
return null;
277-
}
278-
}
279-
return $set;
280-
}
281266
}

SwiftRender.php

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,19 @@ public function __construct()
4545
{
4646
}
4747

48-
4948
/**
5049
* This will make shortcuts to container.
5150
* @param string $m [description]
5251
* @param string $a [description]
5352
* @return ContainerInterface
5453
*/
55-
public function __call($m, $a)
54+
public function __call($method, $args)
5655
{
5756
if (!is_null($this->container)) {
58-
if ($this->container->has($m)) {
59-
return $this->container->get($m, $a);
57+
if ($this->container->has($method)) {
58+
return $this->container->get($method, $args);
6059
} else {
61-
throw new BadMethodCallException('The method "'.$m.'" does not exist in the Container '.
60+
throw new BadMethodCallException('The method "'.$method.'" does not exist in the Container '.
6261
'or the Class "'.static::class.'"!', 1);
6362
}
6463
}
@@ -212,24 +211,23 @@ public function withView(string $file, array $args = array()): self
212211

213212
/**
214213
* Create a partial view
215-
* @param string $key Partal key, example: ("sidebar", "breadcrumb")
216-
* @param string $file Filename
217-
* @param array $args Pass on argummets to template
218-
* @return self
214+
* @param string $keyA Filename/key
215+
* @param array $keyB Args/filename
216+
* @param array $keyC Args
219217
*/
220-
public function setPartial(string $key, string|array $a = array(), array $b = array()): self
218+
public function setPartial(string $keyA, string|array $keyB = array(), array $keyC = array()): self
221219
{
222-
if (is_array($a)) {
223-
$b = $a;
224-
$partial = $key;
220+
if (is_array($keyB)) {
221+
$keyC = $keyB;
222+
$partial = $keyA;
225223
} else {
226-
$partial = $a;
224+
$partial = $keyB;
227225
}
228226

229227
if (is_null($this->file)) {
230-
$this->setFile($key);
228+
$this->setFile($keyA);
231229
}
232-
$func = $this->build($this->file, $b);
230+
$func = $this->build($this->file, $keyC);
233231
$this->partial[$partial][] = $func;
234232
return $this;
235233
}
@@ -338,17 +336,7 @@ public function partial(string $key): self
338336
*/
339337
final public function get(?array $args = null): string
340338
{
341-
if (!is_null($this->bindView)) {
342-
if (($b = $this->existAtGet("buffer")) || ($v = $this->existAtGet("index"))) {
343-
if ($b) {
344-
$this->buffer = $this->bindView;
345-
}
346-
if ($v) {
347-
$this->view = $this->bindView;
348-
}
349-
}
350-
}
351-
339+
$this->buildView();
352340
$output = $this->{$this->get};
353341

354342
// Will merge/replace arguments with current/deafult arguments
@@ -380,6 +368,20 @@ final public function get(?array $args = null): string
380368
return (string)$output;
381369
}
382370

371+
private function buildView(): void
372+
{
373+
if (!is_null($this->bindView)) {
374+
if (($hasBuffer = $this->existAtGet("buffer")) || ($hasIndex = $this->existAtGet("index"))) {
375+
if ($hasBuffer) {
376+
$this->buffer = $this->bindView;
377+
}
378+
if ($hasIndex) {
379+
$this->view = $this->bindView;
380+
}
381+
}
382+
}
383+
}
384+
383385
/**
384386
* Build and Contain template and data until it's executed,
385387
* this means that code is prepared and will not take any extra memory if view would not be called.
@@ -390,8 +392,10 @@ final public function get(?array $args = null): string
390392
*/
391393
private function build(string|callable $file, array $args = array()): callable
392394
{
395+
393396
$this->arguments = $args;
394-
$func = function ($a) use ($file, $args) {
397+
$func = function ($argsFromFile) use ($file, $args) {
398+
395399
if (($dir = ($this->dir[$this->get] ?? null)) || !is_null($dir)) {
396400
if (is_callable($file)) {
397401
$out = $file($this, $args);
@@ -401,11 +405,12 @@ private function build(string|callable $file, array $args = array()): callable
401405
} else {
402406
$filePath = "{$dir}{$file}.{$this->ending}";
403407
if (is_string($filePath) && is_file($filePath)) {
404-
if (is_array($a) && count($a) > 0) {
405-
$args = $a;
408+
if (is_array($argsFromFile) && count($argsFromFile) > 0) {
409+
$args = $argsFromFile;
406410
}
407411
$obj = Traverse::value($args);
408412
include($filePath);
413+
409414
} else {
410415
throw new Exception("Could not require template file add {$this->get}: {$dir}{$file}.", 1);
411416
}
@@ -457,18 +462,18 @@ public function dom(string $key): Document
457462
public function createTag(string $element, string $value, ?array $attr = null)
458463
{
459464
$inst = new Document();
460-
$el = $inst->create($element, $value)->attrArr($attr);
461-
return $el;
465+
$elem = $inst->create($element, $value)->attrArr($attr);
466+
return $elem;
462467
}
463468

464-
public function isDoc($el): bool
469+
public function isDoc($elem): bool
465470
{
466-
return (bool)($el instanceof Document || $el instanceof Element);
471+
return (bool)($elem instanceof Document || $elem instanceof Element);
467472
}
468473

469-
public function isEl($el): bool
474+
public function isEl($elem): bool
470475
{
471-
return (bool)($el instanceof Element);
476+
return (bool)($elem instanceof Element);
472477
}
473478

474479
/*

0 commit comments

Comments
 (0)