<?php
/*
 * PHP::HTMLElements
 * (c) Mefi - 2010
 *
 * http://mefi.be/
 * zokni@mefi.be
 */

// CSS-osztály.
class HTMLElementCSS{

    private 
$properties = array();
    private 
$values = array();

    
// Tulajdonság hozzáadása.
    
public function addProperty($property$value) {
    if(
strlen($property) > && strlen($value) > 0) {
        
array_push($this->properties$property);
        
array_push($this->values$value);
    }
    }

    
// Tulajdonság eltávolítása.
    
public function removeProperty($property) {
    
$key array_search($property$this->properties);
    if(
$key !== false) {
        
$this->properties[$key] = null;
        
$this->values[$key] = null;
    }
    return 
false;
    }

    
// Stílus generálása.
    
public function getStyle($onlyStyle true) {
    if(
count($this->properties) > 0) {
        
$style null;
        
$i 0;
        foreach(
$this->properties as $p) {
        if(
$p !== null) {
            
$v $this->values[$i];
            
$style .= $p ':' $v ';';
        }
        
$i++;
        }
        return (
$onlyStyle) ? $style ' style="' $style '"';
    }
    else {
        return 
false;
    }
    }
}

// HTMLElement ősosztály.
abstract class HTMLElement {

    public 
$ID;
    public 
$style;
    public 
$class;
    
    private 
$name;
    private 
$value;
    private 
$innerHTML;

    private 
$selected;

    function 
setInnerHTML(){}
    function 
render($echo false){}

}

// <p>
class HTMLParagraph extends HTMLElement {

    
// Tartalom változtatása.
    
public function setInnerHTML($content) {
    
$this->innerHTML $content;
    }

    
// Renderelés.
    
public function render($echo false) {
    
$ID = ($this->ID !== null) ? ' id="' $this->ID '"' null;
    
$class = ($this->class !== null) ? ' class="'$this->class '"' :null;
    
$style $this->style->getStyle(false);
    
$element sprintf('<p%s%s%s>%s</p>'$ID$class$style$this->innerHTML);

    if(
$echo) {
        echo 
$element;
    }
    else {
        return 
$element;
    }
    }

    
// Konstruktor.
    
public function __construct ($innerHTML null$ID null$class null$render false$echo false) {
    
$this->setInnerHTML($innerHTML);
    
$this->ID $ID;
    
$this->class $class;
    
$this->style = new HTMLElementCSS();

    if(
$render) {
        
$this->render($echo);
    }
    }
}

//<option>
class HTMLSelectOption extends HTMLElement {

    
// Tartalom változtatása.
    
public function setInnerHTML($innerHTML) {
    
$this->innerHTML $innerHTML;
    }

    
// Renderelés.
    
public function render() {
    
$value = ($this->value !== null) ? ' value="' $this->value '"' null;
    
$selected = ($this->selected !== false) ? ' selected="selected"' null;
    
$element sprintf('<option%s%s>%s</option>'$value$selected$this->innerHTML);

        return 
$element;
    }

    
// Konstruktor.
    
public function __construct ($value null$innerHTML null$selected false) {
    
$this->value $value;
    
$this->setInnerHTML($innerHTML);
    
$this->selected $selected;
    }
}

//<select>
class HTMLSelect extends HTMLElement {

    
// Érték hozzáadása.
    
public function addOption($value$innerHTML$selected false) {
    if(
strlen($value) > && strlen($innerHTML) > 0) {
        
$option = new HTMLSelectOption($value$innerHTML$selected);
        
array_push($this->options$option);
        return 
true;
    }
    else {
        return 
false;
    }
    }

    
// Értékek renderelése.
    
public function getOptions() {
    
$optionValues null;
    foreach(
$this->options as $option) {
        
$optionValues .= $option->render();
    }
    return 
$optionValues;
    }

    
// Renderelés.
    
public function render($echo false) {
    
$ID = ($this->ID !== null) ? ' id="' $this->ID '"' null;
    
$name = ($this->name !== null) ? ' name="' $this->name '"' null;
    
$class = ($this->class !== null) ? ' class="'$this->class '"' :null;
    
$style $this->style->getStyle(false);
    
$options $this->getOptions();
    
$element sprintf('<select%s%s%s%s>%s</select>'$ID$name$class$style$options);

    if(
$echo) {
        echo 
$element;
    }
    else {
        return 
$element;
    }
    }

    
// Konstruktor.
    
public function __construct ($ID null$name null$class null) {
    
$this->ID $ID;
    
$this->name $name;
    
$this->class $class;

    
$this->style = new HTMLElementCSS();

    
$this->options = array();
    }
}


$fuckYeahSelect = new HTMLSelect('szelektem''szelektem''pina');
$fuckYeahSelect->addOption(1'ez egy elem');
$fuckYeahSelect->addOption(2'ez ki van valasztva'true);
$fuckYeahSelect->addOption(3'ez is az');
$fuckYeahSelect->render(true);

$fuckYeahPgs = new HTMLParagraph('Ez egy bekezdés.''fuckyeahpgs''p'truetrue);
$fuckYeahPgs->style->addProperty('color''#000');
$fuckYeahPgs->style->addProperty('font-weight''bold');
$fuckYeahPgs->render(true);

?>