diff --git a/.editorconfig b/.editorconfig index 8fa3c2a..284dbaf 100644 --- a/.editorconfig +++ b/.editorconfig @@ -16,3 +16,6 @@ trim_trailing_whitespace = true [*.json] indent_size = 2 insert_final_newline = ignore + +[*.html] +insert_final_newline = ignore diff --git a/CHANGELOG.md b/CHANGELOG.md index 5715cc9..434f997 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,30 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [1.0.0] - 2017-12-07 - - First release; +## [1.1.0] - XXXX-XX-XX +### Added + - Renderers to facilitate integrations of template-engines: + - Added `Renderer` interface; + - Added `TwigRenderer` that integrates `twig/twig`; + - Added fallback template loading support. + - Template packs to facilitate customization and extensibility of templates: + - Added template pack `default` and defined as fallback; + - Added template pack `bootstrap4` that integrates custom elements of Bootstrap v4.0.0-beta.2. + - Added extra arg `label` on method `getContext` of `Widget` class; + - Support to configure renderer and template pack through `Config` singleton class; + +### Changed + - Class name `PHPFormConfig` to `Config` and moved to `src/` directory; + - `BoundField` attribute name `choices` changed to `options`; + - `BoundField` attribute `options` now return an array instead of formated string; + - `Widgets`, `labelTag` and `ErrorList` now render through default renderer instead of formatter `fleshgrinder/format`; + - `CheckboxSelectMultiple` and `RadioSelect` widget wrapped in an unordered list tag instead of previous `div`; + - Method name `getSubWidgets` to `getOptions` in `Widgets` class; + +### Removed: + - Method `asUL` from `ErrorList` class; + - `config.php` and `templates.php` files; + - Static method `flatatt` from `Attributes` class; ## [1.0.1] - 2017-12-07 ### Added @@ -14,3 +36,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Fix formatValue method on ChoiceWidget when value is empty or null; - Fix validate on ChoiceField when value if empty and not required. + +## [1.0.0] - 2017-12-07 + - First release; diff --git a/composer.json b/composer.json index 4e186ca..98f954c 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ }, "require": { "php": ">=7.0.0", - "fleshgrinder/format": "^1.1" + "fleshgrinder/format": "^1.1", + "twig/twig": "^2.4" }, "require-dev": { "phpunit/phpunit": "~6.0", @@ -38,5 +39,9 @@ "phpcs --standard=phpcs.xml", "phpunit -c phpunit.xml" ] + }, + "config": { + "sort-packages": true, + "optimize-autoloader": true } } diff --git a/config.php b/config.php index a44dd7c..1e7d929 100644 --- a/config.php +++ b/config.php @@ -4,7 +4,6 @@ class PHPFormConfig { const MESSAGES_FILE_PATH = 'src/messages.php'; - const TEMPLATES_FILE_PATH = 'src/templates.php'; private static $config = null; @@ -14,7 +13,6 @@ class PHPFormConfig private function __construct() { $this->messages = include static::MESSAGES_FILE_PATH; - $this->templates = include static::TEMPLATES_FILE_PATH; } public static function getInstance() diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..ab7c2d2 --- /dev/null +++ b/src/Config.php @@ -0,0 +1,75 @@ +templates_dir . $template_pack . "/"; + + if (!file_exists($path)) { + trigger_error("Template pack dir '$path' don't exists.", E_USER_ERROR); + } + + return $path; + } + + /** + * Return renderer class instantiated + * + * @return PHPForm\Renderers\Renderer + */ + public function getRenderer() + { + if (is_null($this->renderer)) { + $fallback_pack_dir = $this->buildPath($this->fallback_template_pack); + $pack_dir = !is_null($this->template_pack) ? $this->buildPath($this->template_pack) : null; + + $this->renderer = new $this->renderer_class($fallback_pack_dir, $pack_dir); + } + + return $this->renderer; + } + + /** + * Define template pack + * + * @param string + */ + public function setTemplatePack($template_pack) + { + $this->template_pack = $template_pack; + } +} diff --git a/src/Errors/ErrorList.php b/src/Errors/ErrorList.php index 1d2c2b2..75d0a25 100644 --- a/src/Errors/ErrorList.php +++ b/src/Errors/ErrorList.php @@ -3,42 +3,27 @@ use ArrayObject; -use Fleshgrinder\Core\Formatter; - -use PHPForm\PHPFormConfig; +use PHPForm\Config; class ErrorList extends ArrayObject { + const TEMPLATE = "error_list.html"; + /** * Returns the error list rendered as HTML. * * @return string */ public function __toString() - { - return $this->asUL(); - } - - /** - * Returns an error dictionary as an unordered list in HTML. - * - * @return string - */ - public function asUL() { if (!count($this)) { return ''; } - $items = []; - $list_item_template = PHPFormConfig::getITemplate("ERRORLIST_ITEM"); - - foreach ($this as $error) { - $items[] = Formatter::format($list_item_template, array("content" => $error)); - } - - $list_template = PHPFormConfig::getITemplate("ERRORLIST"); + $renderer = Config::getInstance()->getRenderer(); - return Formatter::format($list_template, array("items" => implode($items))); + return $renderer->render(self::TEMPLATE, array( + "errors" => $this, + )); } } diff --git a/src/Fields/BoundField.php b/src/Fields/BoundField.php index 60544f2..887c59d 100644 --- a/src/Fields/BoundField.php +++ b/src/Fields/BoundField.php @@ -1,17 +1,16 @@ getValue(); } - if ($name == 'choices') { - if (!isset($subwidgets_cache)) { - $subwidgets_cache = $this->getSubWidgets(); + if ($name == 'options') { + if (!isset($options_cache)) { + $options_cache = $this->getOptions(); } - return $subwidgets_cache; + return $options_cache; } - return parent::__get($name); + return null; } - private function getSubWidgets(array $attrs = array()) + private function getOptions(array $attrs = array()) { - $widget = $this->field->getWidget(); $attrs = $this->buildWidgetAttrs($attrs); - return $widget->getSubWidgets($this->html_name, $this->getValue(), $attrs); + return $this->field->getWidget()->getOptions($this->html_name, $this->getValue(), $attrs); } protected function asWidget($widget = null, array $attrs = array()) @@ -79,10 +77,10 @@ protected function asWidget($widget = null, array $attrs = array()) $attrs = $this->buildWidgetAttrs($attrs); - return $widget->render($this->html_name, $this->getValue(), $attrs); + return $widget->render($this->html_name, $this->getValue(), $this->label, $attrs); } - public function labelTag($contents = null, array $attrs = null) + public function labelTag($contents = null, array $attrs = array()) { $contents = is_null($contents) ? $this->label : $contents; @@ -90,20 +88,15 @@ public function labelTag($contents = null, array $attrs = null) return ""; } - if (!is_null($attrs)) { - $attrs = Attributes::flatatt($attrs); - } - $widget = $this->field->getWidget(); - $label_tpl = PHPFormConfig::getITemplate("LABEL"); - $label_required_tpl = PHPFormConfig::getITemplate("LABEL_REQUIRED"); + $renderer = Config::getInstance()->getRenderer(); - return Formatter::format($label_tpl, array( + return $renderer->render(self::TEMPLATE_LABEL, array( "for" => $widget->buildAutoId($this->html_name), "attrs" => $attrs, "contents" => $contents, - "required" => $this->field->isRequired() ? $label_required_tpl : null + "required" => $this->field->isRequired() )); } diff --git a/src/Fields/MultipleChoiceField.php b/src/Fields/MultipleChoiceField.php index b3d8f12..746987c 100644 --- a/src/Fields/MultipleChoiceField.php +++ b/src/Fields/MultipleChoiceField.php @@ -4,8 +4,6 @@ */ namespace PHPForm\Fields; -use Fleshgrinder\Core\Formatter; - use PHPForm\Exceptions\ValidationError; use PHPForm\PHPFormConfig; use PHPForm\Widgets\SelectMultiple; diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 94b24ac..4fb5ae5 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -16,7 +16,7 @@ abstract class Form implements ArrayAccess, Iterator, Countable { - const PREFIX_TEMPLATE = '{prefix}-{field_name}'; + const PREFIX_FORMAT = '%s-%s'; const NON_FIELD_ERRORS = '__all__'; /** @@ -145,10 +145,7 @@ public function __get(string $name) public function addPrefix(string $field_name) { if (!is_null($this->prefix)) { - return Formatter::format(self::PREFIX_TEMPLATE, array( - "prefix" => $this->prefix, - "field_name" => $field_name - )); + return sprintf(static::PREFIX_FORMAT, $this->prefix, $field_name); } return $field_name; diff --git a/src/Renderers/Renderer.php b/src/Renderers/Renderer.php new file mode 100644 index 0000000..ed4a1ca --- /dev/null +++ b/src/Renderers/Renderer.php @@ -0,0 +1,12 @@ +addLoader(new Twig_Loader_Filesystem($templates_dir)); + } + + $loaders->addLoader(new Twig_Loader_Filesystem($fallback_templates_dir)); + + $this->twig = new Twig_Environment($loaders); + } + + public function getTemplate(string $template_name) + { + return $this->twig->load($template_name); + } + + public function render(string $template_name, array $context) + { + $template = $this->getTemplate($template_name); + return $template->render($context); + } +} diff --git a/src/Singleton.php b/src/Singleton.php new file mode 100644 index 0000000..a0e51a8 --- /dev/null +++ b/src/Singleton.php @@ -0,0 +1,38 @@ + $value) { - $flat[] = Formatter::format('{name}="{value}"', array( - "name" => $name, - "value" => htmlentities($value) - )); - } - - return implode(" ", $flat); - } - public static function prettyName($name) { return ucfirst(str_replace("_", " ", $name)); diff --git a/src/Widgets/CheckboxInput.php b/src/Widgets/CheckboxInput.php index 0f63c0e..37e2f24 100644 --- a/src/Widgets/CheckboxInput.php +++ b/src/Widgets/CheckboxInput.php @@ -6,16 +6,17 @@ class CheckboxInput extends Input { - protected $input_type = 'checkbox'; + const TEMPLATE = 'checkbox.html'; + const INPUT_TYPE = 'checkbox'; - public function getContext(string $name, $value, array $attrs = null) + public function getContext(string $name, $value, string $label = null, array $attrs = null) { if ($value) { $attrs = is_null($attrs) ? array() : $attrs; $attrs["checked"] = "checked"; } - return parent::getContext($name, $value, $attrs); + return parent::getContext($name, $value, $label, $attrs); } public function valueFromData($data, $files, string $name) diff --git a/src/Widgets/CheckboxSelectMultiple.php b/src/Widgets/CheckboxSelectMultiple.php index d6a220a..ab6ebad 100644 --- a/src/Widgets/CheckboxSelectMultiple.php +++ b/src/Widgets/CheckboxSelectMultiple.php @@ -8,22 +8,11 @@ class CheckboxSelectMultiple extends ChoiceWidget { + const TEMPLATE = 'checkbox_select.html'; + const TEMPLATE_CHOICE = 'checkbox_select_option.html'; + const INPUT_TYPE = 'checkbox'; + protected $allow_multiple_selected = true; protected $option_inherits_attrs = true; - protected $selected_attribute = "checked"; - protected $input_type = "checkbox"; - - /** - * The constructor. - */ - public function __construct(array $choices = array(), array $attrs = null) - { - $this->template = PHPFormConfig::getITemplate("CHECKBOX_SELECT_MULTIPLE"); - $this->template_choice = sprintf( - PHPFormConfig::getITemplate("CHECKBOX_SELECT_MULTIPLE_ITEM"), - PHPFormConfig::getITemplate("INPUT") - ); - - parent::__construct($choices, $attrs); - } + protected $selected_attribute = 'checked'; } diff --git a/src/Widgets/ChoiceWidget.php b/src/Widgets/ChoiceWidget.php index a9fe9fa..7e8d8ca 100644 --- a/src/Widgets/ChoiceWidget.php +++ b/src/Widgets/ChoiceWidget.php @@ -4,17 +4,35 @@ */ namespace PHPForm\Widgets; -use Fleshgrinder\Core\Formatter; - use PHPForm\Utils\Attributes; abstract class ChoiceWidget extends Widget { - protected $template_choice = ""; + const TEMPLATE_CHOICE = ''; + const INPUT_TYPE = null; + + /** + * Allow multiple choices being selected. + * @var boolean + */ protected $allow_multiple_selected = false; - protected $input_type = null; + + /** + * Option inherit the attrs of mains widget. + * @var boolean + */ protected $option_inherits_attrs = true; + + /** + * Attr name when option is selected. + * @var string + */ protected $selected_attribute = "selected"; + + /** + * Valid choices for this widget. + * @var array + */ protected $choices; /** @@ -27,31 +45,54 @@ public function __construct(array $choices = array(), array $attrs = null) $this->setChoices($choices); } + /** + * @param array $choices Choices to be setted. + */ public function setChoices(array $choices) { $this->choices = $choices; } - public function buildName($name) + /** + * If allow multiple selected, add [] to the end of name. + * + * @param string $name + * @return string + */ + protected function buildName(string $name) { - if ($this->allow_multiple_selected && substr($name, -2, 2) !== "[]") { - $name = $name . "[]"; - } - - return $name; + return $this->allow_multiple_selected ? $name . "[]" : $name; } - public function getContext(string $name, $value, array $attrs = null) + /** + * Prepare context to be used on render method. + * + * @param string $name Field name. + * @param mixed $value Field value. + * @param array $attrs Extra widget attributes. + * + * @return array + */ + protected function getContext(string $name, $value, string $label = null, array $attrs = null) { - $context = parent::getContext($name, $value, $attrs); + $context = parent::getContext($name, $value, $label, $attrs); $context["name"] = $this->buildName($name); - $context["options"] = implode($this->getSubWidgets($name, $value, $attrs)); + $context["options"] = $this->getOptions($name, $value, $attrs); return $context; } - public function getSubWidgets(string $name, $value, array $attrs = null) + /** + * Prepare options. + * + * @param string $name Choice name. + * @param mixed $value Choice value. + * @param array $attrs Extra choice attributes. + * + * @return array + */ + public function getOptions(string $name, $value, array $attrs = null) { $value = $this->formatValue($value); $subwidgets = array(); @@ -63,21 +104,33 @@ public function getSubWidgets(string $name, $value, array $attrs = null) $selected = false; if (!$has_selected || $this->allow_multiple_selected) { - $selected = in_array($choice_value, $value); - $has_selected = $selected; + $has_selected = $selected = in_array($choice_value, $value); } - $context = $this->getSubWidgetContext($name, $choice_value, $choice_label, $selected, $index, $attrs); - - $subwidgets[] = Formatter::format($this->template_choice, $context); - - $index++; + $subwidgets[] = $this->buildOption( + $name, + $choice_value, + $choice_label, + $selected, + $index++, + $attrs + ); } return $subwidgets; } - public function getSubWidgetContext( + /** + * @param string $name + * @param mixed $value + * @param string $label + * @param bool $is_selected + * @param int $index + * @param array|null $attrs + * + * @return array + */ + protected function buildOption( string $name, $value, string $label, @@ -99,14 +152,22 @@ public function getSubWidgetContext( return array( "for" => $this->buildAutoId($name, $index), - "type" => $this->input_type, - "name" => htmlentities($this->buildName($name)), - "value" => htmlentities($value), - "label" => htmlentities($label), - "attrs" => Attributes::flatatt($attrs), + "type" => static::INPUT_TYPE, + "name" => $this->buildName($name), + "value" => $value, + "label" => $label, + "attrs" => $attrs, + "template" => static::TEMPLATE_CHOICE, ); } + /** + * Format value to be rendered in html. + * + * @param mixed $value Value to be formated. + * + * @return array + */ protected function formatValue($value) { if (is_array($value)) { diff --git a/src/Widgets/DateInput.php b/src/Widgets/DateInput.php index 2e7eb39..be81450 100644 --- a/src/Widgets/DateInput.php +++ b/src/Widgets/DateInput.php @@ -6,8 +6,5 @@ class DateInput extends TemporalInput { - /** - * @var string Format of temporal value. - */ const FORMAT = 'd-m-Y'; } diff --git a/src/Widgets/EmailInput.php b/src/Widgets/EmailInput.php index 7170af0..3e1e6b3 100644 --- a/src/Widgets/EmailInput.php +++ b/src/Widgets/EmailInput.php @@ -6,5 +6,6 @@ class EmailInput extends Input { - protected $input_type = 'email'; + const TEMPLATE = 'email.html'; + const INPUT_TYPE = 'email'; } diff --git a/src/Widgets/FileInput.php b/src/Widgets/FileInput.php index a7f6f37..8ed0ec9 100644 --- a/src/Widgets/FileInput.php +++ b/src/Widgets/FileInput.php @@ -6,7 +6,8 @@ class FileInput extends Input { - protected $input_type = 'file'; + const TEMPLATE = 'file.html'; + const INPUT_TYPE = 'file'; /** * FileInput don't render the value. diff --git a/src/Widgets/HiddenInput.php b/src/Widgets/HiddenInput.php index b06c857..c5811fb 100644 --- a/src/Widgets/HiddenInput.php +++ b/src/Widgets/HiddenInput.php @@ -6,5 +6,6 @@ class HiddenInput extends Input { - protected $input_type = 'hidden'; + const TEMPLATE = 'hidden.html'; + const INPUT_TYPE = 'hidden'; } diff --git a/src/Widgets/Input.php b/src/Widgets/Input.php index 4454d72..3f4add3 100644 --- a/src/Widgets/Input.php +++ b/src/Widgets/Input.php @@ -4,24 +4,10 @@ */ namespace PHPForm\Widgets; -use PHPForm\PHPFormConfig; - abstract class Input extends Widget { - /** - * @var string The input type to use for the widget. - */ - protected $input_type = null; - - /** - * The constructor. - */ - public function __construct(array $attrs = null) - { - $this->template = PHPFormConfig::getITemplate("INPUT"); - - parent::__construct($attrs); - } + const TEMPLATE = 'input.html'; + const INPUT_TYPE = null; /** * Prepare context to be used on render method. @@ -32,10 +18,12 @@ public function __construct(array $attrs = null) * * @return array */ - public function getContext(string $name, $value, array $attrs = null) + public function getContext(string $name, $value, string $label = null, array $attrs = null) { - $context = parent::getContext($name, $value, $attrs); - $context["type"] = $this->input_type; + $context = parent::getContext($name, $value, $label, $attrs); + + $context["type"] = static::INPUT_TYPE; + return $context; } } diff --git a/src/Widgets/NumberInput.php b/src/Widgets/NumberInput.php index 471f90d..3a57046 100644 --- a/src/Widgets/NumberInput.php +++ b/src/Widgets/NumberInput.php @@ -6,5 +6,6 @@ class NumberInput extends Input { - protected $input_type = 'number'; + const TEMPLATE = 'number.html'; + const INPUT_TYPE = 'number'; } diff --git a/src/Widgets/PasswordInput.php b/src/Widgets/PasswordInput.php index a344cc1..831a34b 100644 --- a/src/Widgets/PasswordInput.php +++ b/src/Widgets/PasswordInput.php @@ -6,7 +6,8 @@ class PasswordInput extends Input { - protected $input_type = 'password'; + const TEMPLATE = 'password.html'; + const INPUT_TYPE = 'password'; /** * PasswordInput don't render the value. diff --git a/src/Widgets/RadioSelect.php b/src/Widgets/RadioSelect.php index 74575db..dd28e60 100644 --- a/src/Widgets/RadioSelect.php +++ b/src/Widgets/RadioSelect.php @@ -8,21 +8,10 @@ class RadioSelect extends ChoiceWidget { - protected $option_inherits_attrs = true; - protected $selected_attribute = "checked"; - protected $input_type = "radio"; - - /** - * The constructor. - */ - public function __construct(array $choices = array(), array $attrs = null) - { - $this->template = PHPFormConfig::getITemplate("RADIOSELECT"); - $this->template_choice = sprintf( - PHPFormConfig::getITemplate("RADIOSELECT_ITEM"), - PHPFormConfig::getITemplate("INPUT") - ); + const TEMPLATE = 'radio_select.html'; + const TEMPLATE_CHOICE = 'radio_select_option.html'; + const INPUT_TYPE = 'radio'; - parent::__construct($choices, $attrs); - } + protected $option_inherits_attrs = true; + protected $selected_attribute = 'checked'; } diff --git a/src/Widgets/Select.php b/src/Widgets/Select.php index 728a8f3..93dc67a 100644 --- a/src/Widgets/Select.php +++ b/src/Widgets/Select.php @@ -8,29 +8,19 @@ class Select extends ChoiceWidget { - protected $option_inherits_attrs = false; - - /** - * The constructor. - */ - public function __construct(array $choices = array(), array $attrs = null) - { - $this->template = PHPFormConfig::getITemplate("SELECT"); - $this->template_choice = PHPFormConfig::getITemplate("SELECT_ITEM"); + const TEMPLATE = 'select.html'; + const TEMPLATE_CHOICE = 'select_option.html'; - parent::__construct($choices, $attrs); - } + protected $option_inherits_attrs = false; - public function getContext(string $name, $value, array $attrs = null) + public function getContext(string $name, $value, string $label = null, array $attrs = null) { - if (is_null($attrs)) { - $attrs = array(); - } + $context = parent::getContext($name, $value, $label, $attrs); if ($this->allow_multiple_selected) { - $attrs["multiple"] = "multiple"; + $context["attrs"]["multiple"] = "multiple"; } - return parent::getContext($name, $value, $attrs); + return $context; } } diff --git a/src/Widgets/TextInput.php b/src/Widgets/TextInput.php index 71d28d5..5d8f1a6 100644 --- a/src/Widgets/TextInput.php +++ b/src/Widgets/TextInput.php @@ -6,5 +6,6 @@ class TextInput extends Input { - protected $input_type = 'text'; + const TEMPLATE = 'text.html'; + const INPUT_TYPE = 'text'; } diff --git a/src/Widgets/Textarea.php b/src/Widgets/Textarea.php index 41e4dc8..861a23d 100644 --- a/src/Widgets/Textarea.php +++ b/src/Widgets/Textarea.php @@ -4,10 +4,11 @@ */ namespace PHPForm\Widgets; -use PHPForm\PHPFormConfig; - class Textarea extends Widget { + const TEMPLATE = 'textarea.html'; + const INPUT_TYPE = 'textarea'; + /** * The constructor. */ @@ -19,8 +20,6 @@ public function __construct(array $attrs = null) $extra_attrs = array_merge($extra_attrs, $attrs); } - $this->template = PHPFormConfig::getITemplate("TEXTAREA"); - parent::__construct($extra_attrs); } } diff --git a/src/Widgets/URLInput.php b/src/Widgets/URLInput.php index a4c2bcd..2cd6c6a 100644 --- a/src/Widgets/URLInput.php +++ b/src/Widgets/URLInput.php @@ -6,5 +6,6 @@ class URLInput extends Input { - protected $input_type = 'url'; + const TEMPLATE = 'url.html'; + const INPUT_TYPE = 'url'; } diff --git a/src/Widgets/Widget.php b/src/Widgets/Widget.php index 81e78a5..6c52b4a 100644 --- a/src/Widgets/Widget.php +++ b/src/Widgets/Widget.php @@ -4,15 +4,11 @@ */ namespace PHPForm\Widgets; -use Fleshgrinder\Core\Formatter; - -use PHPForm\Utils\Attributes; +use PHPForm\Config; abstract class Widget { - const AUTO_ID_TEMPLATE = "id_{name}[_{index}?]"; - - protected $template = ""; + const TEMPLATE = ""; /** * @var array Attributes to be added to the widget. @@ -39,11 +35,13 @@ public function __construct(array $attrs = null) * * @return string */ - public function render(string $name, $value, array $attrs = null) + public function render(string $name, $value, string $label = null, array $attrs = null) { - $context = $this->getContext($name, $value, $attrs); + $renderer = Config::getInstance()->getRenderer(); + + $context = $this->getContext($name, $value, $label, $attrs); - return Formatter::format($this->template, $context); + return $renderer->render(static::TEMPLATE, $context); } /** @@ -55,7 +53,7 @@ public function render(string $name, $value, array $attrs = null) * * @return array */ - protected function getContext(string $name, $value, array $attrs = null) + protected function getContext(string $name, $value, string $label = null, array $attrs = null) { $value = $this->formatValue($value); $attrs = $this->buildAttrs($attrs); @@ -65,9 +63,11 @@ protected function getContext(string $name, $value, array $attrs = null) } return array( - "name" => htmlentities($name), - "attrs" => Attributes::flatatt($attrs), - "value" => is_string($value) ? htmlspecialchars($value) : $value, + "for" => $this->buildAutoId($name), + "name" => $name, + "attrs" => $attrs, + "value" => $value, + "label" => $label, ); } @@ -98,13 +98,13 @@ public function valueFromData($data, $files, string $name) } /** - * Return defined subwidget. + * Return defined options. * * @return array */ - public function getSubWidgets(string $name, $value, array $attrs = null) + public function getOptions(string $name, $value, array $attrs = null) { - return $this->widget; + return array(); } /** @@ -144,9 +144,7 @@ private function buildAttrs(array $extra_attrs = null) */ public function buildAutoId(string $name, int $index = null) { - return Formatter::format(self::AUTO_ID_TEMPLATE, array( - "name" => $name, - "index" => $index - )); + $auto_id = is_null($index) ? "id_%s" : "id_%s_%s"; + return sprintf($auto_id, $name, $index); } } diff --git a/src/templates.php b/src/templates.php deleted file mode 100644 index 75fc3b7..0000000 --- a/src/templates.php +++ /dev/null @@ -1,23 +0,0 @@ - '', - "LABEL_REQUIRED" => '*', - - // ErrorList - "ERRORLIST" => '