2017-06-01 09:44:36 +00:00
|
|
|
<?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;
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:21:03 +00:00
|
|
|
public function checkbox(): Input{
|
|
|
|
$this->data['type'] = 'checkbox';
|
|
|
|
$this->data['checkbox'] = true;
|
|
|
|
$this->data['value'] = $this->data['name'];
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-04-28 10:06:00 +00:00
|
|
|
public function number(): Input{
|
|
|
|
$this->data['type'] = 'number';
|
|
|
|
$this->data['number'] = true;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-05-03 11:43:51 +00:00
|
|
|
public function date(): Input{
|
|
|
|
$this->data['type'] = 'date';
|
|
|
|
$this->data['date'] = true;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function time(): Input{
|
|
|
|
$this->data['type'] = 'time';
|
|
|
|
$this->data['time'] = true;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-05-07 12:11:15 +00:00
|
|
|
public function min(string $value) : Input{
|
2018-04-28 10:06:00 +00:00
|
|
|
$this->data['min'] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-05-07 12:11:15 +00:00
|
|
|
public function max(string $value) : Input{
|
2018-04-28 10:06:00 +00:00
|
|
|
$this->data['max'] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-04-27 11:07:46 +00:00
|
|
|
public function password(bool $complexity = false): Input{
|
|
|
|
$this->data['type'] = 'password';
|
|
|
|
$this->data['password'] = true;
|
|
|
|
if($complexity){
|
2018-05-01 16:59:15 +00:00
|
|
|
$regex = '^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).{8,}$';
|
2018-04-27 11:07:46 +00:00
|
|
|
$this->data['pattern'] = $regex;
|
|
|
|
$this->data['regex'] = $regex;
|
|
|
|
$this->data['title'] = 'Mot de passe trop simple';
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-06-01 09:44:36 +00:00
|
|
|
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';
|
2018-05-13 11:42:26 +00:00
|
|
|
$this->data['alpha'] = $value; //TODO: add parttern
|
2017-06-01 09:44:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-25 17:15:57 +00:00
|
|
|
public function valid($data)/*: bool|string*/{
|
2017-06-01 09:44:36 +00:00
|
|
|
$parent = parent::valid($data);
|
|
|
|
|
|
|
|
if($parent !== true || !isset($data))
|
|
|
|
return $parent;
|
|
|
|
|
|
|
|
if(!empty($data)){
|
2018-05-13 11:42:26 +00:00
|
|
|
if(isset($this->data['phone']) && $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';
|
|
|
|
|
|
|
|
if(isset($this->data['number']) && $this->data['number'] == true && !ctype_digit($data))
|
|
|
|
return 'non numérique';
|
|
|
|
|
|
|
|
if(isset($this->data['date']) && $this->data['date'] == true){
|
|
|
|
$d = \DateTime::createFromFormat('Y-m-d', $data);
|
|
|
|
if(!$d || $d->format('Y-m-d') != $data)
|
2017-06-01 09:44:36 +00:00
|
|
|
return 'incorrect';
|
2018-05-13 11:42:26 +00:00
|
|
|
}
|
|
|
|
if(isset($this->data['time']) && $this->data['time'] == true){
|
|
|
|
$t = \DateTime::createFromFormat('H:i', $data);
|
|
|
|
if(!$t || $t->format('H:i') != $data)
|
2017-06-01 09:44:36 +00:00
|
|
|
return 'incorrect';
|
|
|
|
}
|
2018-05-13 11:42:26 +00:00
|
|
|
if(isset($this->data['min']) && $data < $this->data['min'])
|
|
|
|
return 'trop petit';
|
|
|
|
|
|
|
|
if(isset($this->data['max']) && $data > $this->data['max'])
|
|
|
|
return 'trop grand';
|
|
|
|
|
|
|
|
if(isset($this->data['email']) && $this->data['email'] == true && !filter_var($data, FILTER_VALIDATE_EMAIL))
|
|
|
|
return 'incorrect';
|
|
|
|
|
|
|
|
if(isset($this->data['minlength']) && strlen($data) < $this->data['minlength'])
|
|
|
|
return 'trop court';
|
|
|
|
|
|
|
|
if(isset($this->data['maxlength']) && strlen($data) > $this->data['maxlength'])
|
|
|
|
return 'trop long';
|
|
|
|
|
|
|
|
if(isset($this->data['alpha']) && !preg_match('#^[\p{L}'.$this->data['alpha'].']*$#', $data))
|
|
|
|
return 'non alphabétique';
|
|
|
|
|
|
|
|
if(isset($this->data['alphanum']) && !preg_match('#^[\p{L}\p{N}'.$this->data['alphanum'].']*$#', $data))
|
|
|
|
return 'non alphanumérique';
|
|
|
|
|
|
|
|
if(isset($this->data['regex']) && !preg_match('#'.$this->data['regex'].'#', $data))
|
|
|
|
return 'incorrect';
|
|
|
|
|
2017-06-01 09:44:36 +00:00
|
|
|
}
|
|
|
|
return $parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function html(string $more = '') : string{
|
2018-04-28 14:19:06 +00:00
|
|
|
return $this->htmlLabel().
|
|
|
|
'<input name="'.$this->data['name'].'" '.
|
|
|
|
'id="'.$this->getId().'" '.
|
2018-04-27 11:07:46 +00:00
|
|
|
(isset($this->data['value']) && !(isset($this->data['password']) && $this->data['password'] == true) ? 'value="'.$this->data['value'].'" ' : '').
|
2017-06-01 09:44:36 +00:00
|
|
|
(isset($this->data['type']) ? 'type="'.$this->data['type'].'" ' : '').
|
|
|
|
(isset($this->data['title']) ? 'title="'.$this->data['title'].'" ' : '').
|
|
|
|
(isset($this->data['pattern']) ? 'pattern="'.$this->data['pattern'].'" ' : '').
|
2018-04-28 10:12:08 +00:00
|
|
|
(isset($this->data['min']) ? 'min="'.$this->data['min'].'" ' : '').
|
|
|
|
(isset($this->data['max']) ? 'max="'.$this->data['max'].'" ' : '').
|
2017-06-01 09:44:36 +00:00
|
|
|
(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.'>';
|
|
|
|
}
|
|
|
|
}
|