form/src/Element.php

67 lines
1.7 KiB
PHP
Raw Normal View History

2017-06-01 09:44:36 +00:00
<?php
namespace Krutush\Form;
class Element{
protected $data = array();
public function __construct(string $name){
$this->data['name'] = $name;
}
2017-06-10 15:21:03 +00:00
public function rename(string $name): self{
$this->data['name'] = $name;
return $this;
}
2017-06-01 09:44:36 +00:00
public function name() : string{ return $this->data['name']; }
2018-04-28 14:19:06 +00:00
public function id(string $id): self{
2018-05-07 12:11:15 +00:00
$this->data['id'] = $id;
2018-04-28 14:19:06 +00:00
return $this;
}
public function label(string $label, string $more = ''): self {
$this->data['label'] = $label;
$this->data['label.more'] = $more;
return $this;
}
2017-06-10 15:21:03 +00:00
public function required(bool $value = true) : self{
2017-06-01 09:44:36 +00:00
$this->data['required'] = $value;
return $this;
}
2018-04-25 17:15:57 +00:00
public function value(string $value = null) : self{
2017-06-01 09:44:36 +00:00
$this->data['value'] = $value;
return $this;
}
2018-04-25 17:15:57 +00:00
public function get(){
2017-06-01 09:44:36 +00:00
return $this->data['value'];
}
2017-06-10 15:21:03 +00:00
public function error(bool $value = true) : self{
2017-06-01 09:44:36 +00:00
$this->data['error'] = $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
if((!isset($data) || empty($data)) && isset($this->data['required']) && $this->data['required'] == true)
return 'requis';
return true;
}
2018-04-28 14:19:06 +00:00
protected function getId(): string{
return isset($this->data['id']) ? $this->data['id'] : $this->data['name'];
}
protected function htmlLabel(): string{
return isset($this->data['label']) ? '<label for="'.$this->getId().'" '.$this->data['label.more'].'>'.$this->data['label']."</label>\n" : '';
}
2017-06-01 09:44:36 +00:00
public function html(string $more = '') : string{
2018-04-28 14:19:06 +00:00
return $this->htmlLabel().'<span '.$more.'></span>';
2017-06-01 09:44:36 +00:00
}
}