php-censor/src/Form/Input.php

197 lines
3.4 KiB
PHP
Raw Normal View History

2016-04-12 19:31:39 +02:00
<?php
2018-03-04 11:50:08 +01:00
namespace PHPCensor\Form;
2016-04-20 17:39:48 +02:00
2018-02-16 14:18:04 +01:00
use PHPCensor\View;
2016-04-12 19:31:39 +02:00
class Input extends Element
{
2017-11-05 15:48:36 +01:00
/**
* @var boolean
*/
2018-02-16 10:59:36 +01:00
protected $required = false;
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2018-02-16 10:59:36 +01:00
protected $pattern;
2017-11-05 15:48:36 +01:00
/**
* @var callable
*/
2018-02-16 10:59:36 +01:00
protected $validator;
2017-11-05 15:48:36 +01:00
/**
* @var mixed
*/
2018-01-18 15:51:33 +01:00
protected $value;
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2018-02-16 10:59:36 +01:00
protected $error;
2017-11-05 15:48:36 +01:00
/**
* @var boolean
*/
2018-02-16 10:59:36 +01:00
protected $customError = false;
2016-04-12 19:31:39 +02:00
2017-11-05 15:48:36 +01:00
/**
* @param string $name
* @param string $label
* @param boolean $required
*
* @return static
*/
2016-04-12 19:31:39 +02:00
public static function create($name, $label, $required = false)
{
$el = new static();
$el->setName($name);
$el->setLabel($label);
$el->setRequired($required);
return $el;
}
2017-11-05 15:48:36 +01:00
/**
* @return mixed
*/
2016-04-20 17:39:48 +02:00
public function getValue()
{
2018-01-18 15:51:33 +01:00
return $this->value;
2016-04-20 17:39:48 +02:00
}
2016-04-12 19:31:39 +02:00
2017-11-05 15:48:36 +01:00
/**
* @param mixed $value
*
* @return $this
*/
2016-04-20 17:39:48 +02:00
public function setValue($value)
{
2018-01-18 15:51:33 +01:00
$this->value = $value;
2017-11-05 15:48:36 +01:00
2016-04-12 19:31:39 +02:00
return $this;
2016-04-20 17:39:48 +02:00
}
2016-04-12 19:31:39 +02:00
2017-11-05 15:48:36 +01:00
/**
* @return boolean
*/
2016-04-20 17:39:48 +02:00
public function getRequired()
{
2018-02-16 10:59:36 +01:00
return $this->required;
2016-04-20 17:39:48 +02:00
}
2016-04-12 19:31:39 +02:00
2017-11-05 15:48:36 +01:00
/**
* @param boolean $required
*
* @return $this
*/
2016-04-20 17:39:48 +02:00
public function setRequired($required)
{
2018-02-16 10:59:36 +01:00
$this->required = (bool)$required;
2017-11-05 15:48:36 +01:00
2016-04-12 19:31:39 +02:00
return $this;
2016-04-20 17:39:48 +02:00
}
2016-04-12 19:31:39 +02:00
2017-11-05 15:48:36 +01:00
/**
* @return callable
*/
2016-04-20 17:39:48 +02:00
public function getValidator()
{
2018-02-16 10:59:36 +01:00
return $this->validator;
2016-04-20 17:39:48 +02:00
}
2016-04-12 19:31:39 +02:00
2017-11-05 15:48:36 +01:00
/**
* @param callable $validator
*
* @return $this
*/
2016-04-20 17:39:48 +02:00
public function setValidator($validator)
{
if (is_callable($validator) || $validator instanceof \Closure) {
2018-02-16 10:59:36 +01:00
$this->validator = $validator;
2016-04-20 17:39:48 +02:00
}
2016-04-12 19:31:39 +02:00
return $this;
2016-04-20 17:39:48 +02:00
}
2016-04-12 19:31:39 +02:00
2017-11-05 15:48:36 +01:00
/**
* @return string
*/
2016-04-20 17:39:48 +02:00
public function getPattern()
{
2018-02-16 10:59:36 +01:00
return $this->pattern;
2016-04-20 17:39:48 +02:00
}
2016-04-12 19:31:39 +02:00
2017-11-05 15:48:36 +01:00
/**
* @param string $pattern
*
* @return $this
*/
2016-04-20 17:39:48 +02:00
public function setPattern($pattern)
{
2018-02-16 10:59:36 +01:00
$this->pattern = $pattern;
2017-11-05 15:48:36 +01:00
2016-04-12 19:31:39 +02:00
return $this;
2016-04-20 17:39:48 +02:00
}
2017-11-05 15:48:36 +01:00
/**
* @return boolean
*/
2016-04-20 17:39:48 +02:00
public function validate()
{
2018-01-18 15:51:33 +01:00
if ($this->getRequired() && empty($this->value)) {
2018-02-16 10:59:36 +01:00
$this->error = $this->getLabel() . ' is required.';
2016-04-20 17:39:48 +02:00
return false;
}
2018-01-18 15:51:33 +01:00
if ($this->getPattern() && !preg_match('/' . $this->getPattern() . '/', $this->value)) {
2018-02-16 10:59:36 +01:00
$this->error = 'Invalid value entered.';
2017-11-05 15:48:36 +01:00
2016-04-20 17:39:48 +02:00
return false;
}
$validator = $this->getValidator();
if (is_callable($validator)) {
try {
2018-01-18 15:51:33 +01:00
call_user_func_array($validator, [$this->value]);
2016-04-20 17:39:48 +02:00
} catch (\Exception $ex) {
2018-02-16 10:59:36 +01:00
$this->error = $ex->getMessage();
2017-11-05 15:48:36 +01:00
2016-04-20 17:39:48 +02:00
return false;
}
}
2016-04-12 19:31:39 +02:00
2018-02-16 10:59:36 +01:00
if ($this->customError) {
2016-04-12 19:31:39 +02:00
return false;
}
2016-04-20 17:39:48 +02:00
return true;
}
2016-04-12 19:31:39 +02:00
2017-11-05 15:48:36 +01:00
/**
* @param string $message
*
* @return $this
*/
2016-04-12 19:31:39 +02:00
public function setError($message)
{
2018-02-16 10:59:36 +01:00
$this->customError = true;
$this->error = $message;
2017-11-05 15:48:36 +01:00
2016-04-12 19:31:39 +02:00
return $this;
}
2017-11-05 15:48:36 +01:00
/**
* @param View $view
*/
2017-01-13 16:35:41 +01:00
protected function onPreRender(View &$view)
2016-04-20 17:39:48 +02:00
{
2017-11-05 15:48:36 +01:00
$view->value = $this->getValue();
2018-02-16 10:59:36 +01:00
$view->error = $this->error;
$view->pattern = $this->pattern;
$view->required = $this->required;
2016-04-20 17:39:48 +02:00
}
2016-04-21 19:05:32 +02:00
}