Applied fixes from StyleCI

This commit is contained in:
Adam Engebretson authored and StyleCI Bot committed 2015-09-03 13:18:42 -04:00
1 parent 8b7c357e78
commit 017d60144f
8 files changed
+1157 -1172

No files matched your search

+202 -210
View File
@@ -1,14 +1,16 @@
<?php namespace Collective\Html;
<?php
namespace Collective\Html;
use DateTime;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Session\Store as Session;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
class FormBuilder {
use Macroable;
class FormBuilder
{
use Macroable;
/**
* The HTML builder instance.
@@ -20,7 +22,7 @@ class FormBuilder {
/**
* The URL generator instance.
*
* @var \Illuminate\Routing\UrlGenerator $url
* @var \Illuminate\Routing\UrlGenerator
*/
protected $url;
@@ -84,9 +86,9 @@ class FormBuilder {
*/
public function __construct(HtmlBuilder $html, UrlGenerator $url, $csrfToken)
{
$this->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 '<form' . $attributes . '>' . $append;
return '<form'.$attributes.'>'.$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 '</form>';
return '</form>';
}
/**
@@ -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 '<label for="' . $name . '"' . $options . '>' . $value . '</label>';
return '<label for="'.$name.'"'.$options.'>'.$value.'</label>';
}
/**
@@ -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 '<input' . $this->html->attributes($options) . '>';
return '<input'.$this->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 '<textarea' . $options . '>' . e($value) . '</textarea>';
return '<textarea'.$options.'>'.e($value).'</textarea>';
}
/**
@@ -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 "<select{$options}>{$list}</select>";
return "<select{$options}>{$list}</select>";
}
/**
@@ -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 '<optgroup label="' . e($label) . '">' . implode('', $html) . '</optgroup>';
return '<optgroup label="'.e($label).'">'.implode('', $html).'</optgroup>';
}
/**
@@ -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 '<option' . $this->html->attributes($options) . '>' . e($display) . '</option>';
return '<option'.$this->html->attributes($options).'>'.e($display).'</option>';
}
/**
@@ -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 '<option' . $this->html->attributes($options) . '>' . e($display) . '</option>';
return '<option'.$this->html->attributes($options).'>'.e($display).'</option>';
}
/**
@@ -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 '<button' . $this->html->attributes($options) . '>' . $value . '</button>';
return '<button'.$this->html->attributes($options).'>'.$value.'</button>';
}
/**
@@ -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;
}
}
+15 -11
View File
@@ -1,17 +1,21 @@
<?php namespace Collective\Html;
<?php
namespace Collective\Html;
use Illuminate\Support\Facades\Facade;
/**
* @see \Collective\Html\FormBuilder
*/
class FormFacade extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'form'; }
}
class FormFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'form';
}
}
+401 -374
View File
@@ -1,438 +1,465 @@
<?php namespace Collective\Html;
<?php
namespace Collective\Html;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Traits\Macroable;
class HtmlBuilder {
class HtmlBuilder
{
use Macroable;
use Macroable;
/**
* The URL generator instance.
*
* @var \Illuminate\Routing\UrlGenerator
*/
protected $url;
/**
* The URL generator instance.
*
* @var \Illuminate\Routing\UrlGenerator
*/
protected $url;
/**
* Create a new HTML builder instance.
*
* @param \Illuminate\Routing\UrlGenerator $url
*
* @return void
*/
public function __construct(UrlGenerator $url = null)
{
$this->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 '<script'.$this->attributes($attributes).'></script>'.PHP_EOL;
}
return '<script'.$this->attributes($attributes).'></script>'.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 '<link'.$this->attributes($attributes).'>'.PHP_EOL;
}
return '<link'.$this->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 '<img src="'.$this->url->asset($url, $secure).'"'.$this->attributes($attributes).'>';
}
return '<img src="'.$this->url->asset($url, $secure).'"'.$this->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 '<link'.$this->attributes($attributes).'>'.PHP_EOL;
}
return '<link'.$this->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 '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
}
return '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
}
/**
* 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 '<a href="'.$email.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
}
return '<a href="'.$email.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
}
/**
* Obfuscate an e-mail address to prevent spam-bots from sniffing it.
*
* @param string $email
*
* @return string
*/
public function email($email)
{
return str_replace('@', '&#64;', $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('@', '&#64;', $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 = "<dl{$attributes}>";
$html = "<dl{$attributes}>";
foreach ($list as $key => $value) {
$html .= "<dt>$key</dt><dd>$value</dd>";
}
foreach ($list as $key => $value)
{
$html .= "<dt>$key</dt><dd>$value</dd>";
}
$html .= '</dl>';
$html .= '</dl>';
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}</{$type}>";
}
return "<{$type}{$attributes}>{$html}</{$type}>";
}
/**
* 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 '<li>'.e($value).'</li>';
}
}
/**
* 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 '<li>'.e($value).'</li>';
}
}
/**
* 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 '<li>'.$key.$this->listing($type, $value).'</li>';
}
}
/**
* 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 '<li>'.$key.$this->listing($type, $value).'</li>';
}
}
/**
* 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 '<meta'.$this->attributes($attributes).'>'.PHP_EOL;
}
$attributes = array_merge($defaults, $attributes);
return '<meta'.$this->attributes($attributes).'>'.PHP_EOL;
}
}
+15 -11
View File
@@ -1,17 +1,21 @@
<?php namespace Collective\Html;
<?php
namespace Collective\Html;
use Illuminate\Support\Facades\Facade;
/**
* @see \Collective\Html\HtmlBuilder
*/
class HtmlFacade extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'html'; }
}
class HtmlFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'html';
}
}
+55 -56
View File
@@ -1,67 +1,66 @@
<?php namespace Collective\Html;
<?php
namespace Collective\Html;
use Illuminate\Support\ServiceProvider;
class HtmlServiceProvider extends ServiceProvider {
class HtmlServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->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'];
}
}
+28 -28
View File
@@ -1,68 +1,68 @@
<?php
if ( ! function_exists('link_to'))
{
if (!function_exists('link_to')) {
/**
* Generate a HTML link.
*
* @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($url, $title = null, $attributes = array(), $secure = null)
function link_to($url, $title = null, $attributes = [], $secure = null)
{
return app('html')->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);
}