Basic Version

master
sheychen 2017-06-01 11:44:36 +02:00
parent 5778aab9f3
commit 89077f4548
6 changed files with 359 additions and 1 deletions

View File

@ -6,5 +6,14 @@
"name": "sheychen",
"email": "contact@clementbois.fr"
}
]
],
"require": {
//PHP7.1
"krutush/template": "dev-master"
},
"autoload": {
"psr-4": {
"Krutush\\Form\\": "src/"
}
}
}

43
src/Element.php Normal file
View File

@ -0,0 +1,43 @@
<?php
namespace Krutush\Form;
class Element{
protected $data = array();
public function __construct(string $name){
$this->data['name'] = $name;
}
public function name() : string{ return $this->data['name']; }
public function required(bool $value = true) : Element{
$this->data['required'] = $value;
return $this;
}
public function value(string $value) : Element{
$this->data['value'] = $value;
return $this;
}
public function get() : ?string{
return $this->data['value'];
}
public function error(bool $value = true) : Element{
$this->data['error'] = $value;
return $this;
}
public function valid(mixed $data)/* :bool|string */{
if((!isset($data) || empty($data)) && isset($this->data['required']) && $this->data['required'] == true)
return 'requis';
return true;
}
public function html(string $more = '') : string{
return '<span '.$more.'></span>';
}
}

137
src/Form.php Normal file
View File

@ -0,0 +1,137 @@
<?php
namespace Krutush\Form;
use Krutush\Template\Html;
class Form {
private $method;
private $url;
private $elements = array();
private $name;
private $errors = array();
private $set = false;
public function __construct(string $name, string $path, bool $extention = true, bool $folder = true){
$this->name = $name;
$tpl = new Html($path, $extention, $folder);
$tpl->set($name, $this)
->run('buffer');
return $this;
}
public static function sanitize(array $data) : array{
$return = array();
foreach($data as $key => $value){
if(is_string($value))
$return[$key] = strip_tags(trim($value));
}
return $return;
}
public function valid(array $data) : bool{
$data = static::sanitize($data);
$this->set = true;
$valid = true;
foreach($this->elements as $element){
$value = isset($data[$element->name()]) ? $data[$element->name()] : null;
$return = $element->valid($value);
if($return !== true){
$this->errors[] = 'Le champ '.$element->name().' est '.$return.'.';
$valid = false;
}else{
$element->value($value);
}
}
return $valid;
}
public function name() : string{
return $this->name;
}
public function start(string $more = '', string $method = 'post', string $url = null) : string{
if(!in_array($method, array('post', 'get')))
$method = 'post';
if($this->set == false){
$this->method = $method;
$this->url = $url;
}
return '<form method="'.$method.'" '.(isset($url) ? 'action="'.$url.'" ' : '').' '.$more.'>';
}
public function end(string $more = '') : string{
return '</form '.$more.'>';
}
public function errors(string $more = '') : string{
if(empty($this->errors))
return '';
$html = '<div class="errors" '.$more.'>';
foreach($this->errors as $error){
$html .= '<p>'.$error.'</p>';
}
return $html.'</div>';
}
public function submit(string $name = null, string $more = '') : string{
return '<input type="submit" '.(isset($name) ? 'value="'.$name.'" ' : '').$more.'>';
}
function input(string $name) : Element{
if($this->set == true){
$input = $this->get($name);
if(isset($input))
return $input;
}
$input = new Input($name);
$this->add($input);
return $input;
}
function select(string $name) : Element{
if($this->set == true){
$input = $this->get($name);
if(isset($input))
return $input;
}
$input = new Select($name);
$this->add($input);
return $input;
}
function textarea(string $name) : Element{
if($this->set == true){
$input = $this->get($name);
if(isset($input))
return $input;
}
$input = new TextArea($name);
$this->add($input);
return $input;
}
public function add(Element $thing) : void{
if($this->set == false)
$this->elements[] = $thing;
}
public function get(string $name) : ?Element{
foreach($this->elements as $element){
if($element->name() == $name)
return $element;
}
return null;
}
public function values(bool $nullToEmpty = false) : array{
$values = array();
foreach($this->elements as $element){
$value = $element->get();
$values[$element->name()] = $nullToEmpty && !isset($value) ? '' : $value;
}
return $values;
}
}

105
src/Input.php Normal file
View File

@ -0,0 +1,105 @@
<?php
namespace Krutush\Form;
class Input extends Element{
public function text() : Input{
$this->data['type'] = 'text';
return $this;
}
public function phone() : Input{
$this->data['type'] = 'tel';
$this->data['pattern'] = "^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$";
$this->data['phone'] = true;
return $this;
}
public function email() : Input{
$this->data['type'] = 'email';
$this->data['email'] = true;
return $this;
}
public function minlength(int $value) : Input{
$this->data['minlength'] = $value;
return $this;
}
public function maxlength(int $value) : Input{
$this->data['maxlength'] = $value;
return $this;
}
public function alpha(string $value = '') : Input{
$this->data['type'] = 'text';
$this->data['title'] = 'Alphabétique';
$this->data['alpha'] = $value;
return $this;
}
public function alphanum(string $value = '') : Input{
$this->data['type'] = 'text';
$this->data['title'] = 'Alphanumérique';
$this->data['alphanum'] = $value;
return $this;
}
public function regex(string $value) : Input{
$this->data['type'] = 'text';
$this->data['pattern'] = $value;
$this->data['regex'] = $value;
return $this;
}
public function title(string $value) : Input{
$this->data['title'] = $value;
return $this;
}
public function valid(mixde $data)/*: bool|string*/{
$parent = parent::valid($data);
if($parent !== true || !isset($data))
return $parent;
if(!empty($data)){
if(isset($this->data['phone'])){
if($this->data['phone'] == true && !preg_match("#^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$#", $data))
return 'incorrect';
}else if(isset($this->data['email'])){
if($this->data['email'] == true && !filter_var($data, FILTER_VALIDATE_EMAIL))
return 'incorrect';
}else if(isset($this->data['minlength'])){
if(strlen($data) < $this->data['minlength'])
return 'trop court';
}else if(isset($this->data['maxlength'])){
if(strlen($data) > $this->data['maxlength'])
return 'trop long';
}else if(isset($this->data['alpha'])){
if(!preg_match('#^[\p{L}'.$this->data['alpha'].']*$#', $data))
return 'non alphabétique';
}else if(isset($this->data['alphanum'])){
if(!preg_match('#^[\p{L}\p{N}'.$this->data['alphanum'].']*$#', $data))
return 'non alphanumérique';
}else if(isset($this->data['regex'])){
if(!preg_match('#'.$this->data['regex'].'#', $data))
return 'incorrect';
}
}
return $parent;
}
public function html(string $more = '') : string{
return '<input name="'.$this->data['name'].'" '.
(isset($this->data['value']) ? 'value="'.$this->data['value'].'" ' : '').
(isset($this->data['type']) ? 'type="'.$this->data['type'].'" ' : '').
(isset($this->data['title']) ? 'title="'.$this->data['title'].'" ' : '').
(isset($this->data['pattern']) ? 'pattern="'.$this->data['pattern'].'" ' : '').
(isset($this->data['pattern']) ? 'pattern="'.$this->data['pattern'].'" ' : '').
(isset($this->data['minlength']) ? 'minlength="'.$this->data['minlength'].'" ' : '').
(isset($this->data['maxlength']) ? 'maxlength="'.$this->data['maxlength'].'" ' : '').
(isset($this->data['required']) && $this->data['required'] == true ? 'required ' : '').
$more.'>';
}
}

48
src/Select.php Normal file
View File

@ -0,0 +1,48 @@
<?php
namespace Krutush\Form;
class Select extends Element{
public function option(string $value, string $text = null, string $more = '') : Select{
$this->data['options'][] = array(
'value' => $value,
'text' => isset($text) ? $text : $value,
'more' => $more
);
return $this;
}
public function options(array $options) : Select{
foreach($options as $option){
if(is_string($option)){
$this->option($option);
continue;
}
$this->data['options'][] = $option; //TODO convert to $this->option
}
return $this;
}
public function valid(mixed $data)/*: bool|string*/{
$parent = parent::valid($data);
if($parent !== true || !isset($data))
return $parent;
foreach($this->data['options'] as $option){
if($option['value'] == $data)
return $parent;
}
return 'incorrect';
}
public function html(string $more = '') : string{
$html = '<select name="'.$this->data['name'].'" '.
(isset($this->data['required']) && $this->data['required'] == true ? 'required ' : '').
$more.'><option disabled '.(isset($this->data['value']) ? 'selected ' : '').'value style="display:none"> --- </option>';
foreach($this->data['options'] as $option){
$html .= '<option value="'.$option['value'].'" '.((isset($this->data['value']) && $this->data['value'] == $option['value']) ? 'selected="selected" ' : '' ).$option['more'].'>'.$option['text'].'</option>';
}
return $html.'</select>';
}
}

16
src/TextArea.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace Krutush\Form;
class TextArea extends Element{
public function valid(mixed $data)/*: bool|string*/{
return parent::valid($data);
}
public function html(string $more = '') : string{
return '<textarea name="'.$this->data['name'].'" '.
(isset($this->data['value']) ? 'value="'.$this->data['value'].'" ' : '').
(isset($this->data['required']) && $this->data['required'] == true ? 'required ' : '').
$more.'></textarea>';
}
}