Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
claar committed Mar 4, 2015
2 parents 6fc4fb7 + f6096b0 commit 9fc9f7e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Former
## A Laravelish way to create and format forms

[![Build Status](http://img.shields.io/travis/Anahkiasen/former.svg?style=flat)](https://travis-ci.org/Anahkiasen/former)
[![Build Status](http://img.shields.io/travis/formers/former.svg?style=flat)](https://travis-ci.org/formers/former)
[![Latest Stable Version](http://img.shields.io/packagist/v/anahkiasen/former.svg?style=flat)](https://packagist.org/packages/anahkiasen/former)
[![Total Downloads](http://img.shields.io/packagist/dt/anahkiasen/former.svg?style=flat)](https://packagist.org/packages/anahkiasen/former)

Expand Down
12 changes: 6 additions & 6 deletions src/Former/Form/Fields/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function selectValue($value, $parent = null)

foreach ($parent->getChildren() as $child) {
// Search by value
if ($child->getAttribute('value') == $value) {
if ($child->getAttribute('value') === $value) {
$child->selected('selected');
}

Expand Down Expand Up @@ -255,13 +255,13 @@ public function addOption($text = null, $value = null, $attributes = array())
/**
* Use the results from a Fluent/Eloquent query as options
*
* @param array $results An array of Eloquent models
* @param string $value The attribute to use as text
* @param string $key The attribute to use as value
* @param array $results An array of Eloquent models
* @param string|function $text The value to use as text
* @param string|array $attributes The data to use as attributes
*/
public function fromQuery($results, $value = null, $key = null)
public function fromQuery($results, $text = null, $attributes = null)
{
$this->options(Helpers::queryToArray($results, $value, $key));
$this->options(Helpers::queryToArray($results, $text, $attributes));

return $this;
}
Expand Down
88 changes: 61 additions & 27 deletions src/Former/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ public static function translate($key, $fallback = null)
/**
* Transforms an array of models into an associative array
*
* @param array|object $query The array of results
* @param string $value The attribute to use as value
* @param string $key The attribute to use as key
* @param array|object $query The array of results
* @param string|function $text The value to use as text
* @param string|array $attributes The data to use as attributes
*
* @return array A data array
*/
public static function queryToArray($query, $value = null, $key = null)
public static function queryToArray($query, $text = null, $attributes = null)
{
// Automatically fetch Lang objects for people who store translated options lists
// Same of unfetched queries
Expand All @@ -124,42 +124,76 @@ public static function queryToArray($query, $value = null, $key = null)
}
}

//Convert parametrs of old format to new format
if (!is_callable($text)) {
$optionTextValue = $text;
$text = function ($model) use($optionTextValue) {
if ($optionTextValue and isset($model->$optionTextValue)) {
return $model->$optionTextValue;
} elseif (method_exists($model, '__toString')) {
return $model->__toString();
} else {
return null;
}
};
}

if (!is_array($attributes)) {
if (is_string($attributes)) {
$attributes = ['value' => $attributes];
} else {
$attributes = ['value' => null];
}
}

if (!isset($attributes['value'])) {
$attributes['value'] = null;
}
//-------------------------------------------------

// Populates the new options
foreach ($query as $model) {

// If it's an array, convert to object
if (is_array($model)) {
$model = (object) $model;
}

// Calculate the value
if ($value and isset($model->$value)) {
$modelValue = $model->$value;
} elseif (method_exists($model, '__toString')) {
$modelValue = $model->__toString();
} else {
$modelValue = null;
}

// Calculate the key
if ($key and isset($model->$key)) {
$modelKey = $model->$key;
} elseif (method_exists($model, 'getKey')) {
$modelKey = $model->getKey();
} elseif (isset($model->id)) {
$modelKey = $model->id;
} else {
$modelKey = $modelValue;
}
// Calculate option text
$optionText = $text($model);

// Skip if no text value found
if (!$modelValue) {
if (!$optionText) {
continue;
}

$array[$modelKey] = (string) $modelValue;
//Collect option attributes
foreach ($attributes as $optionAttributeName => $modelAttributeName) {
if (is_callable($modelAttributeName)) {
$optionAttributeValue = $modelAttributeName($model);
} elseif ($modelAttributeName and isset($model->$modelAttributeName)) {
$optionAttributeValue = $model->$modelAttributeName;
} elseif($optionAttributeName === 'value') {
//For backward compatibility
if (method_exists($model, 'getKey')) {
$optionAttributeValue = $model->getKey();
} elseif (isset($model->id)) {
$optionAttributeValue = $model->id;
} else {
$optionAttributeValue = $optionText;
}
} else {
$optionAttributeValue = '';
}

//For backward compatibility
if (count($attributes) === 1) {
$array[$optionAttributeValue] = (string) $optionText;
} else {
$array[$optionText][$optionAttributeName] = (string) $optionAttributeValue;
}
}
}

return isset($array) ? $array : $query;
return !empty($array) ? $array : $query;
}
}
60 changes: 57 additions & 3 deletions tests/Fields/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SelectTest extends FormerTests
*
* @var array
*/
private $options = array('foo' => 'bar', 'kal' => 'ter');
private $options = array(0 => 'baz', 'foo' => 'bar', 'kal' => 'ter');

////////////////////////////////////////////////////////////////////
//////////////////////////////// TESTS /////////////////////////////
Expand All @@ -38,14 +38,14 @@ public function testMultiselect()
public function testMultiselectOptions()
{
$select = $this->former->multiselect('foo')->options($this->options)->value(array('foo', 'kal'))->__toString();
$matcher = $this->controlGroup('<select id="foo" multiple="true" name="foo[]"><option value="foo" selected="selected">bar</option><option value="kal" selected="selected">ter</option></select>');
$matcher = $this->controlGroup('<select id="foo" multiple="true" name="foo[]"><option value="0">baz</option><option value="foo" selected="selected">bar</option><option value="kal" selected="selected">ter</option></select>');
$this->assertEquals($matcher, $select);
}

public function testSelectOptions()
{
$select = $this->former->select('foo')->options($this->options)->__toString();
$matcher = $this->controlGroup('<select id="foo" name="foo"><option value="foo">bar</option><option value="kal">ter</option></select>');
$matcher = $this->controlGroup('<select id="foo" name="foo"><option value="0">baz</option><option value="foo">bar</option><option value="kal">ter</option></select>');

$this->assertEquals($matcher, $select);
}
Expand All @@ -67,6 +67,7 @@ public function testSelectPlaceholder()
$matcher = $this->controlGroup(
'<select id="foo" name="foo">'.
'<option value="" disabled="disabled" selected="selected">Pick something</option>'.
'<option value="0">baz</option>'.
'<option value="foo">bar</option>'.
'<option value="kal">ter</option>'.
'</select>');
Expand All @@ -80,13 +81,28 @@ public function testPlaceholderUnselected()
$matcher = $this->controlGroup(
'<select id="foo" name="foo">'.
'<option value="" disabled="disabled">Pick something</option>'.
'<option value="0">baz</option>'.
'<option value="foo" selected="selected">bar</option>'.
'<option value="kal">ter</option>'.
'</select>');

$this->assertEquals($matcher, $select);
}

public function testSelectNumeric()
{
$select = $this->former->select('foo')->value(0)->options($this->options)->placeholder('Pick something')->__toString();
$matcher = $this->controlGroup(
'<select id="foo" name="foo">'.
'<option value="" disabled="disabled">Pick something</option>'.
'<option value="0" selected="selected">baz</option>'.
'<option value="foo">bar</option>'.
'<option value="kal">ter</option>'.
'</select>');

$this->assertEquals($matcher, $select);
}

public function testSelectLang()
{
$select = $this->former->select('foo')->options($this->translator->get('pagination'), 'previous')->__toString();
Expand Down Expand Up @@ -159,6 +175,41 @@ public function testSelectEloquentArray()
$this->assertEquals($matcher, $select);
}

public function testSelectEloquentArrayWithOptionTextAsFunction()
{
$optionTextFunction = function($model) {
return $model->foo;
};
for ($i = 0; $i < 2; $i++) {
$eloquent[] = (object) array('age' => $i, 'foo' => 'bar');
}
$select = $this->former->select('foo')->fromQuery($eloquent, $optionTextFunction, 'age')->__toString();
$matcher = $this->controlGroup('<select id="foo" name="foo"><option value="0">bar</option><option value="1">bar</option></select>');

$this->assertEquals($matcher, $select);
}

public function testSelectEloquentArrayWithOptionAttributes()
{
$optionTextFunction = function($model) {
return $model->foo . $model->age;
};
$optionDataFunction = function($model) {
return $model->foo;
};
$optionAttributes = [
'value' => 'age',
'data-test' => $optionDataFunction,
];
for ($i = 0; $i < 2; $i++) {
$eloquent[] = (object) array('age' => $i, 'foo' => 'bar');
}
$select = $this->former->select('foo')->fromQuery($eloquent, $optionTextFunction, $optionAttributes)->__toString();
$matcher = $this->controlGroup('<select id="foo" name="foo"><option value="0" data-test="bar">bar0</option><option value="1" data-test="bar">bar1</option></select>');

$this->assertEquals($matcher, $select);
}

public function testNestedRelationships()
{
for ($i = 0; $i < 2; $i++) {
Expand Down Expand Up @@ -196,6 +247,7 @@ public function testSelectOptionsValue()
$select = $this->former->select('foo')->data_foo('bar')->options($this->options, 'kal')->__toString();
$matcher = $this->controlGroup(
'<select data-foo="bar" id="foo" name="foo">'.
'<option value="0">baz</option>'.
'<option value="foo">bar</option>'.
'<option value="kal" selected="selected">ter</option>'.
'</select>');
Expand All @@ -208,6 +260,7 @@ public function testSelectOptionsValueMethod()
$select = $this->former->select('foo')->data_foo('bar')->options($this->options)->select('kal')->__toString();
$matcher = $this->controlGroup(
'<select data-foo="bar" id="foo" name="foo">'.
'<option value="0">baz</option>'.
'<option value="foo">bar</option>'.
'<option value="kal" selected="selected">ter</option>'.
'</select>');
Expand All @@ -222,6 +275,7 @@ public function testCanAddAdditionalOptionsToCreatedSelect()
$matcher = $this->controlGroup(
'<select id="foo" name="foo">'.
'<option value=""></option>'.
'<option value="0">baz</option>'.
'<option value="foo">bar</option>'.
'<option value="kal">ter</option>'.
'<option value="ter">bis</option>'.
Expand Down

0 comments on commit 9fc9f7e

Please sign in to comment.