Skip to content

Commit e2355f6

Browse files
committed
Merge branch 'master' into feat/docs
2 parents d6369ab + faa1454 commit e2355f6

File tree

13 files changed

+156
-55
lines changed

13 files changed

+156
-55
lines changed

CHANGELOG.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7-
## [1.1.0] - XXXX-XX-XX
7+
## [2.0.0] - 2017-12-11
88
### Added
9+
- Documentation of package;
910
- Renderers to facilitate integrations of template-engines:
1011
- Added `Renderer` interface;
1112
- Added `TwigRenderer` that integrates `twig/twig`;
@@ -15,12 +16,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1516
- Added template pack `DefaultTemplatePack`. Defined as default template pack;
1617
- Added template pack `Bootstrap4TemplatePack` that integrates Bootstrap v4.0.0-beta.2.
1718
- `Config` singleton class allowing:
18-
- Configure custom template packs class;
19-
- Configure custom messages class;
20-
- Configure custom renderers class.
21-
- Added extra arg `label` to method `getContext` of `Widget` class.
19+
- Configure custom template packs;
20+
- Configure custom messages;
21+
- Configure custom renderers.
22+
- Added extra arg `label` to method `getContext` of `Widget` class;
23+
- `BoundWidget` class to represent the choices of a `ChoiceWidget` in `BoundField`, allowing individual render or data access to each option.
2224

2325
### Changed
26+
- `BoundField` moved from `Fields` to new namespace `Bounds`;
2427
- `BoundField` attribute name `choices` changed to `options`;
2528
- `BoundField` attribute `options` now return an array instead of formated string;
2629
- `Widgets`, `labelTag` and `ErrorList` now render through default renderer instead of formatter `fleshgrinder/format`;

src/Fields/BoundField.php renamed to src/Bounds/BoundField.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace PHPForm\Fields;
2+
namespace PHPForm\Bounds;
33

44
use PHPForm\Config;
55

@@ -10,7 +10,7 @@ class BoundField
1010
private $form;
1111
private $field;
1212
private $name;
13-
private $options_cache;
13+
private $bound_widgets_cache;
1414

1515
public $html_name;
1616
public $help_text;
@@ -55,18 +55,25 @@ public function __get($name)
5555
}
5656

5757
if ($name == 'options') {
58-
if (!isset($options_cache)) {
59-
$options_cache = $this->getOptions();
58+
if (!isset($bound_widgets_cache)) {
59+
$bound_widgets_cache = $this->getSubWidgets();
6060
}
61-
return $options_cache;
61+
return $bound_widgets_cache;
6262
}
6363
}
6464

65-
private function getOptions(array $attrs = array())
65+
private function getSubWidgets(array $attrs = array())
6666
{
67+
$bounds = [];
68+
6769
$attrs = $this->buildWidgetAttrs($attrs);
70+
$options = $this->field->getWidget()->getOptions($this->html_name, $this->getValue(), $attrs);
71+
72+
foreach ($options as $option) {
73+
$bounds[] = new BoundWidget($option);
74+
}
6875

69-
return $this->field->getWidget()->getOptions($this->html_name, $this->getValue(), $attrs);
76+
return $bounds;
7077
}
7178

7279
protected function asWidget($widget = null, array $attrs = array())

src/Bounds/BoundWidget.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace PHPForm\Bounds;
3+
4+
use PHPForm\Config;
5+
6+
class BoundWidget
7+
{
8+
private $data;
9+
private $template;
10+
11+
public $for;
12+
public $type;
13+
public $name;
14+
public $value;
15+
public $label;
16+
17+
public function __construct(array $data)
18+
{
19+
$this->for = $data["for"];
20+
$this->type = $data["type"];
21+
$this->name = $data["name"];
22+
$this->value = $data["value"];
23+
$this->label = $data["label"];
24+
$this->template = $data["template"];
25+
26+
unset($data["template"]);
27+
28+
$this->data = $data;
29+
}
30+
31+
public function __toString()
32+
{
33+
$renderer = Config::getInstance()->getRenderer();
34+
35+
return $renderer->render($this->template, $this->data);
36+
}
37+
}

src/Config.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ class Config extends Singleton
3939
*/
4040
public function setTemplatePack(string $template_pack)
4141
{
42-
$this->template_packs = array_unshift($this->template_packs, $template_pack);
42+
array_unshift($this->template_packs, $template_pack);
4343
}
4444

4545
/**
46-
* Set messages class.
46+
* Redefine default messages.
4747
*
48-
* @param string Class name of Renderer.
48+
* @param array Messages array.
4949
*/
50-
public function setMessages(string $messages_class)
50+
public function setMessages(array $messages)
5151
{
52-
$this->messages_class = $messages_class;
52+
$this->messages_class::setMessages($messages);
5353
}
5454

5555
/**

src/Forms/Form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use Iterator;
88
use UnexpectedValueException;
99

10+
use PHPForm\Bounds\BoundField;
1011
use PHPForm\Errors\ErrorList;
1112
use PHPForm\Exceptions\ValidationError;
12-
use PHPForm\Fields\BoundField;
1313

1414
abstract class Form implements ArrayAccess, Iterator, Countable
1515
{

src/Messages.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,32 @@
88

99
class Messages
1010
{
11-
const REQUIRED = 'This field is required.';
12-
const INVALID_CHOICE = 'Select a valid choice. "{choice}" is not one of the available choices.';
13-
const INVALID_LIST = 'Enter a list of values.';
14-
const INVALID_DATE = 'Enter a valid date.';
15-
const INVALID_DATETIME = 'Enter a valid date/time.';
16-
const INVALID_NUMBER = 'Enter a whole number.';
17-
const INVALID_EMAIL = 'Enter a valid email address.';
18-
const INVALID_URL = 'Enter a valid URL.';
19-
const INVALID_FILE = 'Invalid file submitted.';
20-
const EMPTY_FILE = 'The submitted file is empty.';
21-
const INVALID_FILE_MAX_SIZE = 'Ensure the file has at most {limit} bytes (it has {value} bytes).';
22-
const INVALID_FILE_TYPE = 'Ensure the file is one of "{valid_types}" types (it has {type}).';
23-
const INVALID_MAX_LENGTH = 'Ensure this value has at most {limit} character (it has {value}).';
24-
const INVALID_MAX_VALUE = 'Ensure this value is less than or equal to {limit}.';
25-
const INVALID_MIN_LENGTH = 'Ensure this value has at least {limit} character (it has {value}).';
26-
const INVALID_MIN_VALUE = 'Ensure this value is greater than or equal to {limit}.';
11+
/**
12+
* @var array Default messages
13+
*/
14+
private static $messages = array(
15+
"REQUIRED" => 'This field is required.',
16+
"INVALID_CHOICE" => 'Select a valid choice. "{choice}" is not one of the available choices.',
17+
"INVALID_LIST" => 'Enter a list of values.',
18+
"INVALID_DATE" => 'Enter a valid date.',
19+
"INVALID_DATETIME" => 'Enter a valid date/time.',
20+
"INVALID_NUMBER" => 'Enter a whole number.',
21+
"INVALID_EMAIL" => 'Enter a valid email address.',
22+
"INVALID_URL" => 'Enter a valid URL.',
23+
"INVALID_FILE" => 'Invalid file submitted.',
24+
"EMPTY_FILE" => 'The submitted file is empty.',
25+
"INVALID_FILE_MAX_SIZE" => 'Ensure the file has at most {limit} bytes (it has {value} bytes).',
26+
"INVALID_FILE_TYPE" => 'Ensure the file is one of "{valid_types}" types (it has {type}).',
27+
"INVALID_MAX_LENGTH" => 'Ensure this value has at most {limit} character (it has {value}).',
28+
"INVALID_MAX_VALUE" => 'Ensure this value is less than or equal to {limit}.',
29+
"INVALID_MIN_LENGTH" => 'Ensure this value has at least {limit} character (it has {value}).',
30+
"INVALID_MIN_VALUE" => 'Ensure this value is greater than or equal to {limit}.'
31+
);
32+
33+
public static function setMessages(array $messages)
34+
{
35+
self::$messages = array_merge(self::$messages, $messages);
36+
}
2737

2838
/**
2939
* Format message witg context.
@@ -35,7 +45,7 @@ class Messages
3545
*/
3646
public static function format(string $id, array $context = null)
3747
{
38-
$message = defined("static::$id") ? constant("static::$id") : $id;
48+
$message = array_key_exists($id, self::$messages) ? self::$messages[$id] : $id;
3949

4050
if (!is_null($context)) {
4151
$message = Formatter::format($message, $context);

src/Widgets/FileInput.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ public function formatValue($value)
3232
*/
3333
public function valueFromData($data, $files, string $name)
3434
{
35-
if (array_key_exists($name, $files)) {
36-
return $files[$name];
37-
}
38-
39-
return null;
35+
return !is_null($files) && array_key_exists($name, $files) ? $files[$name] : null;
4036
}
4137
}

src/Widgets/Widget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected function formatValue($value)
9494
*/
9595
public function valueFromData($data, $files, string $name)
9696
{
97-
return array_key_exists($name, $data) ? $data[$name] : null;
97+
return !is_null($data) && array_key_exists($name, $data) ? $data[$name] : null;
9898
}
9999

100100
/**

tests/unit/Fields/BoundFieldTest.php renamed to tests/unit/Bounds/BoundFieldTest.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
2-
namespace PHPForm\Unit\Fields;
2+
namespace PHPForm\Unit\Bounds;
33

44
use PHPUnit\Framework\TestCase;
55

6+
use PHPForm\Bounds\BoundField;
7+
use PHPForm\Bounds\BoundWidget;
68
use PHPForm\Errors\ErrorList;
79
use PHPForm\Exceptions\ValidationError;
8-
use PHPForm\Fields\BoundField;
910
use PHPForm\Fields\CharField;
1011
use PHPForm\Fields\ChoiceField;
1112
use PHPForm\Widgets\RadioSelect;
@@ -214,16 +215,6 @@ public function testOptions()
214215
$field = new ChoiceField(["choices" => array("option1" => "Option1")]);
215216
$bound = new BoundField($this->simple_form, $field, "name");
216217

217-
$expected = array(array(
218-
"for" => "id_name_1",
219-
"type" => null,
220-
"name" => "name",
221-
"value" => "option1",
222-
"label" => "Option1",
223-
"attrs" => array(),
224-
"template" => "select_option.html"
225-
));
226-
227-
$this->assertEquals($expected, $bound->options);
218+
$this->assertInstanceOf(BoundWidget::class, $bound->options[0]);
228219
}
229220
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
namespace PHPForm\Unit\Bounds;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
use PHPForm\Bounds\BoundWidget;
7+
use PHPForm\Widgets\RadioSelect;
8+
9+
class BoundWidgetTest extends TestCase
10+
{
11+
12+
protected function setUp()
13+
{
14+
$this->data = array(
15+
"for" => "for",
16+
"type" => "type",
17+
"name" => "name",
18+
"value" => "value",
19+
"label" => "label",
20+
"attrs" => array(),
21+
"template" => RadioSelect::TEMPLATE_CHOICE,
22+
);
23+
}
24+
25+
public function testConstruct()
26+
{
27+
$bound = new BoundWidget($this->data);
28+
29+
$this->assertAttributeEquals("for", "for", $bound);
30+
$this->assertAttributeEquals("type", "type", $bound);
31+
$this->assertAttributeEquals("name", "name", $bound);
32+
$this->assertAttributeEquals("value", "value", $bound);
33+
$this->assertAttributeEquals("label", "label", $bound);
34+
$this->assertAttributeEquals("radio_select_option.html", "template", $bound);
35+
}
36+
37+
public function testToString()
38+
{
39+
$bound = new BoundWidget($this->data);
40+
41+
$expected = '<label for="for"><input name="name" type="type" value="value"/> label</label>';
42+
$this->assertXmlStringEqualsXmlString((string) $bound, $expected);
43+
}
44+
}

0 commit comments

Comments
 (0)