Commit b4de29c9 authored by Michael Herold's avatar Michael Herold

Renames form value "filters" to "transformations"

parent 13a3cdd3
v0.5.0
- Renames form value "filters" to "transformations"
v0.4.4
- Fixes bug in CheckPattern #2
v0.4.3
......
......@@ -7,9 +7,11 @@ use hemio\form\exception;
/**
*
*/
abstract class FormElement extends \hemio\form\Container
{
protected $name = '';
abstract class FormElement extends \hemio\form\Container {
use \hemio\form\Trait_\MaintainsTransformations;
protected $name = '';
protected $idSuffix = null;
/**
......@@ -25,8 +27,7 @@ abstract class FormElement extends \hemio\form\Container
* @return Abstract_\Form
* @throws exception\NotLazyEnough
*/
public function getForm()
{
public function getForm() {
if ($this->existsInheritableAppendage('_FORM'))
return $this->getInheritableAppendage('_FORM');
else {
......@@ -40,8 +41,7 @@ abstract class FormElement extends \hemio\form\Container
*
* @return string
*/
public function getName()
{
public function getName() {
return $this->name;
}
......@@ -50,12 +50,11 @@ abstract class FormElement extends \hemio\form\Container
* @param array $extraKeys
* @return string
*/
public function getHtmlName(array $extraKeys = [])
{
public function getHtmlName(array $extraKeys = []) {
return sprintf('%s_%s%s'
, $this->getForm()->getHtmlName()
, $this->getName()
, implode('_', $extraKeys)
, $this->getForm()->getHtmlName()
, $this->getName()
, implode('_', $extraKeys)
);
}
......@@ -64,18 +63,19 @@ abstract class FormElement extends \hemio\form\Container
* @param array $extraKeys
* @return string
*/
public function getHtmlId(array $extraKeys = [])
{
public function getHtmlId(array $extraKeys = []) {
return sprintf('%s_%s%s'
, $this->getForm()->getHtmlName()
, $this->getName()
, $this->idSuffix === null ? '' : '_'.$this->idSuffix
, implode('_', $extraKeys)
, $this->getForm()->getHtmlName()
, $this->getName()
, $this->idSuffix === null ? '' : '_' . $this->idSuffix
, implode('_', $extraKeys)
);
}
public function getValueUser()
{
return $this->getForm()->getValueUser($this->getHtmlName());
public function getValueUser() {
return $this->withTransformations(
$this->getForm()->getValueUser($this->getHtmlName())
);
}
}
......@@ -10,10 +10,9 @@ use hemio\html;
*
*
*/
abstract class FormFieldDefault extends FormField implements form_\Focusable
{
abstract class FormFieldDefault extends FormField implements form_\Focusable {
use form_\Trait_\MaintainsFilters;
/**
*
* @var string
......@@ -42,25 +41,36 @@ abstract class FormFieldDefault extends FormField implements form_\Focusable
*
* @param mixed $value
*/
public function setDefaultValue($value)
{
public function setDefaultValue($value) {
$this->defaultValue = $value;
}
/**
* @return mixed Default value
*/
public function getValueDefault()
{
return $this->getFiltered($this->defaultValue);
public function getValueDefault() {
return $this->withTransformations($this->defaultValue);
}
public function getValueStored() {
return $this->withTransformations(
$this->getForm()->getValueStored($this->getName()));
}
public function getValueToUse() {
if ($this->getValueUser() !== null)
return $this->getValueUser();
else if ($this->getValueStored() !== null)
return $this->getValueStored();
else
return $this->getValueDefault();
}
/**
*
* @return html\Input
*/
public function getControlElement()
{
public function getControlElement() {
return $this->control;
}
......@@ -70,42 +80,24 @@ abstract class FormFieldDefault extends FormField implements form_\Focusable
* @param string $title
* @param html\Interface_\Submittable $control
*/
public function init($name, $title, $control, $idSuffix = null)
{
$this->name = $name;
$this->title = $title;
$this->control = $control;
public function init($name, $title, $control, $idSuffix = null) {
$this->name = $name;
$this->title = $title;
$this->control = $control;
$this->idSuffix = $idSuffix;
if (strlen($title) > 0)
$this->setAccessKey(mb_substr($title, 0, 1));
}
public function getValueToUse()
{
if ($this->getValueUser() !== null)
return $this->getValueUser();
else if ($this->getValueStored() !== null)
return $this->getValueStored();
else
return $this->getValueDefault();
}
public function getValueStored()
{
return $this->getFiltered(
$this->getForm()->getValueStored($this->getName()));
}
/**
*
* @return TemplateFormField
* @throws exception\NotLazyEnough
* @throws exception\AppendageTypeError
*/
public function getFieldTemplateClone($special = null)
{
public function getFieldTemplateClone($special = null) {
$appendageName = form_\FormPost::FORM_FIELD_TEMPLATE.'_'.$special;
$appendageName = form_\FormPost::FORM_FIELD_TEMPLATE . '_' . $special;
if (!$this->existsInheritableAppendage($appendageName)) {
$appendageName = form_\FormPost::FORM_FIELD_TEMPLATE;
......@@ -118,20 +110,19 @@ abstract class FormFieldDefault extends FormField implements form_\Focusable
} elseif ($template === null) {
throw new exception\NotLazyEnough(
sprintf(
'There is no "%s" available for this Input', $appendageName
'There is no "%s" available for this Input', $appendageName
)
);
} else {
throw new exception\AppendageTypeError(
sprintf(
'Not an istance of TemplateFormFieldSingle "%s"', $appendageName
'Not an istance of TemplateFormFieldSingle "%s"', $appendageName
)
);
}
}
public function describe()
{
public function describe() {
return 'INPUT';
}
......@@ -139,8 +130,7 @@ abstract class FormFieldDefault extends FormField implements form_\Focusable
*
* @return string
*/
public function __toString()
{
public function __toString() {
if (!$this->filled)
$this->fill();
......@@ -149,27 +139,23 @@ abstract class FormFieldDefault extends FormField implements form_\Focusable
abstract public function fill();
public function setForm(Form $form)
{
public function setForm(Form $form) {
$this->control->setAttribute('form', $form->getHtmlName());
$this->addInheritableAppendage('_FORM', $form);
$form->addLogicalChild($this);
}
public function setAccessKey($key)
{
public function setAccessKey($key) {
$this->getControlElement()->setAttribute('accesskey', $key);
}
public function getHtmlTitle()
{
public function getHtmlTitle() {
$accessKey = $this->getControlElement()->getAttribute('accesskey');
if (strlen($accessKey) !== 1) {
return new html\Str($this->title);
} else {
$matches = [];
if (preg_match('/^(.*?)('.$accessKey.')(.*)$/iu', $this->title,
$matches)) {
if (preg_match('/^(.*?)(' . $accessKey . ')(.*)$/iu', $this->title, $matches)) {
$container = new form_\Container;
$u = new html\U();
......@@ -182,7 +168,7 @@ abstract class FormFieldDefault extends FormField implements form_\Focusable
return $container;
} else {
$container = new form_\Container;
$container->addChild(new html\Str($this->title.' ('));
$container->addChild(new html\Str($this->title . ' ('));
$u = new html\U();
$u->addChild(new html\Str($accessKey));
......@@ -195,27 +181,25 @@ abstract class FormFieldDefault extends FormField implements form_\Focusable
}
}
}
protected $autofocusLevel = 5;
/**
*
* @param bool $focus
*/
public function setAutofocus($focus = true, $level = 10)
{
public function setAutofocus($focus = true, $level = 10) {
if ($focus)
$this->autofocusLevel = $level;
else
$this->autofocusLevel = 0;
}
public function getAutofocusLevel()
{
public function getAutofocusLevel() {
return $this->autofocusLevel;
}
public function engageAutofocus()
{
public function engageAutofocus() {
$this->getControlElement()->setAttribute('autofocus', true);
}
......@@ -223,10 +207,10 @@ abstract class FormFieldDefault extends FormField implements form_\Focusable
*
* @param boolean $required
*/
public function setRequired($required = true)
{
public function setRequired($required = true) {
$this->addValidityCheck(new form_\CheckMinLength(1));
$this->required = $required;
$this->getControlElement()->setAttribute('required', (boolean) $required);
}
}
<?php
namespace hemio\form\Trait_;
trait MaintainsFilters {
/**
*
* @var array[callable]
*/
private $filters = [];
/**
*
*/
public function addFilter(callable $filter) {
$this->filters[] = $filter;
}
/**
*
*/
public function applyFilters(&$var) {
foreach ($this->filters as $filter) {
$var = $filter($var);
}
}
/**
*
*/
public function getFiltered($var) {
foreach ($this->filters as $filter) {
$var = $filter($var);
}
return $var;
}
}
<?php
namespace hemio\form\Trait_;
trait MaintainsTransformations {
/**
*
* @var array
*/
protected $valueTransformations = [];
/**
*
* @param mixed $id
* @param callable $transformation
*/
public function setValueTransformation($id, callable $transformation) {
$this->valueTransformations[$id] = $transformation;
}
/**
* Returns the given value with all transformations applied
*
* @param mixed $value
*/
public function withTransformations($value) {
// null for not provided remains untouched
if ($value === null)
return null;
foreach ($this->valueTransformations as $f)
$value = $f($value);
return $value;
}
}
<?php
/*
* Copyright (C) 2015 Michael Herold <quabla@hemio.de>
*
......@@ -23,37 +24,35 @@ namespace hemio\form;
*
* @author Michael Herold <quabla@hemio.de>
*/
class FieldTextTest extends \Helpers
{
class FieldTextTest extends \Helpers {
public function test1()
{
public function test1() {
$inputName = 'form_test_input1';
$post = [
$inputName => 'New value'
$inputName => 'New-X-value'
];
$stored = [
'input1' => 'DB value'
'input1' => 'DB-X-value'
];
$form = new FormPost('test', $post, null, $stored);
$input1 = new FieldText('input1', _('Title'));
$input1->setDefaultValue('Default value');
$input1->setDefaultValue('Default-X-value');
$input1->setValueTransformation('remove_strange_x', function ($value) {
return str_replace('-X-', ' ', $value);
});
$form->addChild($input1);
$this->assertEquals($inputName, $input1->getHtmlName());
$this->assertEquals('New value', $input1->getValueToUse(),
'Wrong valueToUse');
$this->assertEquals('DB value', $input1->getValueStored(),
'Wrong valueStored');
$this->assertEquals('New value', $input1->getValueUser(),
'Wrong valueUser');
$this->assertEquals('Default value', $input1->getValueDefault(),
'Wrong valueDefault');
$this->assertEquals('New value', $input1->getValueToUse(), 'Wrong valueToUse');
$this->assertEquals('DB value', $input1->getValueStored(), 'Wrong valueStored');
$this->assertEquals('New value', $input1->getValueUser(), 'Wrong valueUser');
$this->assertEquals('Default value', $input1->getValueDefault(), 'Wrong valueDefault');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment