Skip to content

Instantly share code, notes, and snippets.

@alixaxel
Last active December 11, 2015 20:59
Show Gist options
  • Save alixaxel/4659628 to your computer and use it in GitHub Desktop.
Save alixaxel/4659628 to your computer and use it in GitHub Desktop.
<?php
/**
* The MIT License
* http://creativecommons.org/licenses/MIT/
*
* Copyright (c) Alix Axel <[email protected]>
**/
class phunction_HTML_Form extends phunction_HTML
{
public function __construct()
{
}
public static function Checkbox($id, $value = null, $default = null)
{
$result = array('id' => $id, 'name' => $id, 'type' => 'checkbox', 'value' => $value);
if (strpbrk($id, '[]') !== false)
{
$id = str_replace(array('[]', '[', ']'), array('', '.', ''), $id);
}
if (in_array($value, (array) parent::Value($_REQUEST, $id, $default)) === true)
{
$result['checked'] = true;
}
return array_filter($result, 'strlen');
}
public static function Input($id, $default = null, $type = 'text')
{
$result = array('id' => $id, 'name' => $id, 'type' => $type, 'value' => $default);
if (strpbrk($id, '[]') !== false)
{
$id = str_replace(array('[]', '[', ']'), array('', '.', ''), $id);
}
if (($value = parent::Value($_REQUEST, $id)) !== false)
{
$result['value'] = htmlspecialchars_decode($value);
}
return array_filter($result, 'strlen');
}
public static function Option($id, $value = null, $default = null)
{
$result = array('value' => $value);
if (strpbrk($id, '[]') !== false)
{
$id = str_replace(array('[]', '[', ']'), array('', '.', ''), $id);
}
if (in_array($value, (array) parent::Value($_REQUEST, $id, $default)) === true)
{
$result['selected'] = true;
}
return array_filter($result, 'strlen');
}
public static function Radio($id, $value = null, $default = null)
{
$result = array('id' => $id, 'name' => $id, 'type' => 'radio', 'value' => $value);
if (strpbrk($id, '[]') !== false)
{
$id = str_replace(array('[]', '[', ']'), array('', '.', ''), $id);
}
if (in_array($value, (array) parent::Value($_REQUEST, $id, $default)) === true)
{
$result['checked'] = true;
}
return array_filter($result, 'strlen');
}
public static function Token($id)
{
return array('id' => $id, 'name' => $id, 'type' => 'hidden', 'value' => ph()->HTTP->Token($id));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment