Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ trim_trailing_whitespace = true
[*.json]
indent_size = 2
insert_final_newline = ignore

[*.html]
insert_final_newline = ignore
29 changes: 27 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -38,5 +39,9 @@
"phpcs --standard=phpcs.xml",
"phpunit -c phpunit.xml"
]
},
"config": {
"sort-packages": true,
"optimize-autoloader": true
}
}
2 changes: 0 additions & 2 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
class PHPFormConfig
{
const MESSAGES_FILE_PATH = 'src/messages.php';
const TEMPLATES_FILE_PATH = 'src/templates.php';

private static $config = null;

Expand All @@ -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()
Expand Down
75 changes: 75 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
namespace PHPForm;

use PHPForm\Renderers\TwigRenderer;

class Config extends Singleton
{
/**
* @var string Directory with templates
*/
protected $templates_dir = __DIR__ . '/templates/';

/**
* @var string Default fallback template pack
*/
protected $fallback_template_pack = "default";

/**
* @var string Default template pack defined. If null, fallback is used.
*/
protected $template_pack = null;

/**
* @var string Renderer class used to render html content
*/
protected $renderer_class = TwigRenderer::class;

/**
* @var PHPForm\Renderers\Renderer Renderer instance based on $renderer_class.
*/
private $renderer;

/**
* Templates path accordingly with defined template pack.
*
* @return string
*/
private function buildPath($template_pack)
{
$path = $this->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;
}
}
29 changes: 7 additions & 22 deletions src/Errors/ErrorList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
}
}
39 changes: 16 additions & 23 deletions src/Fields/BoundField.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?php
namespace PHPForm\Fields;

use Fleshgrinder\Core\Formatter;

use PHPForm\PHPFormConfig;
use PHPForm\Utils\Attributes;
use PHPForm\Config;

class BoundField
{
const TEMPLATE_LABEL = "label.html";

private $form;
private $field;
private $name;
private $subwidgets_cache;
private $options_cache;

public $html_name;
public $help_text;
Expand Down Expand Up @@ -55,22 +54,21 @@ public function __get($name)
return $this->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())
Expand All @@ -79,31 +77,26 @@ 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;

if (empty($contents)) {
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()
));
}

Expand Down
2 changes: 0 additions & 2 deletions src/Fields/MultipleChoiceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/
namespace PHPForm\Fields;

use Fleshgrinder\Core\Formatter;

use PHPForm\Exceptions\ValidationError;
use PHPForm\PHPFormConfig;
use PHPForm\Widgets\SelectMultiple;
Expand Down
7 changes: 2 additions & 5 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__';

/**
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/Renderers/Renderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Abstract class for renderers definition
*/
namespace PHPForm\Renderers;

interface Renderer
{
public function __construct(string $fallback_templates_dir, string $templates_dir);
public function getTemplate(string $template_name);
public function render(string $template_name, array $context);
}
38 changes: 38 additions & 0 deletions src/Renderers/TwigRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
*
*/
namespace PHPForm\Renderers;

use Twig_Loader_Filesystem;
use Twig_Environment;
use Twig_Loader_Chain;

class TwigRenderer implements Renderer
{
private $twig;

public function __construct(string $fallback_templates_dir, string $templates_dir = null)
{
$loaders = new Twig_Loader_Chain();

if (!is_null($templates_dir)) {
$loaders->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);
}
}
Loading