diff --git a/src/FormBuilder.php b/src/FormBuilder.php index f400924..69f4808 100755 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -1,14 +1,16 @@ -url = $url; - $this->html = $html; - $this->csrfToken = $csrfToken; + $this->url = $url; + $this->html = $html; + $this->csrfToken = $csrfToken; } /** @@ -98,26 +100,25 @@ class FormBuilder { */ public function open(array $options = []) { - $method = 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 // actual method since forms don't support the reserved methods in HTML. $attributes['method'] = $this->getMethod($method); - $attributes['action'] = $this->getAction($options); + $attributes['action'] = $this->getAction($options); - $attributes['accept-charset'] = 'UTF-8'; + $attributes['accept-charset'] = 'UTF-8'; // If the method is PUT, PATCH or DELETE we will need to add a spoofer hidden // field that will instruct the Symfony request to pretend the method is a // different method than it actually is, for convenience from the forms. $append = $this->getAppendage($method); - if(isset($options['files']) && $options['files']) - { - $options['enctype'] = 'multipart/form-data'; - } + if (isset($options['files']) && $options['files']) { + $options['enctype'] = 'multipart/form-data'; + } // Finally we're ready to create the final form HTML field. We will attribute // format the array of attributes. We will also add on the appendage which @@ -133,7 +134,7 @@ class FormBuilder { // extra value for the hidden _method field if it's needed for the form. $attributes = $this->html->attributes($attributes); - return '' . $append; + return ''.$append; } /** @@ -146,9 +147,9 @@ class FormBuilder { */ public function model($model, array $options = []) { - $this->model = $model; + $this->model = $model; - return $this->open($options); + return $this->open($options); } /** @@ -160,7 +161,7 @@ class FormBuilder { */ public function setModel($model) { - $this->model = $model; + $this->model = $model; } /** @@ -170,11 +171,11 @@ class FormBuilder { */ public function close() { - $this->labels = []; + $this->labels = []; - $this->model = null; + $this->model = null; - return ''; + return ''; } /** @@ -184,9 +185,9 @@ class FormBuilder { */ public function token() { - $token = ! empty($this->csrfToken) ? $this->csrfToken : $this->session->getToken(); + $token = !empty($this->csrfToken) ? $this->csrfToken : $this->session->getToken(); - return $this->hidden('_token', $token); + return $this->hidden('_token', $token); } /** @@ -200,13 +201,13 @@ class FormBuilder { */ public function label($name, $value = null, $options = []) { - $this->labels[] = $name; + $this->labels[] = $name; - $options = $this->html->attributes($options); + $options = $this->html->attributes($options); - $value = e($this->formatLabel($name, $value)); + $value = e($this->formatLabel($name, $value)); - return ''; + return ''; } /** @@ -219,7 +220,7 @@ class FormBuilder { */ protected function formatLabel($name, $value) { - return $value ?: ucwords(str_replace('_', ' ', $name)); + return $value ?: ucwords(str_replace('_', ' ', $name)); } /** @@ -234,26 +235,27 @@ class FormBuilder { */ public function input($type, $name, $value = null, $options = []) { - if( ! isset($options['name'])) $options['name'] = $name; + if (!isset($options['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, $options); - if( ! in_array($type, $this->skipValueTypes)) - { - $value = $this->getValueAttribute($name, $value); - } + if (!in_array($type, $this->skipValueTypes)) { + $value = $this->getValueAttribute($name, $value); + } // Once we have the type, value, and ID we can merge 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. $merge = compact('type', 'value', 'id'); - $options = array_merge($options, $merge); + $options = array_merge($options, $merge); - return 'html->attributes($options) . '>'; + return 'html->attributes($options).'>'; } /** @@ -267,7 +269,7 @@ class FormBuilder { */ public function text($name, $value = null, $options = []) { - return $this->input('text', $name, $value, $options); + return $this->input('text', $name, $value, $options); } /** @@ -280,7 +282,7 @@ class FormBuilder { */ public function password($name, $options = []) { - return $this->input('password', $name, '', $options); + return $this->input('password', $name, '', $options); } /** @@ -294,7 +296,7 @@ class FormBuilder { */ public function hidden($name, $value = null, $options = []) { - return $this->input('hidden', $name, $value, $options); + return $this->input('hidden', $name, $value, $options); } /** @@ -308,7 +310,7 @@ class FormBuilder { */ public function email($name, $value = null, $options = []) { - return $this->input('email', $name, $value, $options); + return $this->input('email', $name, $value, $options); } /** @@ -322,7 +324,7 @@ class FormBuilder { */ public function tel($name, $value = null, $options = []) { - return $this->input('tel', $name, $value, $options); + return $this->input('tel', $name, $value, $options); } /** @@ -336,7 +338,7 @@ class FormBuilder { */ public function number($name, $value = null, $options = []) { - return $this->input('number', $name, $value, $options); + return $this->input('number', $name, $value, $options); } /** @@ -350,12 +352,11 @@ class FormBuilder { */ public function date($name, $value = null, $options = []) { - if($value instanceof DateTime) - { - $value = $value->format('Y-m-d'); - } + if ($value instanceof DateTime) { + $value = $value->format('Y-m-d'); + } - return $this->input('date', $name, $value, $options); + return $this->input('date', $name, $value, $options); } /** @@ -369,7 +370,7 @@ class FormBuilder { */ public function time($name, $value = null, $options = []) { - return $this->input('time', $name, $value, $options); + return $this->input('time', $name, $value, $options); } /** @@ -383,7 +384,7 @@ class FormBuilder { */ public function url($name, $value = null, $options = []) { - return $this->input('url', $name, $value, $options); + return $this->input('url', $name, $value, $options); } /** @@ -396,7 +397,7 @@ class FormBuilder { */ public function file($name, $options = []) { - return $this->input('file', $name, null, $options); + return $this->input('file', $name, null, $options); } /** @@ -410,25 +411,27 @@ class FormBuilder { */ public function textarea($name, $value = null, $options = []) { - if( ! isset($options['name'])) $options['name'] = $name; + if (!isset($options['name'])) { + $options['name'] = $name; + } // 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. $options = $this->setTextAreaSize($options); - $options['id'] = $this->getIdAttribute($name, $options); + $options['id'] = $this->getIdAttribute($name, $options); - $value = (string) $this->getValueAttribute($name, $value); + $value = (string) $this->getValueAttribute($name, $value); - unset($options['size']); + unset($options['size']); // Next we will convert the attributes into a string form. Also we have removed // the size attribute, as it was merely a short-cut for the rows and cols on // the element. Then we'll create the final textarea elements HTML for us. $options = $this->html->attributes($options); - return '' . e($value) . ''; + return ''.e($value).''; } /** @@ -440,19 +443,18 @@ class FormBuilder { */ protected function setTextAreaSize($options) { - if(isset($options['size'])) - { - return $this->setQuickTextAreaSize($options); - } + if (isset($options['size'])) { + return $this->setQuickTextAreaSize($options); + } // If the "size" attribute was not specified, we will just look for the regular // columns and rows attributes, using sane defaults if these do not exist on // the attributes array. We'll then return this entire options array back. $cols = array_get($options, 'cols', 50); - $rows = array_get($options, 'rows', 10); + $rows = array_get($options, 'rows', 10); - return array_merge($options, compact('cols', 'rows')); + return array_merge($options, compact('cols', 'rows')); } /** @@ -464,9 +466,9 @@ class FormBuilder { */ protected function setQuickTextAreaSize($options) { - $segments = explode('x', $options['size']); + $segments = explode('x', $options['size']); - return array_merge($options, ['cols' => $segments[0], 'rows' => $segments[1]]); + return array_merge($options, ['cols' => $segments[0], 'rows' => $segments[1]]); } /** @@ -481,39 +483,39 @@ class FormBuilder { */ public function select($name, $list = [], $selected = null, $options = []) { - // When building a select box the "value" attribute is really the selected one + // 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); - $options['id'] = $this->getIdAttribute($name, $options); + $options['id'] = $this->getIdAttribute($name, $options); - if( ! isset($options['name'])) $options['name'] = $name; + if (!isset($options['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 = []; - if(isset($options['placeholder'])) - { - $html[] = $this->placeholderOption($options['placeholder'], $selected); - unset($options['placeholder']); - } + if (isset($options['placeholder'])) { + $html[] = $this->placeholderOption($options['placeholder'], $selected); + unset($options['placeholder']); + } - foreach($list as $value => $display) - { - $html[] = $this->getSelectOption($display, $value, $selected); - } + foreach ($list as $value => $display) { + $html[] = $this->getSelectOption($display, $value, $selected); + } // 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. $options = $this->html->attributes($options); - $list = implode('', $html); + $list = implode('', $html); - return "{$list}"; + return "{$list}"; } /** @@ -529,9 +531,9 @@ class FormBuilder { */ public function selectRange($name, $begin, $end, $selected = null, $options = []) { - $range = array_combine($range = range($begin, $end), $range); + $range = array_combine($range = range($begin, $end), $range); - return $this->select($name, $range, $selected, $options); + return $this->select($name, $range, $selected, $options); } /** @@ -547,7 +549,7 @@ class FormBuilder { */ public function selectYear() { - return call_user_func_array([$this, 'selectRange'], func_get_args()); + return call_user_func_array([$this, 'selectRange'], func_get_args()); } /** @@ -562,14 +564,13 @@ class FormBuilder { */ public function selectMonth($name, $selected = null, $options = [], $format = '%B') { - $months = []; + $months = []; - foreach(range(1, 12) as $month) - { - $months[$month] = strftime($format, mktime(0, 0, 0, $month, 1)); - } + foreach (range(1, 12) as $month) { + $months[$month] = strftime($format, mktime(0, 0, 0, $month, 1)); + } - return $this->select($name, $months, $selected, $options); + return $this->select($name, $months, $selected, $options); } /** @@ -583,12 +584,11 @@ class FormBuilder { */ public function getSelectOption($display, $value, $selected) { - if(is_array($display)) - { - return $this->optionGroup($display, $value, $selected); - } + if (is_array($display)) { + return $this->optionGroup($display, $value, $selected); + } - return $this->option($display, $value, $selected); + return $this->option($display, $value, $selected); } /** @@ -602,14 +602,13 @@ class FormBuilder { */ protected function optionGroup($list, $label, $selected) { - $html = []; + $html = []; - foreach($list as $value => $display) - { - $html[] = $this->option($display, $value, $selected); - } + foreach ($list as $value => $display) { + $html[] = $this->option($display, $value, $selected); + } - return '' . implode('', $html) . ''; + return ''.implode('', $html).''; } /** @@ -623,11 +622,11 @@ class FormBuilder { */ protected function option($display, $value, $selected) { - $selected = $this->getSelectedValue($value, $selected); + $selected = $this->getSelectedValue($value, $selected); - $options = ['value' => $value, 'selected' => $selected]; + $options = ['value' => $value, 'selected' => $selected]; - return 'html->attributes($options) . '>' . e($display) . ''; + return 'html->attributes($options).'>'.e($display).''; } /** @@ -640,12 +639,12 @@ class FormBuilder { */ protected function placeholderOption($display, $selected) { - $selected = $this->getSelectedValue(null, $selected); + $selected = $this->getSelectedValue(null, $selected); - $options = compact('selected'); - $options['value'] = ''; + $options = compact('selected'); + $options['value'] = ''; - return 'html->attributes($options) . '>' . e($display) . ''; + return 'html->attributes($options).'>'.e($display).''; } /** @@ -658,12 +657,11 @@ class FormBuilder { */ protected function getSelectedValue($value, $selected) { - if(is_array($selected)) - { - return in_array($value, $selected) ? 'selected' : null; - } + if (is_array($selected)) { + return in_array($value, $selected) ? 'selected' : null; + } - return ((string) $value == (string) $selected) ? 'selected' : null; + return ((string) $value == (string) $selected) ? 'selected' : null; } /** @@ -678,7 +676,7 @@ class FormBuilder { */ public function checkbox($name, $value = 1, $checked = null, $options = []) { - return $this->checkable('checkbox', $name, $value, $checked, $options); + return $this->checkable('checkbox', $name, $value, $checked, $options); } /** @@ -693,9 +691,11 @@ class FormBuilder { */ public function radio($name, $value = null, $checked = null, $options = []) { - if(is_null($value)) $value = $name; + if (is_null($value)) { + $value = $name; + } - return $this->checkable('radio', $name, $value, $checked, $options); + return $this->checkable('radio', $name, $value, $checked, $options); } /** @@ -711,11 +711,13 @@ class FormBuilder { */ protected function checkable($type, $name, $value, $checked, $options) { - $checked = $this->getCheckedState($type, $name, $value, $checked); + $checked = $this->getCheckedState($type, $name, $value, $checked); - if($checked) $options['checked'] = 'checked'; + if ($checked) { + $options['checked'] = 'checked'; + } - return $this->input($type, $name, $value, $options); + return $this->input($type, $name, $value, $options); } /** @@ -730,8 +732,7 @@ class FormBuilder { */ protected function getCheckedState($type, $name, $value, $checked) { - switch($type) - { + switch ($type) { case 'checkbox': return $this->getCheckboxCheckedState($name, $value, $checked); @@ -754,24 +755,23 @@ class FormBuilder { */ protected function getCheckboxCheckedState($name, $value, $checked) { - if(isset($this->session) && ! $this->oldInputIsEmpty() && is_null($this->old($name))) return false; + if (isset($this->session) && !$this->oldInputIsEmpty() && is_null($this->old($name))) { + return false; + } - if($this->missingOldAndModel($name)) return $checked; + if ($this->missingOldAndModel($name)) { + return $checked; + } - $posted = $this->getValueAttribute($name, $checked); + $posted = $this->getValueAttribute($name, $checked); - if (is_array($posted)) - { - return in_array($value, $posted); - } - else if ($posted instanceof Collection) - { - return $posted->contains('id', $value); - } - else - { - return (bool) $posted; - } + if (is_array($posted)) { + return in_array($value, $posted); + } elseif ($posted instanceof Collection) { + return $posted->contains('id', $value); + } else { + return (bool) $posted; + } } /** @@ -785,9 +785,11 @@ class FormBuilder { */ protected function getRadioCheckedState($name, $value, $checked) { - if($this->missingOldAndModel($name)) return $checked; + if ($this->missingOldAndModel($name)) { + return $checked; + } - return $this->getValueAttribute($name) == $value; + return $this->getValueAttribute($name) == $value; } /** @@ -799,7 +801,7 @@ class FormBuilder { */ protected function missingOldAndModel($name) { - return (is_null($this->old($name)) && is_null($this->getModelValueAttribute($name))); + return (is_null($this->old($name)) && is_null($this->getModelValueAttribute($name))); } /** @@ -812,7 +814,7 @@ class FormBuilder { */ public function reset($value, $attributes = []) { - return $this->input('reset', null, $value, $attributes); + return $this->input('reset', null, $value, $attributes); } /** @@ -826,9 +828,9 @@ class FormBuilder { */ public function image($url, $name = null, $attributes = []) { - $attributes['src'] = $this->url->asset($url); + $attributes['src'] = $this->url->asset($url); - return $this->input('image', $name, null, $attributes); + return $this->input('image', $name, null, $attributes); } /** @@ -841,7 +843,7 @@ class FormBuilder { */ public function submit($value = null, $options = []) { - return $this->input('submit', null, $value, $options); + return $this->input('submit', null, $value, $options); } /** @@ -854,12 +856,11 @@ class FormBuilder { */ public function button($value = null, $options = []) { - if( ! array_key_exists('type', $options)) - { - $options['type'] = 'button'; - } + if (!array_key_exists('type', $options)) { + $options['type'] = 'button'; + } - return 'html->attributes($options) . '>' . $value . ''; + return 'html->attributes($options).'>'.$value.''; } /** @@ -871,9 +872,9 @@ class FormBuilder { */ protected function getMethod($method) { - $method = strtoupper($method); + $method = strtoupper($method); - return $method != 'GET' ? 'POST' : $method; + return $method != 'GET' ? 'POST' : $method; } /** @@ -885,28 +886,25 @@ class FormBuilder { */ protected function getAction(array $options) { - // We will also check for a "route" or "action" parameter on the array so that + // 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 $this->getUrlAction($options['url']); + if (isset($options['url'])) { + return $this->getUrlAction($options['url']); } - if(isset($options['route'])) - { - return $this->getRouteAction($options['route']); - } + if (isset($options['route'])) { + return $this->getRouteAction($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->getControllerAction($options['action']); + elseif (isset($options['action'])) { + return $this->getControllerAction($options['action']); } - return $this->url->current(); + return $this->url->current(); } /** @@ -918,12 +916,11 @@ class FormBuilder { */ protected function getUrlAction($options) { - if(is_array($options)) - { - return $this->url->to($options[0], array_slice($options, 1)); - } + if (is_array($options)) { + return $this->url->to($options[0], array_slice($options, 1)); + } - return $this->url->to($options); + return $this->url->to($options); } /** @@ -935,12 +932,11 @@ class FormBuilder { */ protected function getRouteAction($options) { - if(is_array($options)) - { - return $this->url->route($options[0], array_slice($options, 1)); - } + if (is_array($options)) { + return $this->url->route($options[0], array_slice($options, 1)); + } - return $this->url->route($options); + return $this->url->route($options); } /** @@ -952,12 +948,11 @@ class FormBuilder { */ protected function getControllerAction($options) { - if(is_array($options)) - { - return $this->url->action($options[0], array_slice($options, 1)); - } + if (is_array($options)) { + return $this->url->action($options[0], array_slice($options, 1)); + } - return $this->url->action($options); + return $this->url->action($options); } /** @@ -969,25 +964,23 @@ class FormBuilder { */ protected function getAppendage($method) { - list($method, $appendage) = [strtoupper($method), '']; + list($method, $appendage) = [strtoupper($method), '']; // If the HTTP method is in this list of spoofed methods, we will attach the // method spoofer hidden input to the form. This allows us to use regular // form to initiate PUT and DELETE requests in addition to the typical. - if(in_array($method, $this->spoofedMethods)) - { - $appendage .= $this->hidden('_method', $method); + if (in_array($method, $this->spoofedMethods)) { + $appendage .= $this->hidden('_method', $method); } // If the method is something other than GET we will go ahead and attach the // CSRF token to the form, as this can't hurt and is convenient to simply // always have available on every form the developers creates for them. - if($method != 'GET') - { - $appendage .= $this->token(); + if ($method != 'GET') { + $appendage .= $this->token(); } - return $appendage; + return $appendage; } /** @@ -1000,15 +993,13 @@ class FormBuilder { */ public function getIdAttribute($name, $attributes) { - if(array_key_exists('id', $attributes)) - { - return $attributes['id']; - } + if (array_key_exists('id', $attributes)) { + return $attributes['id']; + } - if(in_array($name, $this->labels)) - { - return $name; - } + if (in_array($name, $this->labels)) { + return $name; + } } /** @@ -1021,19 +1012,21 @@ class FormBuilder { */ public function getValueAttribute($name, $value = null) { - if(is_null($name)) return $value; + if (is_null($name)) { + return $value; + } - if( ! is_null($this->old($name))) - { - return $this->old($name); - } + if (!is_null($this->old($name))) { + return $this->old($name); + } - if( ! is_null($value)) return $value; + if (!is_null($value)) { + return $value; + } - if(isset($this->model)) - { - return $this->getModelValueAttribute($name); - } + if (isset($this->model)) { + return $this->getModelValueAttribute($name); + } } /** @@ -1045,7 +1038,7 @@ class FormBuilder { */ protected function getModelValueAttribute($name) { - return data_get($this->model, $this->transformKey($name)); + return data_get($this->model, $this->transformKey($name)); } /** @@ -1057,10 +1050,9 @@ class FormBuilder { */ public function old($name) { - if(isset($this->session)) - { - return $this->session->getOldInput($this->transformKey($name)); - } + if (isset($this->session)) { + return $this->session->getOldInput($this->transformKey($name)); + } } /** @@ -1070,7 +1062,7 @@ class FormBuilder { */ public function oldInputIsEmpty() { - return (isset($this->session) && count($this->session->getOldInput()) == 0); + return (isset($this->session) && count($this->session->getOldInput()) == 0); } /** @@ -1082,7 +1074,7 @@ class FormBuilder { */ protected function transformKey($key) { - return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $key); + return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $key); } /** @@ -1092,7 +1084,7 @@ class FormBuilder { */ public function getSessionStore() { - return $this->session; + return $this->session; } /** @@ -1104,8 +1096,8 @@ class FormBuilder { */ public function setSessionStore(Session $session) { - $this->session = $session; + $this->session = $session; - return $this; + return $this; } } diff --git a/src/FormFacade.php b/src/FormFacade.php index 064f953..66d459f 100644 --- a/src/FormFacade.php +++ b/src/FormFacade.php @@ -1,17 +1,21 @@ -url = $url; + } - /** - * Create a new HTML builder instance. - * - * @param \Illuminate\Routing\UrlGenerator $url - * @return void - */ - public function __construct(UrlGenerator $url = null) - { - $this->url = $url; - } + /** + * Convert an HTML string to entities. + * + * @param string $value + * + * @return string + */ + public function entities($value) + { + return htmlentities($value, ENT_QUOTES, 'UTF-8', false); + } - /** - * Convert an HTML string to entities. - * - * @param string $value - * @return string - */ - public function entities($value) - { - return htmlentities($value, ENT_QUOTES, 'UTF-8', false); - } + /** + * Convert entities to HTML characters. + * + * @param string $value + * + * @return string + */ + public function decode($value) + { + return html_entity_decode($value, ENT_QUOTES, 'UTF-8'); + } - /** - * Convert entities to HTML characters. - * - * @param string $value - * @return string - */ - public function decode($value) - { - return html_entity_decode($value, ENT_QUOTES, 'UTF-8'); - } + /** + * Generate a link to a JavaScript file. + * + * @param string $url + * @param array $attributes + * @param bool $secure + * + * @return string + */ + public function script($url, $attributes = [], $secure = null) + { + $attributes['src'] = $this->url->asset($url, $secure); - /** - * Generate a link to a JavaScript file. - * - * @param string $url - * @param array $attributes - * @param bool $secure - * @return string - */ - public function script($url, $attributes = array(), $secure = null) - { - $attributes['src'] = $this->url->asset($url, $secure); + return 'attributes($attributes).'>'.PHP_EOL; + } - return 'attributes($attributes).'>'.PHP_EOL; - } + /** + * Generate a link to a CSS file. + * + * @param string $url + * @param array $attributes + * @param bool $secure + * + * @return string + */ + public function style($url, $attributes = [], $secure = null) + { + $defaults = ['media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet']; - /** - * Generate a link to a CSS file. - * - * @param string $url - * @param array $attributes - * @param bool $secure - * @return string - */ - public function style($url, $attributes = array(), $secure = null) - { - $defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet'); + $attributes = $attributes + $defaults; - $attributes = $attributes + $defaults; + $attributes['href'] = $this->url->asset($url, $secure); - $attributes['href'] = $this->url->asset($url, $secure); + return 'attributes($attributes).'>'.PHP_EOL; + } - return 'attributes($attributes).'>'.PHP_EOL; - } + /** + * Generate an HTML image element. + * + * @param string $url + * @param string $alt + * @param array $attributes + * @param bool $secure + * + * @return string + */ + public function image($url, $alt = null, $attributes = [], $secure = null) + { + $attributes['alt'] = $alt; - /** - * Generate an HTML image element. - * - * @param string $url - * @param string $alt - * @param array $attributes - * @param bool $secure - * @return string - */ - public function image($url, $alt = null, $attributes = array(), $secure = null) - { - $attributes['alt'] = $alt; + return 'attributes($attributes).'>'; + } - return 'attributes($attributes).'>'; - } + /** + * Generate a link to a Favicon file. + * + * @param string $url + * @param array $attributes + * @param bool $secure + * + * @return string + */ + public function favicon($url, $attributes = [], $secure = null) + { + $defaults = ['rel' => 'shortcut icon', 'type' => 'image/x-icon']; - /** - * Generate a link to a Favicon file. - * - * @param string $url - * @param array $attributes - * @param bool $secure - * @return string - */ - public function favicon($url, $attributes = array(), $secure = null) - { - $defaults = array('rel' => 'shortcut icon', 'type' => 'image/x-icon'); + $attributes = $attributes + $defaults; - $attributes = $attributes + $defaults; + $attributes['href'] = $this->url->asset($url, $secure); - $attributes['href'] = $this->url->asset($url, $secure); + return 'attributes($attributes).'>'.PHP_EOL; + } - return 'attributes($attributes).'>'.PHP_EOL; - } + /** + * Generate a HTML link. + * + * @param string $url + * @param string $title + * @param array $attributes + * @param bool $secure + * + * @return string + */ + public function link($url, $title = null, $attributes = [], $secure = null) + { + $url = $this->url->to($url, [], $secure); - /** - * Generate a HTML link. - * - * @param string $url - * @param string $title - * @param array $attributes - * @param bool $secure - * @return string - */ - public function link($url, $title = null, $attributes = array(), $secure = null) - { - $url = $this->url->to($url, array(), $secure); + if (is_null($title) || $title === false) { + $title = $url; + } - if (is_null($title) || $title === false) $title = $url; + return 'attributes($attributes).'>'.$this->entities($title).''; + } - return 'attributes($attributes).'>'.$this->entities($title).''; - } + /** + * Generate a HTTPS HTML link. + * + * @param string $url + * @param string $title + * @param array $attributes + * + * @return string + */ + public function secureLink($url, $title = null, $attributes = []) + { + return $this->link($url, $title, $attributes, true); + } - /** - * Generate a HTTPS HTML link. - * - * @param string $url - * @param string $title - * @param array $attributes - * @return string - */ - public function secureLink($url, $title = null, $attributes = array()) - { - return $this->link($url, $title, $attributes, true); - } + /** + * Generate a HTML link to an asset. + * + * @param string $url + * @param string $title + * @param array $attributes + * @param bool $secure + * + * @return string + */ + public function linkAsset($url, $title = null, $attributes = [], $secure = null) + { + $url = $this->url->asset($url, $secure); - /** - * Generate a HTML link to an asset. - * - * @param string $url - * @param string $title - * @param array $attributes - * @param bool $secure - * @return string - */ - public function linkAsset($url, $title = null, $attributes = array(), $secure = null) - { - $url = $this->url->asset($url, $secure); + return $this->link($url, $title ?: $url, $attributes, $secure); + } - return $this->link($url, $title ?: $url, $attributes, $secure); - } + /** + * Generate a HTTPS HTML link to an asset. + * + * @param string $url + * @param string $title + * @param array $attributes + * + * @return string + */ + public function linkSecureAsset($url, $title = null, $attributes = []) + { + return $this->linkAsset($url, $title, $attributes, true); + } - /** - * Generate a HTTPS HTML link to an asset. - * - * @param string $url - * @param string $title - * @param array $attributes - * @return string - */ - public function linkSecureAsset($url, $title = null, $attributes = array()) - { - return $this->linkAsset($url, $title, $attributes, true); - } + /** + * Generate a HTML link to a named route. + * + * @param string $name + * @param string $title + * @param array $parameters + * @param array $attributes + * + * @return string + */ + public function linkRoute($name, $title = null, $parameters = [], $attributes = []) + { + return $this->link($this->url->route($name, $parameters), $title, $attributes); + } - /** - * Generate a HTML link to a named route. - * - * @param string $name - * @param string $title - * @param array $parameters - * @param array $attributes - * @return string - */ - public function linkRoute($name, $title = null, $parameters = array(), $attributes = array()) - { - return $this->link($this->url->route($name, $parameters), $title, $attributes); - } + /** + * Generate a HTML link to a controller action. + * + * @param string $action + * @param string $title + * @param array $parameters + * @param array $attributes + * + * @return string + */ + public function linkAction($action, $title = null, $parameters = [], $attributes = []) + { + return $this->link($this->url->action($action, $parameters), $title, $attributes); + } - /** - * Generate a HTML link to a controller action. - * - * @param string $action - * @param string $title - * @param array $parameters - * @param array $attributes - * @return string - */ - public function linkAction($action, $title = null, $parameters = array(), $attributes = array()) - { - return $this->link($this->url->action($action, $parameters), $title, $attributes); - } + /** + * Generate a HTML link to an email address. + * + * @param string $email + * @param string $title + * @param array $attributes + * + * @return string + */ + public function mailto($email, $title = null, $attributes = []) + { + $email = $this->email($email); - /** - * Generate a HTML link to an email address. - * - * @param string $email - * @param string $title - * @param array $attributes - * @return string - */ - public function mailto($email, $title = null, $attributes = array()) - { - $email = $this->email($email); + $title = $title ?: $email; - $title = $title ?: $email; + $email = $this->obfuscate('mailto:').$email; - $email = $this->obfuscate('mailto:') . $email; + return 'attributes($attributes).'>'.$this->entities($title).''; + } - return 'attributes($attributes).'>'.$this->entities($title).''; - } + /** + * Obfuscate an e-mail address to prevent spam-bots from sniffing it. + * + * @param string $email + * + * @return string + */ + public function email($email) + { + return str_replace('@', '@', $this->obfuscate($email)); + } - /** - * Obfuscate an e-mail address to prevent spam-bots from sniffing it. - * - * @param string $email - * @return string - */ - public function email($email) - { - return str_replace('@', '@', $this->obfuscate($email)); - } + /** + * Generate an ordered list of items. + * + * @param array $list + * @param array $attributes + * + * @return string + */ + public function ol($list, $attributes = []) + { + return $this->listing('ol', $list, $attributes); + } - /** - * Generate an ordered list of items. - * - * @param array $list - * @param array $attributes - * @return string - */ - public function ol($list, $attributes = array()) - { - return $this->listing('ol', $list, $attributes); - } + /** + * Generate an un-ordered list of items. + * + * @param array $list + * @param array $attributes + * + * @return string + */ + public function ul($list, $attributes = []) + { + return $this->listing('ul', $list, $attributes); + } - /** - * Generate an un-ordered list of items. - * - * @param array $list - * @param array $attributes - * @return string - */ - public function ul($list, $attributes = array()) - { - return $this->listing('ul', $list, $attributes); - } + /** + * Generate a description list of items. + * + * @param array $list + * @param array $attributes + * + * @return string + */ + public function dl(array $list, array $attributes = []) + { + $attributes = $this->attributes($attributes); - /** - * Generate a description list of items. - * - * @param array $list - * @param array $attributes - * @return string - */ - public function dl(array $list, array $attributes = []) - { - $attributes = $this->attributes($attributes); + $html = ""; - $html = ""; + foreach ($list as $key => $value) { + $html .= "
$key
$value
"; + } - foreach ($list as $key => $value) - { - $html .= "
$key
$value
"; - } + $html .= ''; - $html .= ''; + return $html; + } - return $html; - } + /** + * Create a listing HTML element. + * + * @param string $type + * @param array $list + * @param array $attributes + * + * @return string + */ + protected function listing($type, $list, $attributes = []) + { + $html = ''; - /** - * Create a listing HTML element. - * - * @param string $type - * @param array $list - * @param array $attributes - * @return string - */ - protected function listing($type, $list, $attributes = array()) - { - $html = ''; + if (count($list) == 0) { + return $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 .= $this->listingElement($key, $type, $value); + } - // 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 .= $this->listingElement($key, $type, $value); - } + $attributes = $this->attributes($attributes); - $attributes = $this->attributes($attributes); + return "<{$type}{$attributes}>{$html}"; + } - return "<{$type}{$attributes}>{$html}"; - } + /** + * Create the HTML for a listing element. + * + * @param mixed $key + * @param string $type + * @param mixed $value + * + * @return string + */ + protected function listingElement($key, $type, $value) + { + if (is_array($value)) { + return $this->nestedListing($key, $type, $value); + } else { + return '
  • '.e($value).'
  • '; + } + } - /** - * Create the HTML for a listing element. - * - * @param mixed $key - * @param string $type - * @param mixed $value - * @return string - */ - protected function listingElement($key, $type, $value) - { - if (is_array($value)) - { - return $this->nestedListing($key, $type, $value); - } - else - { - return '
  • '.e($value).'
  • '; - } - } + /** + * Create the HTML for a nested listing attribute. + * + * @param mixed $key + * @param string $type + * @param mixed $value + * + * @return string + */ + protected function nestedListing($key, $type, $value) + { + if (is_int($key)) { + return $this->listing($type, $value); + } else { + return '
  • '.$key.$this->listing($type, $value).'
  • '; + } + } - /** - * Create the HTML for a nested listing attribute. - * - * @param mixed $key - * @param string $type - * @param mixed $value - * @return string - */ - protected function nestedListing($key, $type, $value) - { - if (is_int($key)) - { - return $this->listing($type, $value); - } - else - { - return '
  • '.$key.$this->listing($type, $value).'
  • '; - } - } + /** + * Build an HTML attribute string from an array. + * + * @param array $attributes + * + * @return string + */ + public function attributes($attributes) + { + $html = []; - /** - * Build an HTML attribute string from an array. - * - * @param array $attributes - * @return string - */ - public function attributes($attributes) - { - $html = array(); + foreach ((array) $attributes as $key => $value) { + $element = $this->attributeElement($key, $value); - foreach ((array) $attributes as $key => $value) - { - $element = $this->attributeElement($key, $value); + if (!is_null($element)) { + $html[] = $element; + } + } - if ( ! is_null($element)) $html[] = $element; - } + return count($html) > 0 ? ' '.implode(' ', $html) : ''; + } - return count($html) > 0 ? ' '.implode(' ', $html) : ''; - } + /** + * Build a single attribute element. + * + * @param string $key + * @param string $value + * + * @return string + */ + protected function attributeElement($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; + } - /** - * Build a single attribute element. - * - * @param string $key - * @param string $value - * @return string - */ - protected function attributeElement($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; + if (!is_null($value)) { + return $key.'="'.e($value).'"'; + } + } - if ( ! is_null($value)) return $key.'="'.e($value).'"'; - } + /** + * Obfuscate a string to prevent spam-bots from sniffing it. + * + * @param string $value + * + * @return string + */ + public function obfuscate($value) + { + $safe = ''; - /** - * Obfuscate a string to prevent spam-bots from sniffing it. - * - * @param string $value - * @return string - */ - public function obfuscate($value) - { - $safe = ''; + foreach (str_split($value) as $letter) { + if (ord($letter) > 128) { + return $letter; + } - foreach (str_split($value) as $letter) - { - if (ord($letter) > 128) return $letter; + // To properly obfuscate the value, we will randomly convert each letter to + // its entity or hexadecimal representation, keeping a bot from sniffing + // the randomly obfuscated letters out of the string on the responses. + switch (rand(1, 3)) { + case 1: + $safe .= '&#'.ord($letter).';'; break; - // To properly obfuscate the value, we will randomly convert each letter to - // its entity or hexadecimal representation, keeping a bot from sniffing - // the randomly obfuscated letters out of the string on the responses. - switch (rand(1, 3)) - { - case 1: - $safe .= '&#'.ord($letter).';'; break; + case 2: + $safe .= '&#x'.dechex(ord($letter)).';'; break; - case 2: - $safe .= '&#x'.dechex(ord($letter)).';'; break; + case 3: + $safe .= $letter; + } + } - case 3: - $safe .= $letter; - } - } + return $safe; + } - return $safe; - } + /** + * Generate a meta tag. + * + * @param string $name + * @param string $content + * @param array $attributes + * + * @return string + */ + public function meta($name, $content, array $attributes = []) + { + $defaults = compact('name', 'content'); - /** - * Generate a meta tag. - * - * @param string $name - * @param string $content - * @param array $attributes - * @return string - */ - public function meta($name, $content, array $attributes = []) - { - $defaults = compact('name', 'content'); - - $attributes = array_merge($defaults, $attributes); - - return 'attributes($attributes).'>'.PHP_EOL; - } + $attributes = array_merge($defaults, $attributes); + return 'attributes($attributes).'>'.PHP_EOL; + } } diff --git a/src/HtmlFacade.php b/src/HtmlFacade.php index e7e593a..1202359 100644 --- a/src/HtmlFacade.php +++ b/src/HtmlFacade.php @@ -1,17 +1,21 @@ -registerHtmlBuilder(); - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $this->registerHtmlBuilder(); + $this->registerFormBuilder(); - $this->registerFormBuilder(); + $this->app->alias('html', 'Collective\Html\HtmlBuilder'); + $this->app->alias('form', 'Collective\Html\FormBuilder'); + } - $this->app->alias('html', 'Collective\Html\HtmlBuilder'); - $this->app->alias('form', 'Collective\Html\FormBuilder'); - } + /** + * Register the HTML builder instance. + * + * @return void + */ + protected function registerHtmlBuilder() + { + $this->app->bindShared('html', function ($app) { + return new HtmlBuilder($app['url']); + }); + } - /** - * Register the HTML builder instance. - * - * @return void - */ - protected function registerHtmlBuilder() - { - $this->app->bindShared('html', function($app) - { - return new HtmlBuilder($app['url']); - }); - } + /** + * Register the form builder instance. + * + * @return void + */ + protected function registerFormBuilder() + { + $this->app->bindShared('form', function ($app) { + $form = new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken()); - /** - * Register the form builder instance. - * - * @return void - */ - protected function registerFormBuilder() - { - $this->app->bindShared('form', function($app) - { - $form = new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken()); - - return $form->setSessionStore($app['session.store']); - }); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array('html', 'form', 'Collective\Html\HtmlBuilder', 'Collective\Html\FormBuilder'); - } + return $form->setSessionStore($app['session.store']); + }); + } + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return ['html', 'form', 'Collective\Html\HtmlBuilder', 'Collective\Html\FormBuilder']; + } } diff --git a/src/helpers.php b/src/helpers.php index fa5bef6..b84673b 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,68 +1,68 @@ link($url, $title, $attributes, $secure); } } -if ( ! function_exists('link_to_asset')) -{ +if (!function_exists('link_to_asset')) { /** * Generate a HTML link to an asset. * - * @param string $url - * @param string $title - * @param array $attributes - * @param bool $secure + * @param string $url + * @param string $title + * @param array $attributes + * @param bool $secure + * * @return string */ - function link_to_asset($url, $title = null, $attributes = array(), $secure = null) + function link_to_asset($url, $title = null, $attributes = [], $secure = null) { return app('html')->linkAsset($url, $title, $attributes, $secure); } } -if ( ! function_exists('link_to_route')) -{ +if (!function_exists('link_to_route')) { /** * Generate a HTML link to a named route. * - * @param string $name - * @param string $title - * @param array $parameters - * @param array $attributes + * @param string $name + * @param string $title + * @param array $parameters + * @param array $attributes + * * @return string */ - function link_to_route($name, $title = null, $parameters = array(), $attributes = array()) + function link_to_route($name, $title = null, $parameters = [], $attributes = []) { return app('html')->linkRoute($name, $title, $parameters, $attributes); } } -if ( ! function_exists('link_to_action')) -{ +if (!function_exists('link_to_action')) { /** * Generate a HTML link to a controller action. * - * @param string $action - * @param string $title - * @param array $parameters - * @param array $attributes + * @param string $action + * @param string $title + * @param array $parameters + * @param array $attributes + * * @return string */ - function link_to_action($action, $title = null, $parameters = array(), $attributes = array()) + function link_to_action($action, $title = null, $parameters = [], $attributes = []) { return app('html')->linkAction($action, $title, $parameters, $attributes); } diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 3eb61ea..f835e01 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -1,543 +1,505 @@ urlGenerator = new UrlGenerator(new RouteCollection, Request::create('/foo', 'GET')); - $this->htmlBuilder = new HtmlBuilder($this->urlGenerator); - $this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, 'abc'); + $this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET')); + $this->htmlBuilder = new HtmlBuilder($this->urlGenerator); + $this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, 'abc'); } - /** * Destroy the test environment. */ public function tearDown() { - m::close(); + m::close(); } + public function testOpeningForm() + { + $form1 = $this->formBuilder->open(['method' => 'GET']); + $form2 = $this->formBuilder->open(['method' => 'POST', 'class' => 'form', 'id' => 'id-form']); + $form3 = $this->formBuilder->open(['method' => 'GET', 'accept-charset' => 'UTF-16']); + $form4 = $this->formBuilder->open(['method' => 'GET', 'accept-charset' => 'UTF-16', 'files' => true]); + $form5 = $this->formBuilder->open(['method' => 'PUT']); - public function testOpeningForm() - { - $form1 = $this->formBuilder->open(['method' => 'GET']); - $form2 = $this->formBuilder->open(['method' => 'POST', 'class' => 'form', 'id' => 'id-form']); - $form3 = $this->formBuilder->open(['method' => 'GET', 'accept-charset' => 'UTF-16']); - $form4 = $this->formBuilder->open(['method' => 'GET', 'accept-charset' => 'UTF-16', 'files' => true]); - $form5 = $this->formBuilder->open(['method' => 'PUT']); + $this->assertEquals('
    ', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + $this->assertEquals('', $form5); + } + public function testClosingForm() + { + $this->assertEquals('
    ', $this->formBuilder->close()); + } - $this->assertEquals('
    ', $form1); - $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - $this->assertEquals('', $form4); - $this->assertEquals('', $form5); - } + public function testFormLabel() + { + $form1 = $this->formBuilder->label('foo', 'Foobar'); + $form2 = $this->formBuilder->label('foo', 'Foobar', ['class' => 'control-label']); + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } - public function testClosingForm() - { - $this->assertEquals('
    ', $this->formBuilder->close()); - } + public function testFormInput() + { + $form1 = $this->formBuilder->input('text', 'foo'); + $form2 = $this->formBuilder->input('text', 'foo', 'foobar'); + $form3 = $this->formBuilder->input('date', 'foobar', null, ['class' => 'span2']); + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + } - public function testFormLabel() - { - $form1 = $this->formBuilder->label('foo', 'Foobar'); - $form2 = $this->formBuilder->label('foo', 'Foobar', ['class' => 'control-label']); + public function testPasswordsNotFilled() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - } + $session->shouldReceive('getOldInput')->never(); + $form1 = $this->formBuilder->password('password'); - public function testFormInput() - { - $form1 = $this->formBuilder->input('text', 'foo'); - $form2 = $this->formBuilder->input('text', 'foo', 'foobar'); - $form3 = $this->formBuilder->input('date', 'foobar', null, ['class' => 'span2']); + $this->assertEquals('', $form1); + } - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - } + public function testFilesNotFilled() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + $session->shouldReceive('getOldInput')->never(); - public function testPasswordsNotFilled() - { - $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + $form = $this->formBuilder->file('img'); - $session->shouldReceive('getOldInput')->never(); + $this->assertEquals('', $form); + } - $form1 = $this->formBuilder->password('password'); + public function testFormText() + { + $form1 = $this->formBuilder->input('text', 'foo'); + $form2 = $this->formBuilder->text('foo'); + $form3 = $this->formBuilder->text('foo', 'foobar'); + $form4 = $this->formBuilder->text('foo', null, ['class' => 'span2']); - $this->assertEquals('', $form1); - } + $this->assertEquals('', $form1); + $this->assertEquals($form1, $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + } + public function testFormTextRepopulation() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + $this->setModel($model = ['relation' => ['key' => 'attribute'], 'other' => 'val']); - public function testFilesNotFilled() - { - $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + $session->shouldReceive('getOldInput')->twice()->with('name_with_dots')->andReturn('some value'); + $input = $this->formBuilder->text('name.with.dots', 'default value'); + $this->assertEquals('', $input); - $session->shouldReceive('getOldInput')->never(); + $session->shouldReceive('getOldInput')->once()->with('text.key.sub')->andReturn(null); + $input = $this->formBuilder->text('text[key][sub]', 'default value'); + $this->assertEquals('', $input); - $form = $this->formBuilder->file('img'); + $session->shouldReceive('getOldInput')->with('relation.key')->andReturn(null); + $input1 = $this->formBuilder->text('relation[key]'); - $this->assertEquals('', $form); - } + $this->setModel($model, false); + $input2 = $this->formBuilder->text('relation[key]'); + $this->assertEquals('', $input1); + $this->assertEquals($input1, $input2); + } - public function testFormText() - { - $form1 = $this->formBuilder->input('text', 'foo'); - $form2 = $this->formBuilder->text('foo'); - $form3 = $this->formBuilder->text('foo', 'foobar'); - $form4 = $this->formBuilder->text('foo', null, ['class' => 'span2']); + public function testFormRepopulationWithMixOfArraysAndObjects() + { + $this->formBuilder->model(['user' => (object) ['password' => 'apple']]); + $input = $this->formBuilder->text('user[password]'); + $this->assertEquals('', $input); - $this->assertEquals('', $form1); - $this->assertEquals($form1, $form2); - $this->assertEquals('', $form3); - $this->assertEquals('', $form4); - } + $this->formBuilder->model((object) ['letters' => ['a', 'b', 'c']]); + $input = $this->formBuilder->text('letters[1]'); + $this->assertEquals('', $input); + } + public function testFormPassword() + { + $form1 = $this->formBuilder->password('foo'); + $form2 = $this->formBuilder->password('foo', ['class' => 'span2']); - public function testFormTextRepopulation() - { - $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); - $this->setModel($model = ['relation' => ['key' => 'attribute'], 'other' => 'val']); + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } - $session->shouldReceive('getOldInput')->twice()->with('name_with_dots')->andReturn('some value'); - $input = $this->formBuilder->text('name.with.dots', 'default value'); - $this->assertEquals('', $input); + public function testFormHidden() + { + $form1 = $this->formBuilder->hidden('foo'); + $form2 = $this->formBuilder->hidden('foo', 'foobar'); + $form3 = $this->formBuilder->hidden('foo', null, ['class' => 'span2']); - $session->shouldReceive('getOldInput')->once()->with('text.key.sub')->andReturn(null); - $input = $this->formBuilder->text('text[key][sub]', 'default value'); - $this->assertEquals('', $input); + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + } - $session->shouldReceive('getOldInput')->with('relation.key')->andReturn(null); - $input1 = $this->formBuilder->text('relation[key]'); + public function testFormEmail() + { + $form1 = $this->formBuilder->email('foo'); + $form2 = $this->formBuilder->email('foo', 'foobar'); + $form3 = $this->formBuilder->email('foo', null, ['class' => 'span2']); - $this->setModel($model, false); - $input2 = $this->formBuilder->text('relation[key]'); + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + } - $this->assertEquals('', $input1); - $this->assertEquals($input1, $input2); - } + public function testFormTel() + { + $form1 = $this->formBuilder->tel('foo'); + $form2 = $this->formBuilder->tel('foo', 'foobar'); + $form3 = $this->formBuilder->tel('foo', null, ['class' => 'span2']); + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + } - public function testFormRepopulationWithMixOfArraysAndObjects() - { - $this->formBuilder->model(['user' => (object)['password' => 'apple']]); - $input = $this->formBuilder->text('user[password]'); - $this->assertEquals('', $input); + public function testFormNumber() + { + $form1 = $this->formBuilder->number('foo'); + $form2 = $this->formBuilder->number('foo', 1); + $form3 = $this->formBuilder->number('foo', null, ['class' => 'span2']); - $this->formBuilder->model((object)['letters' => ['a', 'b', 'c']]); - $input = $this->formBuilder->text('letters[1]'); - $this->assertEquals('', $input); - } + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + } + public function testFormDate() + { + $form1 = $this->formBuilder->date('foo'); + $form2 = $this->formBuilder->date('foo', '2015-02-20'); + $form3 = $this->formBuilder->date('foo', \Carbon\Carbon::now()); + $form4 = $this->formBuilder->date('foo', null, ['class' => 'span2']); - public function testFormPassword() - { - $form1 = $this->formBuilder->password('foo'); - $form2 = $this->formBuilder->password('foo', ['class' => 'span2']); + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + } - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - } + public function testFormTime() + { + $form1 = $this->formBuilder->time('foo'); + $form2 = $this->formBuilder->time('foo', \Carbon\Carbon::now()->format('H:i')); + $form3 = $this->formBuilder->time('foo', null, ['class' => 'span2']); + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + } - public function testFormHidden() - { - $form1 = $this->formBuilder->hidden('foo'); - $form2 = $this->formBuilder->hidden('foo', 'foobar'); - $form3 = $this->formBuilder->hidden('foo', null, ['class' => 'span2']); + public function testFormFile() + { + $form1 = $this->formBuilder->file('foo'); + $form2 = $this->formBuilder->file('foo', ['class' => 'span2']); - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - } + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } + public function testFormTextarea() + { + $form1 = $this->formBuilder->textarea('foo'); + $form2 = $this->formBuilder->textarea('foo', 'foobar'); + $form3 = $this->formBuilder->textarea('foo', null, ['class' => 'span2']); + $form4 = $this->formBuilder->textarea('foo', null, ['size' => '60x15']); - public function testFormEmail() - { - $form1 = $this->formBuilder->email('foo'); - $form2 = $this->formBuilder->email('foo', 'foobar'); - $form3 = $this->formBuilder->email('foo', null, ['class' => 'span2']); + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + } - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - } - - - public function testFormTel() - { - $form1 = $this->formBuilder->tel('foo'); - $form2 = $this->formBuilder->tel('foo', 'foobar'); - $form3 = $this->formBuilder->tel('foo', null, ['class' => 'span2']); - - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - } - - - public function testFormNumber() - { - $form1 = $this->formBuilder->number('foo'); - $form2 = $this->formBuilder->number('foo', 1); - $form3 = $this->formBuilder->number('foo', null, ['class' => 'span2']); - - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - } - - public function testFormDate() - { - $form1 = $this->formBuilder->date('foo'); - $form2 = $this->formBuilder->date('foo', '2015-02-20'); - $form3 = $this->formBuilder->date('foo', \Carbon\Carbon::now()); - $form4 = $this->formBuilder->date('foo', null, ['class' => 'span2']); - - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - $this->assertEquals('', $form4); - } - - public function testFormTime() - { - $form1 = $this->formBuilder->time('foo'); - $form2 = $this->formBuilder->time('foo', \Carbon\Carbon::now()->format('H:i')); - $form3 = $this->formBuilder->time('foo', null, ['class' => 'span2']); - - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - } - - - public function testFormFile() - { - $form1 = $this->formBuilder->file('foo'); - $form2 = $this->formBuilder->file('foo', ['class' => 'span2']); - - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - } - - - public function testFormTextarea() - { - $form1 = $this->formBuilder->textarea('foo'); - $form2 = $this->formBuilder->textarea('foo', 'foobar'); - $form3 = $this->formBuilder->textarea('foo', null, ['class' => 'span2']); - $form4 = $this->formBuilder->textarea('foo', null, ['size' => '60x15']); - - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - $this->assertEquals('', $form4); - } - - - public function testSelect() - { - $select = $this->formBuilder->select( + public function testSelect() + { + $select = $this->formBuilder->select( 'size', ['L' => 'Large', 'S' => 'Small'] ); - $this->assertEquals($select, ''); + $this->assertEquals($select, ''); - - - $select = $this->formBuilder->select( + $select = $this->formBuilder->select( 'size', ['L' => 'Large', 'S' => 'Small'], 'L' ); - $this->assertEquals($select, ''); + $this->assertEquals($select, ''); - - - $select = $this->formBuilder->select( + $select = $this->formBuilder->select( 'size', ['L' => 'Large', 'S' => 'Small'], null, ['class' => 'class-name', 'id' => 'select-id'] ); - $this->assertEquals($select, ''); + $this->assertEquals($select, ''); - - - $this->formBuilder->label('select-name-id'); - $select = $this->formBuilder->select( + $this->formBuilder->label('select-name-id'); + $select = $this->formBuilder->select( 'select-name-id', [], null, ['name' => 'select-name'] ); - $this->assertEquals($select, ''); - } + $this->assertEquals($select, ''); + } + public function testFormSelectRepopulation() + { + $list = ['L' => 'Large', 'M' => 'Medium', 'S' => 'Small']; + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + $this->setModel($model = ['size' => ['key' => 'S'], 'other' => 'val']); - public function testFormSelectRepopulation() - { - $list = ['L' => 'Large', 'M' => 'Medium', 'S' => 'Small']; - $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); - $this->setModel($model = ['size' => ['key' => 'S'], 'other' => 'val']); + $session->shouldReceive('getOldInput')->twice()->with('size')->andReturn('M'); + $select = $this->formBuilder->select('size', $list, 'S'); + $this->assertEquals($select, ''); - $session->shouldReceive('getOldInput')->twice()->with('size')->andReturn('M'); - $select = $this->formBuilder->select('size', $list, 'S'); - $this->assertEquals($select, ''); + $session->shouldReceive('getOldInput')->twice()->with('size.multi')->andReturn(['L', 'S']); + $select = $this->formBuilder->select('size[multi][]', $list, 'M', ['multiple' => 'multiple']); + $this->assertEquals($select, ''); - $session->shouldReceive('getOldInput')->twice()->with('size.multi')->andReturn(['L', 'S']); - $select = $this->formBuilder->select('size[multi][]', $list, 'M', ['multiple' => 'multiple']); - $this->assertEquals($select, ''); + $session->shouldReceive('getOldInput')->once()->with('size.key')->andReturn(null); + $select = $this->formBuilder->select('size[key]', $list); + $this->assertEquals($select, ''); + } - $session->shouldReceive('getOldInput')->once()->with('size.key')->andReturn(null); - $select = $this->formBuilder->select('size[key]', $list); - $this->assertEquals($select, ''); - } - - public function testFormWithOptionalPlaceholder() - { - $select = $this->formBuilder->select( + public function testFormWithOptionalPlaceholder() + { + $select = $this->formBuilder->select( 'size', ['L' => 'Large', 'S' => 'Small'], null, ['placeholder' => 'Select One...'] ); - $this->assertEquals($select, ''); + $this->assertEquals($select, ''); - $select = $this->formBuilder->select( + $select = $this->formBuilder->select( 'size', ['L' => 'Large', 'S' => 'Small'], 'L', ['placeholder' => 'Select One...'] ); - $this->assertEquals($select, ''); - } - - public function testFormSelectYear() - { - $select1 = $this->formBuilder->selectYear('year', 2000, 2020); - $select2 = $this->formBuilder->selectYear('year', 2000, 2020, null, ['id' => 'foo']); - $select3 = $this->formBuilder->selectYear('year', 2000, 2020, '2000'); - - $this->assertContains('', $select2); - $this->assertContains('', $range); - $this->assertContains('', $range); - } - - - public function testFormSelectMonth() - { - $month1 = $this->formBuilder->selectMonth('month'); - $month2 = $this->formBuilder->selectMonth('month', '1'); - $month3 = $this->formBuilder->selectMonth('month', null, ['id' => 'foo']); - - $this->assertContains('', $month2); - $this->assertContains('', $form1); - $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - $this->assertEquals('', $form4); - } - - - public function testFormCheckboxRepopulation() - { - $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); - $session->shouldReceive('getOldInput')->withNoArgs()->andReturn([1]); - - $session->shouldReceive('getOldInput')->once()->with('check')->andReturn(null); - $check = $this->formBuilder->checkbox('check', 1, true); - $this->assertEquals('', $check); - - $session->shouldReceive('getOldInput')->with('check.key')->andReturn('yes'); - $check = $this->formBuilder->checkbox('check[key]', 'yes'); - $this->assertEquals('', $check); - - $session->shouldReceive('getOldInput')->with('multicheck')->andReturn([1, 3]); - $check1 = $this->formBuilder->checkbox('multicheck[]', 1); - $check2 = $this->formBuilder->checkbox('multicheck[]', 2, true); - $check3 = $this->formBuilder->checkbox('multicheck[]', 3); - - $this->assertEquals('', $check1); - $this->assertEquals('', $check2); - $this->assertEquals('', $check3); - } - - - public function testFormCheckboxWithModelRelation() - { - $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); - $session->shouldReceive('getOldInput')->withNoArgs()->andReturn(array()); - $session->shouldReceive('getOldInput')->with('items')->andReturn(null); - - $mockModel2 = new StdClass; - $mockModel2->id = 2; - $mockModel3 = new StdClass; - $mockModel3->id = 3; - $this->setModel(array('items' => new Collection(array($mockModel2, $mockModel3)))); - - $check1 = $this->formBuilder->checkbox('items[]', 1); - $check2 = $this->formBuilder->checkbox('items[]', 2); - $check3 = $this->formBuilder->checkbox('items[]', 3, false); - $check4 = $this->formBuilder->checkbox('items[]', 4, true); - - $this->assertEquals('', $check1); - $this->assertEquals('', $check2); - $this->assertEquals('', $check3); - $this->assertEquals('', $check4); - } - - - public function testFormCheckboxWithoutSession() - { - $form1 = $this->formBuilder->checkbox('foo'); - $form2 = $this->formBuilder->checkbox('foo', 'foobar', true); - - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - } - - - public function testFormRadio() - { - $form1 = $this->formBuilder->input('radio', 'foo'); - $form2 = $this->formBuilder->radio('foo'); - $form3 = $this->formBuilder->radio('foo', 'foobar', true); - $form4 = $this->formBuilder->radio('foo', 'foobar', false, ['class' => 'span2']); - - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - $this->assertEquals('', $form3); - $this->assertEquals('', $form4); - } - - - public function testFormRadioRepopulation() - { - $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); - - $session->shouldReceive('getOldInput')->with('radio')->andReturn(1); - - $radio1 = $this->formBuilder->radio('radio', 1); - $radio2 = $this->formBuilder->radio('radio', 2, true); - - $this->assertEquals('', $radio1); - $this->assertEquals('', $radio2); - } - - - public function testFormSubmit() - { - $form1 = $this->formBuilder->submit('foo'); - $form2 = $this->formBuilder->submit('foo', ['class' => 'span2']); - - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - } - - - public function testFormButton() - { - $form1 = $this->formBuilder->button('foo'); - $form2 = $this->formBuilder->button('foo', ['class' => 'span2']); - - $this->assertEquals('', $form1); - $this->assertEquals('', $form2); - } - - - public function testResetInput() - { - $resetInput = $this->formBuilder->reset('foo'); - $this->assertEquals('', $resetInput); - } - - - public function testImageInput() - { - $url = 'http://laravel.com/'; - $image = $this->formBuilder->image($url); - - $this->assertEquals('', $image); - } - - protected function setModel(array $data, $object = true) - { - if ($object) $data = new FormBuilderModelStub($data); - - $this->formBuilder->model($data, ['method' => 'GET']); - } -} - -class FormBuilderModelStub { - - protected $data; - - public function __construct(array $data = []) - { - foreach ($data as $key => $val) - { - if (is_array($val)) $val = new self($val); - - $this->data[$key] = $val; + $this->assertEquals($select, ''); } - } + public function testFormSelectYear() + { + $select1 = $this->formBuilder->selectYear('year', 2000, 2020); + $select2 = $this->formBuilder->selectYear('year', 2000, 2020, null, ['id' => 'foo']); + $select3 = $this->formBuilder->selectYear('year', 2000, 2020, '2000'); - public function __get($key) - { - return $this->data[$key]; - } + $this->assertContains('', $select2); + $this->assertContains('', $range); + $this->assertContains('', $range); + } + + public function testFormSelectMonth() + { + $month1 = $this->formBuilder->selectMonth('month'); + $month2 = $this->formBuilder->selectMonth('month', '1'); + $month3 = $this->formBuilder->selectMonth('month', null, ['id' => 'foo']); + + $this->assertContains('', $month2); + $this->assertContains('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + } + + public function testFormCheckboxRepopulation() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + $session->shouldReceive('getOldInput')->withNoArgs()->andReturn([1]); + + $session->shouldReceive('getOldInput')->once()->with('check')->andReturn(null); + $check = $this->formBuilder->checkbox('check', 1, true); + $this->assertEquals('', $check); + + $session->shouldReceive('getOldInput')->with('check.key')->andReturn('yes'); + $check = $this->formBuilder->checkbox('check[key]', 'yes'); + $this->assertEquals('', $check); + + $session->shouldReceive('getOldInput')->with('multicheck')->andReturn([1, 3]); + $check1 = $this->formBuilder->checkbox('multicheck[]', 1); + $check2 = $this->formBuilder->checkbox('multicheck[]', 2, true); + $check3 = $this->formBuilder->checkbox('multicheck[]', 3); + + $this->assertEquals('', $check1); + $this->assertEquals('', $check2); + $this->assertEquals('', $check3); + } + + public function testFormCheckboxWithModelRelation() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + $session->shouldReceive('getOldInput')->withNoArgs()->andReturn([]); + $session->shouldReceive('getOldInput')->with('items')->andReturn(null); + + $mockModel2 = new StdClass(); + $mockModel2->id = 2; + $mockModel3 = new StdClass(); + $mockModel3->id = 3; + $this->setModel(['items' => new Collection([$mockModel2, $mockModel3])]); + + $check1 = $this->formBuilder->checkbox('items[]', 1); + $check2 = $this->formBuilder->checkbox('items[]', 2); + $check3 = $this->formBuilder->checkbox('items[]', 3, false); + $check4 = $this->formBuilder->checkbox('items[]', 4, true); + + $this->assertEquals('', $check1); + $this->assertEquals('', $check2); + $this->assertEquals('', $check3); + $this->assertEquals('', $check4); + } + + public function testFormCheckboxWithoutSession() + { + $form1 = $this->formBuilder->checkbox('foo'); + $form2 = $this->formBuilder->checkbox('foo', 'foobar', true); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } + + public function testFormRadio() + { + $form1 = $this->formBuilder->input('radio', 'foo'); + $form2 = $this->formBuilder->radio('foo'); + $form3 = $this->formBuilder->radio('foo', 'foobar', true); + $form4 = $this->formBuilder->radio('foo', 'foobar', false, ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + } + + public function testFormRadioRepopulation() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + + $session->shouldReceive('getOldInput')->with('radio')->andReturn(1); + + $radio1 = $this->formBuilder->radio('radio', 1); + $radio2 = $this->formBuilder->radio('radio', 2, true); + + $this->assertEquals('', $radio1); + $this->assertEquals('', $radio2); + } + + public function testFormSubmit() + { + $form1 = $this->formBuilder->submit('foo'); + $form2 = $this->formBuilder->submit('foo', ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } + + public function testFormButton() + { + $form1 = $this->formBuilder->button('foo'); + $form2 = $this->formBuilder->button('foo', ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } + + public function testResetInput() + { + $resetInput = $this->formBuilder->reset('foo'); + $this->assertEquals('', $resetInput); + } + + public function testImageInput() + { + $url = 'http://laravel.com/'; + $image = $this->formBuilder->image($url); + + $this->assertEquals('', $image); + } + + protected function setModel(array $data, $object = true) + { + if ($object) { + $data = new FormBuilderModelStub($data); + } + + $this->formBuilder->model($data, ['method' => 'GET']); + } +} + +class FormBuilderModelStub +{ + protected $data; + + public function __construct(array $data = []) + { + foreach ($data as $key => $val) { + if (is_array($val)) { + $val = new self($val); + } + + $this->data[$key] = $val; + } + } + + public function __get($key) + { + return $this->data[$key]; + } + + public function __isset($key) + { + return isset($this->data[$key]); + } } diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index 9e32201..1bc3eed 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -1,59 +1,57 @@ urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET')); + $this->htmlBuilder = new HtmlBuilder($this->urlGenerator); + } - /** - * Setup the test environment. - */ - public function setUp() - { - $this->urlGenerator = new UrlGenerator(new RouteCollection, Request::create('/foo', 'GET')); - $this->htmlBuilder = new HtmlBuilder($this->urlGenerator); - } + /** + * Destroy the test environment. + */ + public function tearDown() + { + m::close(); + } - /** - * Destroy the test environment. - */ - public function tearDown() - { - m::close(); - } + public function testDl() + { + $list = [ + 'foo' => 'bar', + 'bing' => 'baz', + ]; - public function testDl() - { - $list = [ - 'foo' => 'bar', - 'bing' => 'baz' - ]; + $attributes = ['class' => 'example']; - $attributes = ['class' => 'example']; + $result = $this->htmlBuilder->dl($list, $attributes); - $result = $this->htmlBuilder->dl($list, $attributes); + $this->assertEquals('
    foo
    bar
    bing
    baz
    ', $result); + } - $this->assertEquals('
    foo
    bar
    bing
    baz
    ', $result); - } + public function testMeta() + { + $result = $this->htmlBuilder->meta('description', 'Lorem ipsum dolor sit amet.'); - public function testMeta() - { - $result = $this->htmlBuilder->meta('description', 'Lorem ipsum dolor sit amet.'); + $this->assertEquals(''.PHP_EOL, $result); + } - $this->assertEquals(''.PHP_EOL, $result); - } + public function testMetaOpenGraph() + { + $result = $this->htmlBuilder->meta(null, 'website', ['property' => 'og:type']); - public function testMetaOpenGraph() - { - $result = $this->htmlBuilder->meta(null, 'website', ['property' => 'og:type']); - - $this->assertEquals(''.PHP_EOL, $result); - } + $this->assertEquals(''.PHP_EOL, $result); + } public function testFavicon() { @@ -61,7 +59,6 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase { $target = $this->urlGenerator->to('bar.ico'); $result = $this->htmlBuilder->favicon('http://foo.com/bar.ico'); - $this->assertEquals('' . PHP_EOL, $result); + $this->assertEquals(''.PHP_EOL, $result); } - }