Files
majestic/form/Form.php

155 lines
3.3 KiB
PHP
Raw Normal View History

<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage form
* @since 2010-04-24
*/
abstract class Form
{
const SUCCESS = 'success';
2012-11-19 19:20:15 +04:00
const ERROR = 'error';
/**
* @var FormField[]
*/
protected $fields = array();
2012-11-19 19:20:15 +04:00
/**
* @var array
*/
protected $messages = array(
self::SUCCESS => 'Form data valid',
self::ERROR => 'Form data invalid');
protected $valid = true;
2012-11-19 19:20:15 +04:00
public function __construct()
{
$this->init();
}
2012-11-19 19:20:15 +04:00
/**
* @param string $name
2012-11-19 19:20:15 +04:00
* @param bool|string $message
* @return FormField
*/
protected function addField($name, $message = false)
{
$this->fields[$name] = new FormField($message);
return $this->fields[$name];
}
2012-11-19 19:20:15 +04:00
public function isValid($data)
{
if (!is_array($data)) {
2011-11-25 13:54:31 +04:00
throw new InitializationException(__CLASS__ . '::' . __METHOD__ . ' expects an array');
}
2012-11-19 19:20:15 +04:00
foreach ($this->fields as $field_name => $field) {
if (isset($data[$field_name])) {
$this->valid &= $field->isValid($data[$field_name], $data);
} else {
$this->valid &= $field->isValid(null, $data);
}
}
if (!$this->valid) {
$this->fillHelperData();
}
return $this->valid;
}
2012-11-19 19:20:15 +04:00
public function getMessages()
{
$messages = array();
foreach ($this->fields as $name => $field) {
if ($mess = $field->getMessage()) {
$messages[$name] = $mess;
}
}
return $messages;
}
2012-11-19 19:20:15 +04:00
public function getValue($key)
{
if (isset($this->fields[$key])) {
return $this->fields[$key]->getValue();
}
return false;
}
2012-11-19 19:20:15 +04:00
/**
* @return array
*/
public function getValues()
{
$values = array();
foreach ($this->fields as $key => $field) {
if (!$field->isIgnored()) {
$values[$key] = $field->getValue();
}
}
return $values;
}
2012-11-19 19:20:15 +04:00
/**
* @return array
*/
public function getSourceValues()
{
$values = array();
foreach ($this->fields as $key => $field) {
$values[$key] = $field->getSourceValue();
}
return $values;
}
2012-11-19 19:20:15 +04:00
/**
* @return string
*/
public function getMessageType()
{
return ($this->valid) ? self::SUCCESS : self::ERROR;
}
2012-11-19 19:20:15 +04:00
/**
* @return string
*/
public function getMessage()
{
return $this->messages[$this->getMessageType()];
}
2012-11-19 19:20:15 +04:00
/**
* @param string $message
* @return Form
*/
public function setSuccessMessage($message)
{
$this->messages[self::SUCCESS] = (string) $message;
return $this;
}
2012-11-19 19:20:15 +04:00
/**
* @param string $message
* @return Form
*/
public function setErrorMessage($message)
{
$this->messages[self::ERROR] = (string) $message;
return $this;
}
2012-11-19 19:20:15 +04:00
protected function fillHelperData()
{
$data['messages'] = $this->getMessages();
$data['values'] = $this->getSourceValues();
Session::set(get_class($this), $data);
}
2012-11-19 19:20:15 +04:00
abstract protected function init();
}