2018-10-05 21:32:15 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
|
|
|
*
|
2020-04-10 15:39:39 -04:00
|
|
|
* PHP version 7.4
|
2018-10-05 21:32:15 -04:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-01-08 15:39:49 -05:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
2018-10-05 21:32:15 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-08-04 09:30:21 -04:00
|
|
|
* @version 5.1
|
2018-10-05 21:32:15 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient;
|
|
|
|
|
2019-12-09 14:34:23 -05:00
|
|
|
use Aura\Html\HelperLocator;
|
2018-10-05 21:32:15 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2019-12-09 14:34:23 -05:00
|
|
|
use Aviat\Ion\Di\Exception\ContainerException;
|
|
|
|
use Aviat\Ion\Di\Exception\NotFoundException;
|
2018-10-05 21:32:15 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper object to manage form generation, especially for config editing
|
|
|
|
*/
|
|
|
|
final class FormGenerator {
|
|
|
|
/**
|
|
|
|
* Html generation helper
|
|
|
|
*
|
2019-12-09 14:34:23 -05:00
|
|
|
* @var HelperLocator
|
2018-10-05 21:32:15 -04:00
|
|
|
*/
|
2020-04-10 20:01:46 -04:00
|
|
|
private HelperLocator $helper;
|
2018-10-05 21:32:15 -04:00
|
|
|
|
2018-11-09 10:38:35 -05:00
|
|
|
/**
|
|
|
|
* FormGenerator constructor.
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
2018-11-09 10:38:35 -05:00
|
|
|
*/
|
2020-08-21 12:30:01 -04:00
|
|
|
private function __construct(ContainerInterface $container)
|
2018-10-05 21:32:15 -04:00
|
|
|
{
|
|
|
|
$this->helper = $container->get('html-helper');
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:30:01 -04:00
|
|
|
/**
|
|
|
|
* Create a new FormGenerator
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public static function new(ContainerInterface $container): self
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return new static($container);
|
|
|
|
}
|
|
|
|
catch (\Throwable $e)
|
|
|
|
{
|
|
|
|
dump($e);
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:32:15 -04:00
|
|
|
/**
|
|
|
|
* Generate the html structure of the form
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $form
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-11-09 10:38:35 -05:00
|
|
|
public function generate(string $name, array $form): string
|
2018-10-05 21:32:15 -04:00
|
|
|
{
|
|
|
|
$type = $form['type'];
|
2020-03-12 12:32:32 -04:00
|
|
|
$display = $form['display'] ?? TRUE;
|
|
|
|
$value = $form['value'] ?? '';
|
2018-10-05 21:32:15 -04:00
|
|
|
|
2020-03-12 12:32:32 -04:00
|
|
|
if ($display === FALSE)
|
2018-10-05 21:32:15 -04:00
|
|
|
{
|
2020-03-12 12:47:02 -04:00
|
|
|
return (string)$this->helper->input([
|
2018-10-05 21:32:15 -04:00
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => $name,
|
2020-03-12 12:32:32 -04:00
|
|
|
'value' => $value,
|
2018-10-05 21:32:15 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'name' => $name,
|
2020-03-12 12:32:32 -04:00
|
|
|
'value' => $value,
|
2018-10-05 21:32:15 -04:00
|
|
|
'attribs' => [
|
|
|
|
'id' => $name,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
switch($type)
|
|
|
|
{
|
|
|
|
case 'boolean':
|
|
|
|
$params['type'] = 'radio';
|
|
|
|
$params['options'] = [
|
|
|
|
'1' => 'Yes',
|
|
|
|
'0' => 'No',
|
|
|
|
];
|
|
|
|
unset($params['attribs']['id']);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'string':
|
|
|
|
$params['type'] = 'text';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'select':
|
|
|
|
$params['type'] = 'select';
|
|
|
|
$params['options'] = array_flip($form['options']);
|
|
|
|
break;
|
2020-04-10 20:01:46 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2018-10-05 21:32:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (['readonly', 'disabled'] as $key)
|
|
|
|
{
|
2020-03-12 12:32:32 -04:00
|
|
|
if (array_key_exists($key, $form) && $form[$key] !== FALSE)
|
2018-10-05 21:32:15 -04:00
|
|
|
{
|
|
|
|
$params['attribs'][$key] = $form[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 10:38:35 -05:00
|
|
|
return (string)$this->helper->input($params);
|
2018-10-05 21:32:15 -04:00
|
|
|
}
|
|
|
|
}
|