Skip to content

Commit

Permalink
Restructures FormFields etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Herold committed Jan 8, 2015
1 parent 3d0a386 commit 2162c6a
Show file tree
Hide file tree
Showing 35 changed files with 617 additions and 568 deletions.
28 changes: 17 additions & 11 deletions src/Abstract_/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ abstract class Form extends \hemio\html\Form {
* @var array
*/
private $post = [];
public $arrStoredValues = [];

/**
*
* @var array
*/
protected $storedValues = [];

public function __construct($name, array $post = null, array $get = null, array $stored = []) {
if ($post === null)
Expand All @@ -37,30 +42,31 @@ public function __construct($name, array $post = null, array $get = null, array
$this->post = $post;
$this->get = $get;
$this->name = $name;
$this->storedValues = $stored;

$this->setAttribute('name', $this->getHtmlName());
$this->setId($this->getHtmlName());
$this->addInheritableAppendage('_INPUT_SINGLE_TEMPLATE', new template\FormLineP);
$this->addInheritableAppendage(self::FORM_FIELD_TEMPLATE, new template\FormLineP);
$this->addInheritableAppendage('_FORM', $this);
}

/**
* @todo potentially completly useless in this functions, form elements should have this options?
* @return TemplateFormFieldSingle
* @return TemplateFormField
* @deprecated since version 1.0
*/
public function getLineTemplate() {
return $this->getInheritableAppendage(self::SINGLE_CONTROL_TEMPLATE);
return $this->getInheritableAppendage(self::FORM_FIELD_TEMPLATE);
}

/**
* @return TemplateFormFieldSingle
* @return TemplateFormField
*/
public function getSingleControlTemplate() {
return $this->getInheritableAppendage(self::SINGLE_CONTROL_TEMPLATE);
return $this->getInheritableAppendage(self::FORM_FIELD_TEMPLATE);
}
const SINGLE_CONTROL_TEMPLATE = '_INPUT_SINGLE_TEMPLATE';

const FORM_FIELD_TEMPLATE = '_INPUT_SINGLE_TEMPLATE';

/**
* Check for occured errors
Expand All @@ -69,7 +75,7 @@ public function getSingleControlTemplate() {
*/
public function dataValid() {
foreach (new \RecursiveIteratorIterator($this) as $child) {
if ($child instanceof Abstract_\FormField && !$child->dataValid()) {
if ($child instanceof Abstract_\FormElement && !$child->dataValid()) {
return false;
}
}
Expand Down Expand Up @@ -114,8 +120,8 @@ public function getPost($key) {
* @return mixed
*/
public function getValueStored($key) {
if (isset($this->arrStoredValues[$key])) {
return $this->arrStoredValues[$key];
if (isset($this->storedValues[$key])) {
return $this->storedValues[$key];
} else {
return null;
}
Expand Down
62 changes: 62 additions & 0 deletions src/Abstract_/FormElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace hemio\form\Abstract_;

use hemio\form\exception;

/**
*
*/
abstract class FormElement extends \hemio\form\Container {

protected $name = '';

/**
* Is active value in the form correct.
*
* @return boolean
*/
abstract public function dataValid();

/**
* Has the value changed with respective to the stored or default value.
* @return boolean
*/
abstract public function changed();

/**
* Get the form to which this element belongs
*
* @return Abstract_\Form
* @throws exception\NotLazyEnough
*/
public function getForm() {
if ($this->existsInheritableAppendage('_FORM'))
return $this->getInheritableAppendage('_FORM');
else {
#print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));

throw new exception\NotLazyEnough(
'No Form for FormElement found. Maybe not yet a child of a Form.');
}
}

/**
*
* @return string
*/
public function getName() {
return $this->name;
}

/**
*
* @param array $extraKeys
* @return string
*/
public function getHtmlName(array $extraKeys = []) {
return $this->getForm()->getHtmlName() .
'_' . $this->getName() . implode('_', $extraKeys);
}

}
36 changes: 9 additions & 27 deletions src/Abstract_/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,25 @@

namespace hemio\form\Abstract_;

use hemio\form\exception;

/**
*
* Field in a form that expects inputs maybe composed of several input elements.
*/
abstract class FormField extends \hemio\form\Container {

protected $name = '';
abstract class FormField extends FormElement {

/**
* Is active value in the form correct.
*
* @return boolean
* @todo not implemented
*/
abstract public function dataValid();

/**
* Has the value changed with respective to the stored or default value.
* @return boolean
*/
abstract public function changed();
public function changed() {
return false;
}

/**
* Get the form to which this element belongs
*
* @return Abstract_\Form
* @throws exception\NotLazyEnough
* @todo not implemented
*/
public function getForm() {
if ($this->existsInheritableAppendage('_FORM'))
return $this->getInheritableAppendage('_FORM');
else {
#print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));

throw new exception\NotLazyEnough(
'No Form for FormElement found. Maybe not yet a child of a Form.');
}
public function dataValid() {
return true;
}

}
143 changes: 143 additions & 0 deletions src/Abstract_/FormFieldDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace hemio\form\Abstract_;

use hemio\form as form_;
use hemio\form\exception;
use hemio\html;

/**
*
*
*/
abstract class FormFieldDefault extends FormField {

use form_\Trait_\MaintainsFilters;

/**
*
* @var string
*/
public $title = '';

/**
*
* @var boolean
*/
protected $filled = false;

/**
*
* @var html\Interface_\Submittable
*/
protected $control;

/**
*
* @var string
*/
protected $defaultValue = '';

/**
*
* @param mixed $value
*/
public function setDefaultValue($value) {
$this->defaultValue = $value;
}

/**
* @return mixed Default value
*/
public function getValueDefault() {
return $this->getFiltered($this->defaultValue);
}

/**
*
* @param string $name
* @param string $title
* @param html\Interface_\Submittable $control
*/
public function init($name, $title, $control) {
$this->name = $name;
$this->title = $title;
$this->control = $control;
}

public function getValueToUse() {
if ($this->getValueUser() !== null)
return $this->getValueUser();
else if ($this->getValueStored() !== null)
return $this->getValueStored();
else
return $this->getValueDefault();
}

public function getValueUser() {
$value = $this->getForm()->getValueUser($this->getHtmlName());
if ($value === null)
return null;
else
return $this->getFiltered($value);
}

public function getValueStored() {
return $this->getFiltered(
$this->getForm()->getValueStored($this->getName()));
}

public function changed() {
return $this->getValueStored() != $this->getValueUser();
}

/**
*
* @return TemplateFormField
* @throws exception\NotLazyEnough
* @throws exception\AppendageTypeError
*/
public function getFieldTemplateClone($special = null) {

$appendageName = form_\FormPost::FORM_FIELD_TEMPLATE . '_' . $special;

if (!$this->existsInheritableAppendage($appendageName)) {
$appendageName = form_\FormPost::FORM_FIELD_TEMPLATE;
}

$template = $this->getInheritableAppendage($appendageName);

if ($template instanceof TemplateFormField) {
return clone $template;
} elseif ($template === null) {
throw new exception\NotLazyEnough(
sprintf(
'There is no "%s" available for this Input', $appendageName
)
);
} else {
throw new exception\AppendageTypeError(
sprintf(
'Not an istance of TemplateFormFieldSingle "%s"', $appendageName
)
);
}
}

public function describe() {
return 'INPUT';
}

/**
*
* @return string
*/
public function __toString() {
if (!$this->filled)
$this->fill();

return parent::__toString();
}

abstract public function fill();
}
36 changes: 0 additions & 36 deletions src/Abstract_/FormFieldIn.php

This file was deleted.

Loading

0 comments on commit 2162c6a

Please sign in to comment.