Common files

This commit is contained in:
2020-12-15 17:45:27 -03:00
parent 736eb72254
commit 6c945e915f
5 changed files with 104 additions and 0 deletions

5
common/Alias/View.php Normal file
View File

@ -0,0 +1,5 @@
<?php
namespace ProVM\Common\Alias;
interface View {
}

View File

@ -0,0 +1,12 @@
<?php
namespace ProVM\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use ProVM\Common\Alias\View;
class Home {
public function __invoke(Request $request, Response $response, View $view) {
return $view->render($response, 'home');
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace ProVM\Common\Definition;
use Slim\Views\Blade;
use ProVM\Common\Alias\View as ViewInterface;
class View extends Blade implements ViewInterface {
}

21
common/Helper/Merger.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace ProVM\Common\Helper;
class Merger {
protected $separator;
public function __construct(string $separator) {
$this->separator = $separator;
}
protected $array;
public function start(): Merger {
$this->array = [];
return $this;
}
public function add($element): Merger {
$this->array []= $element;
return $this;
}
public function merge() {
return implode($this->separator, $this->array);
}
}

58
common/Helper/Tree.php Normal file
View File

@ -0,0 +1,58 @@
<?php
namespace ProVM\Common\Helper;
class Tree {
protected $merger;
public function __construct($separator) {
$this->merger = new Merger($separator);
}
protected $tree;
public function add($value, $parent = null, $alias = null): Tree {
if ($parent === null) {
if ($alias === null) {
$this->tree []= $value;
return $this;
}
$this->tree[$alias] = $value;
return $this;
}
if (!isset($this->tree[$parent])) {
return $this;
}
if ($alias === null) {
$this->tree []= $this->merger->start()
->add($this->tree[$parent])
->add($value)
->merge();
return $this;
}
$this->tree[$alias] = $this->merger->start()
->add($this->tree[$parent])
->add($value)
->merge();
return $this;
}
public function check_keys($tree) {
$bool = true;
foreach ($tree as $def) {
if (!isset($def[2])) {
continue;
}
$key = $def[2];
$bool &= array_key_exists($key, $this->tree);
}
return $bool;
}
public function addTree($tree) {
while (!$this->check_keys($tree)) {
foreach ($tree as $def) {
$this->add($def[0], $def[1] ?? null, $def[2] ?? null);
}
}
}
public function build($tree) {
$this->tree = [];
$this->addTree($tree);
return (object) $this->tree;
}
}