Pop\Form\Element::__construct PHP Method

__construct() public method

Instantiate the form element object
public __construct ( string $type, string $name, string $value = null, string | array $marked = null, string $indent = null ) : Element
$type string
$name string
$value string
$marked string | array
$indent string
return Element
    public function __construct($type, $name, $value = null, $marked = null, $indent = null)
    {
        $this->name = $name;
        $this->type = $type;
        // Check the element type, else set the properties.
        if (!in_array($type, $this->allowedTypes)) {
            throw new Exception('Error: That input type is not allowed.');
        }
        // Create the element based on the type passed.
        switch ($type) {
            // Textarea element
            case 'textarea':
                parent::__construct('textarea', null, null, false, $indent);
                $this->setAttributes(array('name' => $name, 'id' => $name));
                $this->nodeValue = $value;
                $this->value = $value;
                break;
                // Select element
            // Select element
            case 'select':
                parent::__construct('select', null, null, false, $indent);
                $this->setAttributes(array('name' => $name, 'id' => $name));
                // Create the child option elements.
                foreach ($value as $k => $v) {
                    if (is_array($v)) {
                        $opt = new Child('optgroup', null, null, false, $indent);
                        $opt->setAttributes('label', $k);
                        foreach ($v as $ky => $vl) {
                            $o = new Child('option', null, null, false, $indent);
                            $o->setAttributes('value', $ky);
                            // Determine if the current option element is selected.
                            if (is_array($this->marked)) {
                                if (in_array($ky, $this->marked)) {
                                    $o->setAttributes('selected', 'selected');
                                }
                            } else {
                                if ($ky == $this->marked) {
                                    $o->setAttributes('selected', 'selected');
                                }
                            }
                            $o->setNodeValue($vl);
                            $opt->addChild($o);
                        }
                    } else {
                        $opt = new Child('option', null, null, false, $indent);
                        $opt->setAttributes('value', $k);
                        // Determine if the current option element is selected.
                        if (is_array($this->marked)) {
                            if (in_array($k, $this->marked)) {
                                $opt->setAttributes('selected', 'selected');
                            }
                        } else {
                            if ($k == $this->marked) {
                                $opt->setAttributes('selected', 'selected');
                            }
                        }
                        $opt->setNodeValue($v);
                    }
                    $this->addChild($opt);
                }
                $this->value = $value;
                break;
                // Radio element(s)
            // Radio element(s)
            case 'radio':
                parent::__construct('fieldset', null, null, false, $indent);
                $this->setAttributes('class', 'radio-btn-fieldset');
                // Create the radio elements and related span elements.
                $i = null;
                foreach ($value as $k => $v) {
                    $rad = new Child('input', null, null, false, $indent);
                    $rad->setAttributes(array('type' => $type, 'class' => 'radio-btn', 'name' => $name, 'id' => $name . $i, 'value' => $k));
                    // Determine if the current radio element is checked.
                    if ($k == $this->marked) {
                        $rad->setAttributes('checked', 'checked');
                    }
                    $span = new Child('span', null, null, false, $indent);
                    $span->setAttributes('class', 'radio-span');
                    $span->setNodeValue($v);
                    $this->addChildren(array($rad, $span));
                    $i++;
                }
                $this->value = $value;
                break;
                // Checkbox element(s)
            // Checkbox element(s)
            case 'checkbox':
                parent::__construct('fieldset', null, null, false, $indent);
                $this->setAttributes('class', 'check-box-fieldset');
                // Create the checkbox elements and related span elements.
                $i = null;
                foreach ($value as $k => $v) {
                    $chk = new Child('input', null, null, false, $indent);
                    $chk->setAttributes(array('type' => $type, 'class' => 'check-box', 'name' => $name . '[]', 'id' => $name . $i, 'value' => $k));
                    // Determine if the current radio element is checked.
                    if (in_array($k, $this->marked)) {
                        $chk->setAttributes('checked', 'checked');
                    }
                    $span = new Child('span', null, null, false, $indent);
                    $span->setAttributes('class', 'check-span');
                    $span->setNodeValue($v);
                    $this->addChildren(array($chk, $span));
                    $i++;
                }
                $this->value = $value;
                break;
                // Input element
            // Input element
            default:
                if ($type == 'button') {
                    $nodeType = 'button';
                    $type = 'submit';
                } else {
                    $nodeType = 'input';
                }
                parent::__construct($nodeType, null, null, false, $indent);
                $this->setAttributes(array('type' => $type, 'name' => $name, 'id' => $name));
                if (!is_array($value)) {
                    if ($nodeType == 'button') {
                        $this->nodeValue = $value;
                    }
                    $this->setAttributes('value', $value);
                }
                $this->value = $value;
        }
        // If a certain value is marked (selected or checked), set the property here.
        if (null !== $marked) {
            $this->marked = $marked;
        }
    }

Usage Example

コード例 #1
0
ファイル: Csrf.php プロジェクト: akinyeleolubodun/PhireCMS2
 /**
  * Constructor
  *
  * Instantiate the CSRF form element object.
  *
  * @param  string $name
  * @param  string $value
  * @param  int    $expire
  * @param  string $indent
  * @return \Pop\Form\Element\Csrf
  */
 public function __construct($name, $value = null, $expire = 300, $indent = null)
 {
     // Start a session.
     if (session_id() == '') {
         session_start();
     }
     // If token does not exist, create one
     if (!isset($_SESSION['pop_csrf'])) {
         $this->token = array('value' => sha1(rand(10000, getrandmax()) . $value), 'expire' => (int) $expire, 'start' => time());
         $_SESSION['pop_csrf'] = serialize($this->token);
         // Else, retrieve existing token
     } else {
         $this->token = unserialize($_SESSION['pop_csrf']);
         // Check to see if the token has expired
         if ($this->token['expire'] > 0) {
             if ($this->token['expire'] + $this->token['start'] < time()) {
                 $this->token = array('value' => sha1(rand(10000, getrandmax()) . $value), 'expire' => (int) $expire, 'start' => time());
                 $_SESSION['pop_csrf'] = serialize($this->token);
             }
         }
     }
     parent::__construct('hidden', $name, $this->token['value'], null, $indent);
     $this->setRequired(true);
     $this->setValidator();
 }
All Usage Examples Of Pop\Form\Element::__construct