OOPを使用する前に、フォーム処理を有効にするためのより基本的なオブジェクトをいくつか用意する必要があります。たとえば、エラーメッセージを含むPHPスーパーグローバル内のフォームフィールドのオブジェクト指向検証などです。
単なる模範:
// defining the interfaces and classes of the form values and validation, form field
// and fields
Interface FormValue
{
public function getValue();
}
interface FormValueValidator
{
/**
* @param FormValue $value
* @return bool
*/
public function validate(FormValue $value);
}
class NamedFormValue implements FormValue
{
private $name;
private $value;
public function __construct(array $data, $name, $default = null)
{
$this->name = $name;
$this->value = isset($data[$name]) ? $data[$name] : $default;
}
public function getValue()
{
return $this->value;
}
public function getName()
{
return $this->name;
}
}
class EmailValidator implements FormValueValidator
{
public function validate(FormValue $value)
{
$result = filter_var((string) $value->getValue(), FILTER_VALIDATE_EMAIL);
return is_string($result);
}
}
class FormFieldFactory
{
/**
* @param $name
* @param array $field
* @return FormField
*/
public function create($name, array $field)
{
$validatorClass = sprintf('%sValidator', @$field['validator']);
$validator = new $validatorClass;
$formField = new FormField($name);
$formField->setValidator($validator);
$formField->setErrorMessage($field['error_msg']);
return $formField;
}
}
class FormField
{
private $name;
/**
* @var FormValueValidator
*/
private $validator;
private $errorMessage;
/**
* @var FormValue
*/
private $value;
public function __construct($name)
{
$this->name = $name;
}
public function setValidator(FormValueValidator $validator)
{
$this->validator = $validator;
}
public function isValid()
{
return $this->validator->validate($this->value);
}
public function setErrorMessage($errorMessage)
{
$this->errorMessage = $errorMessage;
}
public function getErrorMessage() {
return $this->errorMessage;
}
public function setValue(FormValue $value)
{
$this->value = $value;
}
/**
* @return FormValue
*/
public function getValue()
{
return $this->value;
}
}
class FormFields extends IteratorIterator
{
private $fieldFactory;
private $invalidFields;
public function __construct(array $definitions, $fieldFactory)
{
parent::__construct(new ArrayIterator($definitions));
$this->fieldFactory = $fieldFactory;
}
public function current()
{
$name = $this->getInnerIterator()->key();
$definition = $this->getInnerIterator()->current();
return $this->fieldFactory->create($name, $definition);
}
public function valid()
{
return $this->getInnerIterator()->valid();
}
/**
* @param array $data
* @return bool true on success, false on validation error
*/
public function validateOn(array $data)
{
$this->invalidFields = array();
foreach($this as $name => $field) {
/* @var $field FormField */
$value = new NamedFormValue($data, $name);
$field->setValue($value);
$valid = $field->isValid();
$valid || ($this->invalidFields[$name] = $field);
}
return 0 === count($this->invalidFields);
}
/**
* @return FormField[]
*/
public function getInvalidFields()
{
return $this->invalidFields;
}
}
// defining the form in array notation:
$form = array(
'fields' => array(
'email' => array(
'validator' => 'Email',
'error_msg' => 'Cannot validate emailadress',
),
),
);
// processing the form validation
$messages = array();
$fields = new FormFields($form['fields'], new FormFieldFactory());
$fields->validateOn($_POST)
foreach ($fields->getInvalidFields() as $field) {
$messages[] = $field->getErrorMessage();
}
var_dump($messages);
例示的な出力:
array(1) {
[0]=>
string(27) "Cannot validate emailadress"
}