OpenSource CLI-App to install and handle stuff related to Web-Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

122 lines
2.4 KiB

<?php
namespace App\Menus;
use PhpSchool\CliMenu\MenuItem\StaticItem;
use Respect\Validation\Exceptions\NestedValidationException;
/**
*
*
*
*
*/
class ItemValidator
{
// items from menu
private $items;
// current message
private $message;
// validator
private $validator;
/**
*
*
*/
public function __construct($validator)
{
$this->validator = $validator;
$this->message = new StaticItem(''); // must create to compare in items
}
/**
* remove all items from menu and save them
*
* @param [type] $menu
*
*/
private function clear(&$menu)
{
$this->items = $menu->getItems();
foreach($menu->getItems() as $item) {
$menu->removeItem($item);
}
}
/**
* add staticItem after item
*
* @param [type] $menu
* @param [type] $after
* @param [type] $target
*
*/
private function addAfter(&$menu, $after, $target)
{
$this->clear($menu);
foreach($this->items as $item) {
$menu->addItem($item);
if ($after === $item) {
$menu->addItem($target, true);
}
}
}
/**
* remove staticItem
*
*
* @param [type] $menu [description]
* @param [type] $target [description]
*
*/
private function remove(&$menu, $target)
{
$this->clear($menu);
foreach($this->items as $item) {
if ($item === $target) {
continue;
}
$menu->addItem($item);
}
}
/**
* validate,
* if failed show staticItem with errors,
* if not remove staticItem if found
*
* @param [type] $menu
* @param [type] $item
* @param [type] $data
*
*/
public function validate(&$menu, $item, $data)
{
try {
$this->validator->assert($data);
} catch(NestedValidationException $exception) {
$errors = $exception->getMessages();
}
// remove message
$this->remove($menu, $this->message);
// if errors a set add message
if (isset($errors)) {
// @TODO use ColorUtil
$this->message->setText("\033[33m"."\xE2\x9A\xA0 ".join(' ', $errors));
$this->addAfter($menu, $item, $this->message);
}
}
}