Skip to content

Commit

Permalink
Merge branch '5.6' into 5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
mlantz committed Sep 5, 2018
2 parents 68caec9 + 974605f commit 777b6d3
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 35 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"require-dev": {
"illuminate/database": "5.7.*",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~5.4"
"phpunit/phpunit": "~7.1"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 0 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="true"
strict="false"
verbose="true"
>
<testsuites>
Expand Down
60 changes: 47 additions & 13 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class FormBuilder
*/
protected $csrfToken;

/**
* Consider Request variables while auto fill.
* @var bool
*/
protected $considerRequest = false;

/**
* The session store implementation.
*
Expand Down Expand Up @@ -108,6 +114,7 @@ class FormBuilder
* @param \Illuminate\Contracts\Routing\UrlGenerator $url
* @param \Illuminate\Contracts\View\Factory $view
* @param string $csrfToken
* @param Request $request
*/
public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken, Request $request = null)
{
Expand Down Expand Up @@ -190,7 +197,7 @@ public function setModel($model)
{
$this->model = $model;
}

/**
* Get the current model instance on the form builder.
*
Expand Down Expand Up @@ -516,7 +523,7 @@ public function week($name, $value = null, $options = [])

return $this->input('week', $name, $value, $options);
}

/**
* Create a file input field.
*
Expand Down Expand Up @@ -730,7 +737,7 @@ public function selectMonth($name, $selected = null, $options = [], $format = '%
*/
public function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = [])
{
if (is_array($display)) {
if (is_iterable($display)) {
return $this->optionGroup($display, $value, $selected, $optgroupAttributes, $attributes);
}

Expand All @@ -755,7 +762,7 @@ protected function optionGroup($list, $label, $selected, array $attributes = [],
$space = str_repeat("&nbsp;", $level);
foreach ($list as $value => $display) {
$optionAttributes = $optionsAttributes[$value] ?? [];
if (is_array($display)) {
if (is_iterable($display)) {
$html[] = $this->optionGroup($display, $value, $selected, $attributes, $optionAttributes, $level+5);
} else {
$html[] = $this->option($space.$display, $value, $selected, $optionAttributes);
Expand Down Expand Up @@ -805,7 +812,7 @@ protected function placeholderOption($display, $selected)
'value' => '',
];

return $this->toHtmlString('<option' . $this->html->attributes($options) . ' hidden="hidden">' . e($display, false) . '</option>');
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display, false) . '</option>');
}

/**
Expand Down Expand Up @@ -907,7 +914,7 @@ protected function getCheckedState($type, $name, $value, $checked)
return $this->getRadioCheckedState($name, $value, $checked);

default:
return $this->getValueAttribute($name) === $value;
return $this->compareValues($name, $value);
}
}

Expand Down Expand Up @@ -960,7 +967,21 @@ protected function getRadioCheckedState($name, $value, $checked)
return $checked;
}

return $this->getValueAttribute($name) === $value;
return $this->compareValues($name, $value);
}

/**
* Determine if the provide value loosely compares to the value assigned to the field.
* Use loose comparison because Laravel model casting may be in affect and therefore
* 1 == true and 0 == false.
*
* @param string $name
* @param string $value
* @return bool
*/
protected function compareValues($name, $value)
{
return $this->getValueAttribute($name) == $value;
}

/**
Expand Down Expand Up @@ -1276,7 +1297,7 @@ public function getValueAttribute($name, $value = null)
if ($hasNullMiddleware
&& is_null($old)
&& is_null($value)
&& ! is_null($this->view->shared('errors'))
&& !is_null($this->view->shared('errors'))
&& count($this->view->shared('errors')) > 0
) {
return null;
Expand All @@ -1297,14 +1318,27 @@ public function getValueAttribute($name, $value = null)
}
}

/**
* Take Request in fill process
* @param bool $consider
*/
public function considerRequest($consider = true)
{
$this->considerRequest = $consider;
}

/**
* Get value from current Request
* @param $name
* @return array|null|string
*/
protected function request($name)
{
if (! isset($this->request)) {
if (!$this->considerRequest) {
return null;
}

if (!isset($this->request)) {
return null;
}

Expand Down Expand Up @@ -1342,16 +1376,16 @@ public function old($name)
$key = $this->transformKey($name);
$payload = $this->session->getOldInput($key);

if (! is_array($payload)) {
if (!is_array($payload)) {
return $payload;
}

if (! in_array($this->type, ['select', 'checkbox'])) {
if (! isset($this->payload[$key])) {
if (!in_array($this->type, ['select', 'checkbox'])) {
if (!isset($this->payload[$key])) {
$this->payload[$key] = collect($payload);
}

if (! empty($this->payload[$key])) {
if (!empty($this->payload[$key])) {
$value = $this->payload[$key]->shift();
return $value;
}
Expand Down
6 changes: 5 additions & 1 deletion src/HtmlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function style($url, $attributes = [], $secure = null)
{
$defaults = ['media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet'];

$attributes = array_merge($attributes, $defaults);
$attributes = array_merge($defaults, $attributes);

$attributes['href'] = $this->url->asset($url, $secure);

Expand Down Expand Up @@ -455,6 +455,10 @@ protected function attributeElement($key, $value)
return $value ? $key : '';
}

if (is_array($value) && $key === 'class') {
return 'class="' . implode(' ', $value) . '"';
}

if (! is_null($value)) {
return $key . '="' . e($value, false) . '"';
}
Expand Down
6 changes: 3 additions & 3 deletions tests/FormAccessibleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Illuminate\Database\Capsule\Manager as Capsule;
use Mockery as m;

class FormAccessibleTest extends PHPUnit_Framework_TestCase
class FormAccessibleTest extends PHPUnit\Framework\TestCase
{
public function setUp()
{
Expand Down Expand Up @@ -74,12 +74,12 @@ public function testItCanGetRelatedValueForForms()
$model = new ModelThatUsesForms($this->modelData);
$this->assertEquals($model->getFormValue('address.street'), 'abcde st');
}

public function testItCanUseGetAccessorValuesWhenThereAreNoFormAccessors()
{
$model = new ModelThatUsesForms($this->modelData);
$this->formBuilder->setModel($model);

$this->assertEquals($this->formBuilder->getValueAttribute('email'), 'mutated@tjshafer.com');
}

Expand Down
Loading

0 comments on commit 777b6d3

Please sign in to comment.