Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function _($x) {
- return htmlspecialchars($x);
- }
- function filter($x) {
- $r = array();
- foreach ($x as $v) {
- if ($v) array_push($r, $v);
- }
- return $r;
- }
- class Input {
- // The type of the input-field is stored in the attribute-slot.
- private $typeName;
- // This is an array of elements that will be rendered when the input-field
- // type requires sub-elements, such as the `select` input-field. The values
- // inserted here depend on the input-field type. They will be ignored when
- // the input-field type does not require sub-elements.
- private $subElements;
- // Attributes of the input-field are saved in this array.
- private $attributes;
- // This array contains all css-classes for the input-field and will be
- // treated special from the $attributes slot. The class-value generated
- // here will overwrite attributes set in the $attributes array.
- private $classes;
- public function __construct($typeName, $id=null, $class=null) {
- $this->typeName = $typeName;
- $this->subElements = array();
- $this->attributes = array();
- $this->classes = array();
- if ($id !== null)
- $this->setAttribute('id', $id);
- if ($class !== null)
- $this->setClasses(explode(' ', $class));
- }
- public function setClasses($classes) {
- if (!is_array($classes))
- throw new InvalidArgumentException('expected array.');
- $this->classes = $classes;
- }
- public function addClass(/* ... */) {
- $args = func_get_args();
- $this->addClassA($args);
- }
- public function addClassA($array) {
- foreach ($array as $className) {
- // Class-names with spaces in it will be added seperately, again.
- $multipleNames = filter(explode(' ', $className));
- if (count($multipleNames) > 1) {
- $this->addClassA($multipleNames);
- }
- elseif ($multipleNames) {
- $className = $multipleNames[0];
- $contained = array_search($className, $this->classes);
- if ($contained === false) {
- array_push($this->classes, $className);
- }
- }
- }
- }
- public function removeClass($className) {
- $index = array_search($className, $this->classes);
- if ($index !== false) {
- unset($this->classes[$index]);
- }
- }
- public function setAttribute($name, $value) {
- if ($name == 'class' or $name == 'type') {
- throw new LogicException('class and type attributes are treated differently.');
- }
- $this->attributes[$name] = $value;
- }
- // removeAttribute()
- // hasAttribute()
- public function render() {
- $string = '<input type="' . _($this->typeName) . '" ';
- // Render the class-names.
- if ($this->classes) {
- $classNames = implode(' ', $this->classes);
- $string .= 'class="' . $classNames . '" ';
- }
- // Render the rest of the attributes.
- foreach ($this->attributes as $key => $value) {
- $string .= $key . '="' . _($value) . '" ';
- }
- // Special treatment for `select`, etc..
- $string .= '/>';
- return $string;
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <body>
- <?php
- $i = new Input('text', 'myInput', 'foo bar');
- $i->addClass('foo', 'baar', 'spam FU', 'eggs');
- print $i->render();
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement