2016-04-12 19:31:39 +02:00
|
|
|
<?php
|
|
|
|
|
2018-03-04 11:50:08 +01:00
|
|
|
namespace PHPCensor\Form;
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2018-02-16 14:18:04 +01:00
|
|
|
use PHPCensor\View;
|
2016-04-12 19:31:39 +02:00
|
|
|
|
|
|
|
abstract class Element
|
|
|
|
{
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-01-18 15:51:33 +01:00
|
|
|
protected $name;
|
2017-11-05 15:48:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-01-18 15:51:33 +01:00
|
|
|
protected $id;
|
2017-11-05 15:48:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-01-18 15:51:33 +01:00
|
|
|
protected $label;
|
2017-11-05 15:48:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-01-18 15:51:33 +01:00
|
|
|
protected $class;
|
2017-11-05 15:48:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-01-18 15:51:33 +01:00
|
|
|
protected $containerClass;
|
2017-11-05 15:48:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Element
|
|
|
|
*/
|
2018-01-18 15:51:33 +01:00
|
|
|
protected $parent;
|
2016-04-21 19:05:32 +02:00
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @param string|null $name
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function __construct($name = null)
|
|
|
|
{
|
|
|
|
if (!is_null($name)) {
|
|
|
|
$this->setName($name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function getName()
|
|
|
|
{
|
2018-01-18 15:51:33 +01:00
|
|
|
return $this->name;
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function setName($name)
|
|
|
|
{
|
2018-01-18 15:51:33 +01:00
|
|
|
$this->name = strtolower(preg_replace('/([^a-zA-Z0-9_\-%])/', '', $name));
|
|
|
|
|
2016-04-12 19:31:39 +02:00
|
|
|
return $this;
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function getId()
|
|
|
|
{
|
2018-01-18 15:51:33 +01:00
|
|
|
return !$this->id
|
|
|
|
? ('element-' . $this->name)
|
|
|
|
: $this->id;
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $id
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function setId($id)
|
|
|
|
{
|
2018-01-18 15:51:33 +01:00
|
|
|
$this->id = $id;
|
|
|
|
|
2016-04-12 19:31:39 +02:00
|
|
|
return $this;
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function getLabel()
|
|
|
|
{
|
2018-01-18 15:51:33 +01:00
|
|
|
return $this->label;
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $label
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function setLabel($label)
|
|
|
|
{
|
2018-01-18 15:51:33 +01:00
|
|
|
$this->label = $label;
|
|
|
|
|
2016-04-12 19:31:39 +02:00
|
|
|
return $this;
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function getClass()
|
|
|
|
{
|
2018-01-18 15:51:33 +01:00
|
|
|
return $this->class;
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function setClass($class)
|
|
|
|
{
|
2018-01-18 15:51:33 +01:00
|
|
|
$this->class = $class;
|
|
|
|
|
2016-04-12 19:31:39 +02:00
|
|
|
return $this;
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function getContainerClass()
|
|
|
|
{
|
2018-01-18 15:51:33 +01:00
|
|
|
return $this->containerClass;
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function setContainerClass($class)
|
|
|
|
{
|
2018-01-18 15:51:33 +01:00
|
|
|
$this->containerClass = $class;
|
|
|
|
|
2016-04-12 19:31:39 +02:00
|
|
|
return $this;
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @param Element $parent
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function setParent(Element $parent)
|
|
|
|
{
|
2018-01-18 15:51:33 +01:00
|
|
|
$this->parent = $parent;
|
|
|
|
|
2016-04-12 19:31:39 +02:00
|
|
|
return $this;
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $viewFile
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-21 19:05:32 +02:00
|
|
|
public function render($viewFile = null)
|
|
|
|
{
|
|
|
|
if (is_null($viewFile)) {
|
2017-11-05 15:48:36 +01:00
|
|
|
$class = explode('\\', get_called_class());
|
2016-04-21 19:05:32 +02:00
|
|
|
$viewFile = end($class);
|
|
|
|
}
|
|
|
|
|
2018-03-04 12:04:15 +01:00
|
|
|
$view = new View('Form/' . $viewFile);
|
2016-04-21 19:05:32 +02:00
|
|
|
|
2018-01-18 15:51:33 +01:00
|
|
|
$view->name = $this->getName();
|
|
|
|
$view->id = $this->getId();
|
|
|
|
$view->label = $this->getLabel();
|
|
|
|
$view->class = $this->getClass();
|
|
|
|
$view->containerClass = $this->getContainerClass();
|
|
|
|
$view->parent = $this->parent;
|
2016-04-21 19:05:32 +02:00
|
|
|
|
2017-01-13 16:35:41 +01:00
|
|
|
$this->onPreRender($view);
|
2016-04-21 19:05:32 +02:00
|
|
|
|
|
|
|
return $view->render();
|
|
|
|
}
|
|
|
|
|
2017-11-05 15:48:36 +01:00
|
|
|
/**
|
|
|
|
* @param View $view
|
|
|
|
*/
|
2017-01-13 16:35:41 +01:00
|
|
|
abstract protected function onPreRender(View &$view);
|
2016-04-21 19:05:32 +02:00
|
|
|
}
|