Changed whole Input Objects

they now lives by themselves when before
they where highly connected to Form.php
This commit is contained in:
Florian Bouillon 2019-04-25 16:14:25 +02:00
parent 90a69fd16c
commit cacdc2c3d6
6 changed files with 190 additions and 55 deletions

View File

@ -2,20 +2,84 @@
namespace AdminPanel\Form;
class AbstractInput implements Input
use AdminPanel\AdminPanel;
/**
* this abstract class has default functionnality for all working
* before doing `parent::function` please see what it does
*/
abstract class AbstractInput implements Input
{
protected $attributes = array();
protected $options = array();
public function __construct()
{
$arr = explode('\\', get_class($this));
$this->setOption("type", strtolower(
str_replace(
"Input",
"",
$arr[count($arr) - 1]
)
));
}
public function getAttributesList(): array
{
return array(
'name',
'type',
'id',
'value',
'placeholder',
'style'
);
}
public function getOptionsList(): array
{
return array(
'label',
'value'
);
}
public function getAttributes(): array
{
return $this->attributes;
}
public function getOptions(): array
{
return array();
return $this->options;
}
public function processOption(string $optionName, $value): array
public function getOption(string $name)
{
return array($optionName => $value);
return $this->options[$name];
}
public function getTemplate(): string
public function setOption(string $name, $value)
{
if (in_array($name, $this->getOptionsList())) {
$this->options[$name] = $value;
}
if (in_array($name, $this->getAttributesList())) {
if (in_array($name, array("id", "name")) &&
!array_key_exists("id", $this->attributes) &&
!array_key_exists("name", $this->attributes)
) {
$this->attributes["name"] = $value;
$this->attributes["id"] = $value;
}
$this->attributes[$name] = $value;
}
}
public function render(): string
{
$arr = explode('\\', get_class($this));
$tpName = strtolower(
@ -25,6 +89,17 @@ class AbstractInput implements Input
$arr[count($arr) - 1]
)
);
return "@AdminPanel/form/" . $tpName . ".twig";
return $this->getTwig()->render(
"@AdminPanel/form/" . $tpName . ".twig",
array(
'attributes' => $this->attributes,
'options' => $this->options
)
);
}
protected function getTwig()
{
return AdminPanel::getInstance()->getTwig();
}
}

View File

@ -2,12 +2,25 @@
namespace AdminPanel\Form;
use AdminPanel\AdminPanel;
class ChoiceInput extends AbstractInput
{
public function getOptions(): array
public function getOptionsList(): array
{
return array_merge(
parent::getOptionsList(),
array(
'choices'
)
);
}
public function getAttributesList(): array
{
return array(
'choices'
'name',
'id'
);
}
}

View File

@ -2,6 +2,26 @@
namespace AdminPanel\Form;
use DateTime;
class DateInput extends AbstractInput
{
public function getOption(string $name)
{
if ($name === "value") {
return new DateTime($this->options["name"]);
} else {
return parent::getOption($name);
}
}
public function setOption($name, $value)
{
if ($name === "value") {
$this->attributes["value"] = (new DateTime($value))->format('Y-m-d');
} else {
parent::setOption($name, $value);
}
}
}

View File

@ -6,22 +6,40 @@ use AdminPanel\AdminPanel;
class EntityInput extends AbstractInput
{
public function getOptions(): array
public function __construct()
{
return array(
$this->setOption("type", "text");
}
public function getOptionsList(): array
{
return array_merge(
parent::getOptionsList(),
array(
'entity'
)
);
}
public function processOption(string $optionName, $value): array
public function setOption(string $name, $value)
{
if ($optionName === 'entity') {
return array(
'entities' => AdminPanel::getInstance()->getEm()->getRepository($value)->findAll()
);
if ($name === 'entity') {
$this->options["entities"] = AdminPanel::getInstance()->getEm()->getRepository($value)->findAll();
} elseif ($name === "name") {
$this->attributes["list"] = $value . "_list";
}
parent::setOption($name, $value);
}
public function getOption($name)
{
if ($name === "value") {
$repo = AdminPanel::getInstance()->getEm()->getRepository($this->options["entity"]);
return $repo->findOneBy(array(
'name' => $this->options["value"]
));
} else {
return parent::getOption($name);
}
return array(
$optionName => $value
);
}
}

View File

@ -4,29 +4,62 @@ namespace AdminPanel\Form;
interface Input
{
/**
* get definable options in form
* some are processed elsewhere like
* name, value
* Get list of used in this input
*
* @return array
* @return array array containing options
*/
public function getAttributesList(): array;
/**
* get options that will be passed into the final object
*
* @return void
*/
public function getAttributes(): array;
/**
* Get list of options usable by this input
*
* @return array array containing options
*/
public function getOptionsList(): array;
/**
* get options that will be passed into the final object
*
* @return void
*/
public function getOptions(): array;
/**
* run things when you got option
* get a single option that will be passed in the final object
* (option can either go in attributes or options)
*
* @param string $optionName
* @param mixed $value
* @param string $name option's name
*
* @return array
* @return mixed option's value
*/
public function processOption(string $optionName, $value): array;
public function getOption(string $name);
/**
* Get template file
* Process the options given by php or json
*
* (you can do whatever you want with it)
*
* @param string $name name of the option
* @param mixed $value value of element
*
* @return void
*/
public function setOption(string $name, $value);
/**
* return the html to render the current input
*
*
* @return string
*/
public function getTemplate(): string;
public function render(): string;
}

View File

@ -2,30 +2,6 @@
namespace AdminPanel\Form;
class TextInput implements Input
class TextInput extends AbstractInput
{
public function getOptions(): array
{
return array(
);
}
public function processOption(string $optionName, $value): array
{
return array($optionName => $value);
}
public function getTemplate(): string
{
$arr = explode('\\', self::class);
$tpName = strtolower(
str_replace(
"Input",
"",
$arr[count($arr) - 1]
)
);
return "@AdminPanel/form/" . $tpName . ".twig";
}
}