Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
Conflicts:
	src/Impreso/Element/Select.php
	src/Impreso/Element/TextArea.php
  • Loading branch information
Michał Lipek committed Oct 9, 2014
2 parents 0b825e4 + 7b5f30a commit 17e9c8a
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 32 deletions.
24 changes: 20 additions & 4 deletions src/Impreso/Container/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ public function populate(array $data, $strict = false)
}
foreach ($elements as $element) {
/* @var $element \Impreso\Element\Element */
$element->setValue($value);
if ($element->isArrayType()) {
// I'm not proud of this code, but it works... TODO refactor this
$element->setValue($data[substr($key, 0, -3)]);
continue;
}
else {
$element->setValue($value);
}
}
}
return $this;
Expand All @@ -55,9 +62,17 @@ public function getData()
$result = array();
$tmp = array();
foreach ($this->getElements() as $element) {
/* @var $element \Impreso\Element\Base */
/* @var $element \Impreso\Element\Element */
if ($element->has('disabled')) continue;
$tmp[] = urlencode($element->getName()).'='.urlencode($element->getValue());
$value = $element->getValue();
if (is_array($value)) {
foreach ($value as $v) {
$tmp[] = urlencode($element->getName()).'='.urlencode($v);
}
}
else {
$tmp[] = urlencode($element->getName()).'='.urlencode($element->getValue());
}
}
parse_str(implode('&', $tmp), $result);
return $result;
Expand Down Expand Up @@ -85,13 +100,14 @@ public function getElementsByName($name)
if (empty($result)) {
if (preg_match('/\[([0-9]+)\]$/', $name, $matches)) {
$index = (int)$matches[1];
$elementArrayName = str_replace("[$index]", '[]', $element->getName());
$elementArrayName = str_replace("[$index]", '[]', $name);
$arrayElements = array();
foreach ($this->getElements() as $element) {
if ($element->getName() == $elementArrayName) {
$arrayElements[] = $element;
}
}

if (isset($arrayElements[$index])) {
$result[] = $arrayElements[$index];
}
Expand Down
4 changes: 4 additions & 0 deletions src/Impreso/Element/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ function getRawValue()
return $this->get('value');
}

public function isArrayType()
{
return false;
}
}
11 changes: 7 additions & 4 deletions src/Impreso/Element/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function validate()
$this->validateErrors = array();

foreach ($this->validators as $validator) {
/* @var $validator Validator */
$tmp = $validator->validate($this->getValue());
if (!$tmp) {
$result = false;
Expand Down Expand Up @@ -166,11 +167,13 @@ public function getLabel()
return $this->label;
}

abstract function render();
abstract public function isArrayType();

abstract function setValue($value);
abstract public function render();

abstract function getValue();
abstract public function setValue($value);

abstract function getRawValue();
abstract public function getValue();

abstract public function getRawValue();
}
5 changes: 5 additions & 0 deletions src/Impreso/Element/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public function getRawValue()
{
return $this->get('value');
}

public function isArrayType()
{
return false;
}
}
57 changes: 34 additions & 23 deletions src/Impreso/Element/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,17 @@ public function __construct($name = null)
public function render()
{
$attributes = $this->getAttributes();

$options = '';
foreach ($this->getOptions() as $k => $v) {
$optionAttributes = array();
if (is_array($v)) {

if (is_array($v)) {
$optionsGroup = new HtmlElement('optgroup', '', array(
'label' => $k,
));
foreach ($v as $k2 => $v2) {
$optionAttributes = array();
$optionAttributes['value'] = $k2;
if ($k2 == $this->getValue()) {
if ($k2 == $this->getValue() || is_array($this->getValue()) && in_array($k2, $this->getValue())) {
$optionAttributes['selected'] = true;
}
$option = new HtmlElement('option', $v2, $optionAttributes);
Expand All @@ -62,8 +61,9 @@ public function render()
$options .= $optionsGroup;
}
else {
$optionAttributes = array();
$optionAttributes['value'] = $k;
if ($k == $this->getValue()) {
if ($k == $this->getValue() || is_array($this->getValue()) && in_array($k, $this->getValue())) {
$optionAttributes['selected'] = true;
}
$options .= (string)(new HtmlElement('option', $v, $optionAttributes));
Expand All @@ -80,24 +80,37 @@ public function render()
*/
public function setValue($value)
{
if ($this->isSimple()) {
if (!array_key_exists($value, $this->options)) {
throw new \InvalidArgumentException("Key '$value' in Impreso\\Element\\Select '{$this->getName()}' options doesn't exists!");
}
if (is_array($value)) {
return $this->setValues($value);
}
else {
$found = false;
foreach ($this->options as $v) {
if (array_key_exists($value, $v)) {
$found = true;
break;
}
}
if (!$found) {

$options = $this->options;
if (!$this->isSimple()) {
$options = call_user_func_array('array_merge', $options);
}

if (!array_key_exists($value, $options)) {
throw new \InvalidArgumentException("Key '$value' in Impreso\\Element\\Select '{$this->getName()}' options doesn't exists!");
}

$this->value = (string)$value;
return $this;
}

public function setValues(array $values)
{
$options = $this->options;
if (!$this->isSimple()) {
$options = call_user_func_array('array_merge', $options);
}

$this->value = array();
foreach ($values as $value) {
if (!array_key_exists($value, $options)) {
throw new \InvalidArgumentException("Key '$value' in Impreso\\Element\\Select '{$this->getName()}' options doesn't exists!");
}
$this->value[] = $value;
}
$this->value = (string)$value;
return $this;
}

Expand All @@ -121,10 +134,8 @@ private function isSimple()
return true;
}

public function testFluentInterface()
public function isArrayType()
{
$element = new Select('test');
$element->setOptions(array('test' => 'Test'));
$this->assertEquals($element, $element->setValue('test'));
return $this->has('multiple');
}
}
5 changes: 5 additions & 0 deletions src/Impreso/Element/TextArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ function getRawValue()
{
return $this->value;
}

public function isArrayType()
{
return false;
}
}
21 changes: 20 additions & 1 deletion tests/Impreso/Container/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

use Impreso\Container\Form;
use Impreso\Element\Password;
use Impreso\Element\Submit;
use Impreso\Element\Select;
use Impreso\Element\Text;
use Impreso\Validator\CustomValidator;
use Impreso\Validator\PasswordCharactersValidator;

Expand Down Expand Up @@ -49,4 +50,22 @@ public function testErrors()
$form->addError('xyz');
$this->assertEquals(array('abc', 'xyz'), $form->getErrors());
}

public function testGetElementsByName()
{
$form = new Form();
$select = new Select('name[]');
$select->setOptions(array(
2 => 'Bron', 10 => 'Bran', 5 => 'Brienne',
));
$select->set('multiple', true);
$form->addElement($select);

$input = new Text('age');
$form->addElement($input);

//$this->assertEquals(array($select), $form->getElementsByName('name'));
$this->assertEquals(array($select), $form->getElementsByName('name[]'));
$this->assertEquals(array($input), $form->getElementsByName('age'));
}
}
54 changes: 54 additions & 0 deletions tests/Impreso/Element/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,58 @@ public function testSetValue()
$element->setValue('');
$this->assertEquals('', $element->getValue());
}

public function testMutliple()
{
$element = new Select();
$element->set('multiple', true);
$this->assertContains(' multiple', $element->render());

$element->setOptions(array(
'bran' => 'Bran',
'bron' => 'Bron',
'brienne' => 'Brienne',
));
$element->setValue(array(
'bran', 'brienne',
));
$values = $element->getValue();
$this->assertCount(2, $values);
$this->assertContains('bran', $values);
$this->assertContains('brienne', $values);
}

public function testMutlipleWithOptgroups()
{
$element = new Select();
$element->set('multiple', true);
$this->assertContains(' multiple', $element->render());

$element->setOptions(array(
'starks' => array(
'bran' => 'Bran',
'arya' => 'Arya',
'sansa' => 'Sansa',
),
'lanisters' => array(
'cersei' => 'Cersei',
'joffrey' => 'Joffrey',
),
'targaryen' => array(
'daenerys' => 'Queen of the Andals and the Rhoynar and the First Men, Lord of the Seven Kingdoms. Khaleesi of the Great Grass Sea',
'viserys' => 'Viserys',
)
));
$element->setValue(array(
'sansa', 'cersei', 'daenerys',
));
$values = $element->getValue();
$this->assertCount(3, $values);
$this->assertContains('daenerys', $values);
$this->assertContains('cersei', $values);
$this->assertContains('sansa', $values);

$html = $element->render();
$this->assertEquals(3, substr_count($html, 'selected'));
}
}
69 changes: 69 additions & 0 deletions tests/Impreso/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,73 @@ public function testFormWithInactiveElements()
$this->assertArrayNotHasKey('select', $result);
$this->assertArrayHasKey('text', $result);
}

public function testPopulateFormWithElementsWithArrayName()
{
$form = new Form();
$form->addElement(new Text('name[]'));
$form->addElement(new Text('name[]'));
$form->addElement(new Text('name[]'));

$form->populate(array(
'name' => array(
'Petyr',
'Varys',
'Tyrion',
)
));

$data = $form->getData();
$this->assertArrayHasKey('name', $data);
$this->assertCount(3, $data['name']);
}

public function testPopulateFormWithMultipleSelect()
{
$form = new Form();
$select = new Select('name[]');
$select->setOptions(array(
2 => 'Bron', 10 => 'Bran', 5 => 'Brienne',
));
$select->set('multiple', true);
$form->addElement($select);

$form->populate(array(
'name' => array(2, 10)
));

$data = $form->getData();
$this->assertArrayHasKey('name', $data);
$names = $data['name'];
$this->assertContains(2, $names);
$this->assertContains(10, $names);
$this->assertCount(2, $names);
}

public function testPopulateFormWithMultipleSelectAndSomeOtherFields()
{
$form = new Form();
$select = new Select('name[]');
$select->setOptions(array(
2 => 'Bron', 10 => 'Bran', 5 => 'Brienne',
));
$select->set('multiple', true);
$form->addElement($select);

$input = new Text('age');
$form->addElement($input);

$form->populate(array(
'name' => array(2, 10),
'age' => 15,
));

$data = $form->getData();
$this->assertArrayHasKey('name', $data);
$names = $data['name'];
$this->assertContains(2, $names);
$this->assertEquals(15, $data['age']);
$this->assertContains(10, $names);
$this->assertCount(2, $names);
}
}

0 comments on commit 17e9c8a

Please sign in to comment.