Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Sample form helper. Review and add more later
  • Loading branch information
harikt committed Aug 19, 2012
1 parent 52bc40d commit dfeaf6c
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 0 deletions.
1 change: 1 addition & 0 deletions scripts/instance.php
Expand Up @@ -14,4 +14,5 @@
'scriptsFoot' => function() { return new \Aura\View\Helper\Scripts; },
'styles' => function() { return new \Aura\View\Helper\Styles; },
'title' => function() { return new \Aura\View\Helper\Title; },
'formtext' => function() { return new \Aura\View\Helper\Formtext; },
]));
2 changes: 2 additions & 0 deletions src.php
Expand Up @@ -24,3 +24,5 @@
require_once __DIR__ . '/src/Aura/View/Helper/Scripts.php';
require_once __DIR__ . '/src/Aura/View/Helper/Styles.php';
require_once __DIR__ . '/src/Aura/View/Helper/Title.php';
require_once __DIR__ . '/src/Aura/View/Helper/FormElement.php';
require_once __DIR__ . '/src/Aura/View/Helper/Formtext.php';
173 changes: 173 additions & 0 deletions src/Aura/View/Helper/FormElement.php
@@ -0,0 +1,173 @@
<?php
/**
*
* This file is part of the Aura Project for PHP.
*
* @package Aura.View
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\View\Helper;

/**
*
* Helper to generate `<a ... />` tags.
*
* @package Aura.View
*
*/
abstract class FormElement extends AbstractHelper
{
/**
*
* Default form element information.
*
* @var array
*
*/
protected $info = array(
'type' => '',
'name' => '',
'value' => '',
'label' => '',
'attribs' => '',
'options' => array(),
'require' => false,
'disable' => false,
'invalid' => array(),
);

/**
*
* The order in which to process info keys.
*
* Attribs is last so that attributes are unset properly.
*
* @var array
*
*/
protected $keys = array(
'type',
'name',
'value',
'label',
'options',
'require',
'disable',
'invalid',
'attribs',
);

/**
*
* The form element type (text, radio, etc).
*
* @var string
*
*/
protected $type;

/**
*
* The form element name.
*
* @var string
*
*/
protected $name;


/**
*
* The form element value.
*
* @var string
*
*/
protected $value;

/**
*
* The form element label.
*
* @var string
*
*/
protected $label;

/**
*
* The form element attributes (checked, selected, readonly, etc).
*
* @var array
*
*/

protected $attribs;

/**
*
* Options for checkbox, select, and radio elements.
*
* @var array
*
*/
protected $options;

/**
*
* Whether or not the element is required.
*
* @var bool
*
*/
protected $require;

/**
*
* Whether or not the element is to be disabled.
*
* @var bool
*
*/
protected $disable;

/**
*
* Feedback messages for the element.
*
* @var bool
*
*/
protected $invalid;

/**
*
* Prepares an info array and imports to the properties.
*
* @param array $info An array of element information.
*
* @return void
*
*/
protected function prepare($info)
{
$info = array_merge($this->info, $info);

settype($info['type'], 'string');
settype($info['name'], 'string');
settype($info['label'], 'string');
settype($info['attribs'], 'array');
settype($info['options'], 'array');
settype($info['require'], 'bool');
settype($info['disable'], 'bool');
settype($info['invalid'], 'array');

foreach ($this->keys as $key) {
unset($info['attribs'][$key]);
$this->$key = $info[$key];
}
}
}

41 changes: 41 additions & 0 deletions src/Aura/View/Helper/Formtext.php
@@ -0,0 +1,41 @@
<?php
/**
*
* This file is part of the Aura Project for PHP.
*
* @package Aura.View
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\View\Helper;

/**
*
* Helper to create a form of type text
*
* @package Aura.View
*
*/
class Formtext extends FormElement
{
/**
*
* Generates a 'text' element.
*
* @param array $info An array of element information.
*
* @return string The element XHTML.
*
*/
public function __invoke($info)
{
$this->prepare($info);
return '<input type="text"'
. ' name="' . $this->name . '"'
. ' value="' . $this->value . '"'
. $this->attribs($this->attribs)
. ' />';
}
}

0 comments on commit dfeaf6c

Please sign in to comment.