diff --git a/FormBuilder.php b/FormBuilder.php index 8d666e2..676fdf2 100644 --- a/FormBuilder.php +++ b/FormBuilder.php @@ -76,7 +76,7 @@ class FormBuilder { */ public function open(array $options = array()) { - $method = strtoupper(array_get($options, 'method', 'post')); + $method = array_get($options, 'method', 'post'); // We need to extract the proper method from the attributes. If the method is // something other than GET or POST we'll use POST since we will spoof the @@ -101,10 +101,17 @@ class FormBuilder { // format the array of attributes. We will also add on the appendage which // is used to spoof the requests for PUT and DELETE requests to the app. $attributes = array_merge( + $attributes, array_except($options, $this->reserved) + ); - return ''.$append; + // Finally, we will concatenate all of the attributes into a single string so + // we can build out the final form open statement. We'll also append on an + // extra value for the hidden _method field if it's needed for the form. + $attributes = Html::attributes($attributes); + + return ''.$append; } /** @@ -150,16 +157,16 @@ class FormBuilder { * * @param string $name * @param string $value - * @param array $attributes + * @param array $options * @return string */ - public function label($name, $value, $attributes = array()) + public function label($name, $value, $options = array()) { $this->labels[] = $name; - $attributes = Html::attributes($attributes); + $options = Html::attributes($options); - return ''; + return ''; } /** @@ -168,17 +175,17 @@ class FormBuilder { * @param string $type * @param string $name * @param string $value - * @param array $attributes + * @param array $options * @return string */ - public function input($type, $name, $value = null, $attributes = array()) + public function input($type, $name, $value = null, $options = array()) { - $attributes['name'] = $name; + $options['name'] = $name; // We will get the appropriate value for the given field. We will look for the // value in the session for the value in the old input data then we'll look // in the model instance if one is set. Otherwise we will just use empty. - $id = $this->getIdAttribute($name, $attributes); + $id = $this->getIdAttribute($name, $options); $value = $this->getValueAttribute($name, $value); @@ -187,9 +194,9 @@ class FormBuilder { // Once we have the type, value, and ID we can marge them into the rest of the // attributes array so we can convert them into their HTML attribute format // when creating the HTML element. Then, we will return the entire input. - $attributes = array_merge($attributes, $merge); + $options = array_merge($options, $merge); - return ''; + return ''; } /** @@ -197,24 +204,24 @@ class FormBuilder { * * @param string $name * @param string $value - * @param array $attributes + * @param array $options * @return string */ - public function text($name, $value = null, $attributes = array()) + public function text($name, $value = null, $options = array()) { - return $this->input('text', $name, $value, $attributes); + return $this->input('text', $name, $value, $options); } /** * Create a password input field. * * @param string $name - * @param array $attributes + * @param array $options * @return string */ - public function password($name, $attributes = array()) + public function password($name, $options = array()) { - return $this->input('password', $name, '', $attributes); + return $this->input('password', $name, '', $options); } /** @@ -222,12 +229,12 @@ class FormBuilder { * * @param string $name * @param string $value - * @param array $attributes + * @param array $options * @return string */ - public function hidden($name, $value = null, $attributes = array()) + public function hidden($name, $value = null, $options = array()) { - return $this->input('hidden', $name, $value, $attributes); + return $this->input('hidden', $name, $value, $options); } /** @@ -235,24 +242,24 @@ class FormBuilder { * * @param string $name * @param string $value - * @param array $attributes + * @param array $options * @return string */ - public function email($name, $value = null, $attributes = array()) + public function email($name, $value = null, $options = array()) { - return $this->input('email', $name, $value, $attributes); + return $this->input('email', $name, $value, $options); } /** * Create a file input field. * * @param string $name - * @param array $attributes + * @param array $options * @return string */ - public function file($name, $attributes = array()) + public function file($name, $options = array()) { - return $this->input('file', $name, null, $attributes); + return $this->input('file', $name, null, $options); } /** @@ -260,69 +267,69 @@ class FormBuilder { * * @param string $name * @param string $value - * @param array $attributes + * @param array $options * @return string */ - public function textarea($name, $value = null, $attributes = array()) + public function textarea($name, $value = null, $options = array()) { - $attributes['name'] = $name; + $options['name'] = $name; - $attributes['id'] = $this->getIdAttribute($name, $attributes); + $options['id'] = $this->getIdAttribute($name, $options); // Next we will look for the rows and cols attributes, as each of these are put // on the textarea element definition. If they are not present, we will just // assume some sane default values for these attributes for the developer. - $attributes = $this->setTextAreaSize($attributes); + $options = $this->setTextAreaSize($options); $value = (string) $this->getValueAttribute($value); - return ''.e($value).''; + return ''.e($value).''; } /** * Set the text area size on the attributes. * - * @param array $attributes + * @param array $options * @return array */ - protected function setTextAreaSize($attributes) + protected function setTextAreaSize($options) { - if (isset($attributes['size'])) + if (isset($options['size'])) { - $segments = explode('x', $attributes['size']); + $segments = explode('x', $options['size']); - return array_merge($attributes, array('cols' => $segments[0], 'rows' => $segments[1])); + return array_merge($options, array('cols' => $segments[0], 'rows' => $segments[1])); } - return array_merge($attributes, array('cols' => 50, 'rows' => 10)); + return array_merge($options, array('cols' => 50, 'rows' => 10)); } /** * Create a select box field. * * @param string $name - * @param array $options + * @param array $list * @param string $selected - * @param array $attributes + * @param array $options * @return string */ - public function select($name, $options = array(), $selected = null, $attributes = array()) + public function select($name, $list = array(), $selected = null, $options = array()) { // When building a select box the "value" attribute is really the selected one // so we will use that when checking the model or session for a value which // should provide a convenient method of re-populating the forms on post. $selected = $this->getValueAttribute($name, $selected); - $attributes['id'] = $this->getIdAttribute($name, $attributes); + $options['id'] = $this->getIdAttribute($name, $options); - $attributes['name'] = $name; + $options['name'] = $name; // We will simply loop through the options and build an HTML value for each of // them until we have an array of HTML declarations. Then we will join them // all together into one single HTML element that can be put on the form. $html = array(); - foreach ($options as $value => $display) + foreach ($list as $value => $display) { $html[] = $this->getSelectOption($display, $value, $selected); } @@ -330,11 +337,11 @@ class FormBuilder { // Once we have all of this HTML, we can join this into a single element after // formatting the attributes into an HTML "attributes" string, then we will // build out a final select statement, which will contain all the values. - $attributes = Html::attributes($attributes); + $options = Html::attributes($options); $list = implode('', $html); - return "{$list}"; + return "{$list}"; } /** @@ -358,16 +365,16 @@ class FormBuilder { /** * Create an option group form element. * - * @param array $options + * @param array $list * @param string $label * @param string $selected * @return string */ - protected function optionGroup($options, $label, $selected) + protected function optionGroup($list, $label, $selected) { $html = array(); - foreach ($options as $value => $display) + foreach ($list as $value => $display) { $html[] = $this->option($display, $value, $selected); } @@ -387,9 +394,9 @@ class FormBuilder { { $selected = $this->getSelectedValue($value, $selected); - $attributes = array('value' => e($value), 'selected' => $selected); + $options = array('value' => e($value), 'selected' => $selected); - return ''.e($display).''; + return ''.e($display).''; } /** @@ -415,12 +422,12 @@ class FormBuilder { * @param string $name * @param mixed $value * @param bool $checked - * @param array $attributes + * @param array $options * @return string */ - public function checkbox($name, $value = 1, $checked = null, $attributes = array()) + public function checkbox($name, $value = 1, $checked = null, $options = array()) { - return $this->checkable('checkbox', $name, $value, $checked, $attributes); + return $this->checkable('checkbox', $name, $value, $checked, $options); } /** @@ -429,14 +436,14 @@ class FormBuilder { * @param string $name * @param mixed $value * @param bool $checked - * @param array $attributes + * @param array $options * @return string */ - public function radio($name, $value = null, $checked = null, $attributes = array()) + public function radio($name, $value = null, $checked = null, $options = array()) { if (is_null($value)) $value = $name; - return $this->checkable('radio', $name, $value, $checked, $attributes); + return $this->checkable('radio', $name, $value, $checked, $options); } /** @@ -446,40 +453,40 @@ class FormBuilder { * @param string $name * @param mixed $value * @param bool $checked - * @param array $attributes + * @param array $options * @return string */ protected function checkable($type, $name, $value, $checked, $attributs) { if (is_null($checked)) $checked = (bool) $this->getValueAttribute($name, null); - if ($checked) $attributes['checked'] = 'checked'; + if ($checked) $options['checked'] = 'checked'; - return $this->input($type, $name, $value, $attributes); + return $this->input($type, $name, $value, $options); } /** * Create a submit button element. * * @param string $value - * @param array $attributes + * @param array $options * @return string */ - public function submit($value = null, $attributes = array()) + public function submit($value = null, $options = array()) { - return $this->input('submit', null, $value, $attributes); + return $this->input('submit', null, $value, $options); } /** * Create a button element. * * @param string $value - * @param array $attributes + * @param array $options * @return string */ - public function button($value = null, $attributes = array()) + public function button($value = null, $options = array()) { - return ''.e($value).''; + return ''.e($value).''; } /** @@ -513,15 +520,19 @@ class FormBuilder { */ protected function getAction(array $options) { - if (isset($options['url'])) return $options['url']; - // We will also check for a "route" or "action" parameter on the array so that // developers can easily specify a route or controller action when creating // a form providing a convenient interface for creating the form actions. + if (isset($options['url'])) return $options['url']; + if (isset($options['route'])) { return $this->url->route($options['route']); } + + // If an action is available, we are attempting to open a form to a controller + // action route. So, we will use the URL generator to get the path to these + // actions and return them from the method. Otherwise, we'll use current. elseif (isset($options['action'])) { return $this->url->action($options['action']); diff --git a/HtmlBuilder.php b/HtmlBuilder.php index 305ef06..c7cf424 100644 --- a/HtmlBuilder.php +++ b/HtmlBuilder.php @@ -2,6 +2,97 @@ class Html { + /** + * Generate an ordered list of items. + * + * @param array $items + * @param array $attributes + * @return string + */ + public static function ol($list, $attributes = array()) + { + return static::listing('ol', $list, $attributes); + } + + /** + * Generate an un-ordered list of items. + * + * @param array $items + * @param array $attributes + * @return string + */ + public static function ul($list, $attributes = array()) + { + return static::listing('ul', $list, $attributes); + } + + /** + * Create a listing HTML element. + * + * @param string $type + * @param array $list + * @param array $attributes + * @return string + */ + protected static function listing($type, $list, $attributes) + { + $html = ''; + + if (count($list) == 0) return $html; + + // Essentially we will just spin through the list and build the list of the HTML + // elements from the array. We will also handled nested lists in case that is + // present in the array. Then we will build out the final listing elements. + foreach ($list as $key => $value) + { + $html .= static::listingElement($key, $type, $value); + } + + $attributes = static::attributes($attributes); + + return "<{$type}{$attributes}>{$html}"; + } + + /** + * Create the HTML for a listing element. + * + * @param mied $key + * @param string $type + * @param string $value + * @return string + */ + protected static function listingElement($key, $type, $value) + { + if (is_array($value)) + { + return static::nestedListing($key, $type, $value); + } + else + { + return '
  • '.e($value).'
  • '; + } + } + + /** + * Create the HTML for a nested listing attribute. + * + * @param mied $key + * @param string $type + * @param string $value + * @return string + */ + protected static function nestedListing($key, $type, $value) + { + if (is_int($key)) + { + return static::listing($type, $value); + } + else + { + return '
  • '.$key.static::listing($type, $value).'
  • '; + } + } + /** * Build an HTML attribute string from an array. * @@ -12,26 +103,29 @@ class Html { { $html = array(); + // For numeric keys we will assume that the key and the value are the same + // as this will convert HTML attributes such as "required" to a correct + // form like required="required" instead of using incorrect numerics. foreach ((array) $attributes as $key => $value) { - // For numeric keys we will assume that the key and the value are the same - // as this will convert HTML attributes such as "required" to a correct - // form like required="required" instead of using incorrect numerics. - if (is_numeric($key)) - { - $key = $value; - } - - // Before we add the attribute, we will make sure the value actually has a - // value so we don't append any null valued attributes onto the list of - // our attributes. We will just skip them over so we do not add them. - if ( ! is_null($value)) - { - $html[] = $key.'="'.e($value).'"'; - } + $html[] = static::attributeElement($key, $value); } return count($html) > 0 ? ' '.implode(' ', $html) : ''; } + /** + * Build a single attribute element. + * + * @param string $key + * @param string $value + * @return string + */ + protected static function attributeElement($key, $value) + { + if (is_numeric($key)) $key = $value; + + if ( ! is_null($value)) return $key.'="'.e($value).'"'; + } + } \ No newline at end of file diff --git a/HtmlServiceProvider.php b/HtmlServiceProvider.php new file mode 100644 index 0000000..7ce95be --- /dev/null +++ b/HtmlServiceProvider.php @@ -0,0 +1,37 @@ +app['form'] = $this->app->share(function($app) + { + return new FormBuilder($app['url'], $app['session']->getToken()); + }); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('form'); + } + +} \ No newline at end of file