diff --git a/composer.json b/composer.json
index 36dbb51..7fbcfde 100644
--- a/composer.json
+++ b/composer.json
@@ -6,5 +6,14 @@
"name": "sheychen",
"email": "contact@clementbois.fr"
}
- ]
+ ],
+ "require": {
+ //PHP7.1
+ "krutush/template": "dev-master"
+ },
+ "autoload": {
+ "psr-4": {
+ "Krutush\\Form\\": "src/"
+ }
+ }
}
\ No newline at end of file
diff --git a/src/Element.php b/src/Element.php
new file mode 100644
index 0000000..b156e6e
--- /dev/null
+++ b/src/Element.php
@@ -0,0 +1,43 @@
+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 '';
+ }
+}
\ No newline at end of file
diff --git a/src/Form.php b/src/Form.php
new file mode 100644
index 0000000..0aa6a87
--- /dev/null
+++ b/src/Form.php
@@ -0,0 +1,137 @@
+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 '
';
+ }
+
+ public function errors(string $more = '') : string{
+ if(empty($this->errors))
+ return '';
+
+ $html = '';
+ foreach($this->errors as $error){
+ $html .= '
'.$error.'
';
+ }
+ return $html.'
';
+ }
+
+ public function submit(string $name = null, string $more = '') : string{
+ return '';
+ }
+
+ 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;
+ }
+}
\ No newline at end of file
diff --git a/src/Input.php b/src/Input.php
new file mode 100644
index 0000000..7aee67d
--- /dev/null
+++ b/src/Input.php
@@ -0,0 +1,105 @@
+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 '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.'>';
+ }
+}
\ No newline at end of file
diff --git a/src/Select.php b/src/Select.php
new file mode 100644
index 0000000..d64e3d2
--- /dev/null
+++ b/src/Select.php
@@ -0,0 +1,48 @@
+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 = '';
+ }
+}
\ No newline at end of file
diff --git a/src/TextArea.php b/src/TextArea.php
new file mode 100644
index 0000000..89a8372
--- /dev/null
+++ b/src/TextArea.php
@@ -0,0 +1,16 @@
+data['name'].'" '.
+ (isset($this->data['value']) ? 'value="'.$this->data['value'].'" ' : '').
+ (isset($this->data['required']) && $this->data['required'] == true ? 'required ' : '').
+ $more.'>';
+ }
+}
\ No newline at end of file