r454 - in src/classes: Form Template

From: <voxusAT@ATshadanakar.org>
Date: Thu, 9 Jun 2005 19:59:10 +0400 (MSD)

Author: voxus
Date: 2005-06-09 19:59:09 +0400 (Thu, 09 Jun 2005)
New Revision: 454

Added:
   src/classes/Form/Filter.class.php
   src/classes/Form/FiltrablePrimitive.class.php
   src/classes/Form/PlainForm.class.php
Modified:
   src/classes/Form/BasePrimitive.class.php
   src/classes/Form/Form.class.php
   src/classes/Form/Primitive.class.php
   src/classes/Template/PropertyFilterForm.class.php
Log:
* major form refactoring
! API changed
+ filtrable primitives (integer and string atm)
? more on ML

Modified: src/classes/Form/BasePrimitive.class.php
===================================================================
--- src/classes/Form/BasePrimitive.class.php 2005-06-09 12:44:31 UTC (rev 453)
+++ src/classes/Form/BasePrimitive.class.php 2005-06-09 15:59:09 UTC (rev 454)
@@ -20,10 +20,10 @@
                 protected $required = false;
 
                 /**
- * @var mixed not filtered value received from importing scope
+ * @var mixed raw copy of importing scope's value
                  * @access protected
                 **/
- protected $rawValue = null;
+ protected $raw = null;
 
                 public function __construct($name)
                 {
@@ -61,23 +61,17 @@
                 
                 public function getRawValue()
                 {
- return $this->rawValue;
+ return $this->raw;
                 }
                 
- public function getSafeRawValue()
- {
- return
- null === $this->rawValue // be concrete
- ? $this->default
- : $this->rawValue;
- }
-
                 public function getActualValue()
                 {
- return
- null === $this->value // be concrete
- ? $this->default
- : $this->value;
+ if (null !== $this->value)
+ return $this->value;
+ elseif (null !== $this->raw)
+ return $this->raw;
+
+ return $this->default;
                 }
                 
                 public function setValue($value)
@@ -87,9 +81,9 @@
                         return $this;
                 }
                 
- public function setRawValue($rawValue)
+ public function setRawValue($raw)
                 {
- $this->rawValue = $rawValue;
+ $this->raw = $raw;
                         
                         return $this;
                 }
@@ -105,6 +99,20 @@
                         
                         return $this;
                 }
+
+ public function required()
+ {
+ $this->required = true;
+
+ return $this;
+ }
+
+ public function optional()
+ {
+ $this->required = false;
+
+ return $this;
+ }
 
                 protected function import(&$scope)
                 {
@@ -112,7 +120,7 @@
                                 isset($scope[$this->name]) &&
                                 $scope[$this->name] !== ''
                         ) {
- $this->rawValue = $scope[$this->name];
+ $this->raw = $scope[$this->name];
                                 
                                 return true;
                         }
@@ -120,4 +128,4 @@
                         return null;
                 }
         }
-?>
+?>
\ No newline at end of file

Added: src/classes/Form/Filter.class.php
===================================================================
--- src/classes/Form/Filter.class.php 2005-06-09 12:44:31 UTC (rev 453)
+++ src/classes/Form/Filter.class.php 2005-06-09 15:59:09 UTC (rev 454)
@@ -0,0 +1,34 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Konstantin V. Arkhipov *
+ * voxusAT@ATgentoo.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ class Filter /* Factory */
+ {
+ public static function text()
+ {
+ return Singletone::getInstance()->TextFilter();
+ }
+ }
+
+ abstract class BaseFilter extends Singletone
+ {
+ abstract public function filter($value);
+ }
+
+ class TextFilter extends BaseFilter
+ {
+ public function filter($value)
+ {
+ return htmlspecialchars(strip_tags($value));
+ }
+ }
+?>
\ No newline at end of file

Property changes on: src/classes/Form/Filter.class.php
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: src/classes/Form/FiltrablePrimitive.class.php
===================================================================
--- src/classes/Form/FiltrablePrimitive.class.php 2005-06-09 12:44:31 UTC (rev 453)
+++ src/classes/Form/FiltrablePrimitive.class.php 2005-06-09 15:59:09 UTC (rev 454)
@@ -0,0 +1,33 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Konstantin V. Arkhipov *
+ * voxusAT@ATgentoo.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ abstract class FiltrablePrimitive extends RangedPrimitive
+ {
+ private $filter = null;
+
+ public function setFilter(BaseFilter $filter)
+ {
+ $this->filter = $filter;
+
+ return $this;
+ }
+
+ protected function selfFilter()
+ {
+ if ($this->filter)
+ $this->value = $this->filter->filter($this->value);
+
+ return $this;
+ }
+ }
+?>
\ No newline at end of file

Property changes on: src/classes/Form/FiltrablePrimitive.class.php
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: src/classes/Form/Form.class.php
===================================================================
--- src/classes/Form/Form.class.php 2005-06-09 12:44:31 UTC (rev 453)
+++ src/classes/Form/Form.class.php 2005-06-09 15:59:09 UTC (rev 454)
@@ -11,14 +11,12 @@
  ***************************************************************************/
 /* $Id$ */
 
- class Form
+ class Form extends PlainForm
         {
                 const WRONG = 0x0001;
                 const MISSING = 0x0002;
                 
                 private $errors = array();
- private $primitives = array();
- private $aliases = array();
                 private $labels = array();
                 
                 public static function create()
@@ -26,77 +24,51 @@
                         return new Form();
                 }
                 
- public function addAlias($primitiveName, $alias)
+ public function &getErrors()
                 {
- if (!$this->primitives[$primitiveName])
- throw new WrongArgumentException("{$primitiveName} does not exist");
+ return $this->errors;
+ }
 
- $this->aliases[$alias] = $primitiveName;
+ public function dropAllErrors()
+ {
+ $this->errors = array();
                         
                         return $this;
                 }
-
- public function getByAlias($alias)
- {
- if (!isset($this->aliases[$alias]))
- return null;
 
- return $this->get($this->aliases[$alias]);
- }
-
- public function getAnyValue($name)
- {
- if (!($prm = $this->get($name)) && !($prm = $this->getByAlias($name)))
- throw new WrongArgumentException(
- "there is no such primitive with name or alias '{$name}'");
-
- return $prm->getActualValue();
- }
-
- public function primitiveExist($name)
- {
- return
- (
- isset($this->primitives[$name]) ||
- isset($this->aliases[$name])
- );
- }
-
+ /**
+ * primitive marking
+ **/
                 public function markMissing($primitiveName)
                 {
- if (!$this->primitiveExist($primitiveName))
- throw new WrongArgumentException("knows nothing about '{$primitiveName}'");
+ if (!$prm = $this->get($primitiveName))
+ throw new ObjectNotFoundException("knows nothing about '{$primitiveName}'");
 
- $this->errors[$primitiveName] = Form::MISSING;
+ $this->errors[$prm->getName()] = Form::MISSING;
                         
                         return $this;
                 }
                 
                 public function markWrong($primitiveName)
                 {
- if (!$this->primitiveExist($primitiveName))
- throw new WrongArgumentException("knows nothing about '{$primitiveName}'");
+ if (!$prm = $this->get($primitiveName))
+ throw new ObjectNotFoundException("knows nothing about '{$primitiveName}'");
 
- $this->errors[$primitiveName] = Form::WRONG;
+ $this->errors[$prm->getName()] = Form::WRONG;
                         
                         return $this;
                 }
 
                 public function markGood($primitiveName)
                 {
- if (!$this->primitiveExist($primitiveName))
- throw new WrongArgumentException("knows nothing about '{$primitiveName}'");
+ if (!$prm = $this->get($primitiveName))
+ throw new ObjectNotFoundException("knows nothing about '{$primitiveName}'");
 
- unset($this->errors[$primitiveName]);
+ unset($this->errors[$prm->getName()]);
                         
                         return $this;
                 }
 
- public function &getErrors()
- {
- return $this->errors;
- }
-
                 /**
                  * Returns plain list of error's labels
                 **/
@@ -131,112 +103,14 @@
                 **/
                 private function addErrorLabel($primitiveName, $errorType, $label)
                 {
- if (!$this->primitiveExist($primitiveName))
- throw new WrongArgumentException("knows nothing about '{$primitiveName}'");
+ if (!$prm = $this->get($primitiveName))
+ throw new ObjectNotFoundException("knows nothing about '{$primitiveName}'");
                         
- $this->labels[$primitiveName][$errorType] = $label;
+ $this->labels[$prm->getName()][$errorType] = $label;
 
                         return $this;
                 }
                 
- public function add(BasePrimitive $prm)
- {
- // TODO: check duplicate primitives
- $this->primitives[$prm->getName()] = $prm;
-
- return $this;
- }
-
- public function get($name)
- {
- if (isset($this->primitives[$name]))
- return $this->primitives[$name];
- else
- return null;
- }
-
- public function getValue($name)
- {
- if (!($prm = $this->get($name)))
- throw new WrongArgumentException("{$name} doesn't exist in form");
-
- return $prm->getValue();
- }
-
- public function getRawValue($name)
- {
- if (!$this->get($name))
- throw new WrongArgumentException("{$name} doesn't exist in form");
-
- return $this->get($name)->getRawValue();
- }
-
- public function getActualValue($name)
- {
- if (!$this->get($name))
- throw new WrongArgumentException("{$name} doesn't exist in form");
-
- return $this->get($name)->getActualValue();
- }
-
- public function getSafeRawValue($name)
- {
- if (!$this->get($name))
- throw new WrongArgumentException("{$name} doesn't exist in form");
-
- return $this->get($name)->getSafeRawValue();
- }
-
- public function getChoiceValue($name)
- {
- $list = &$this->primitives[$name]->getList();
- $value = &$this->primitives[$name]->getValue();
-
- if ($value !== null)
- return $list[$value]; // TODO: handle multilist (value is array)
- else
- return null;
- }
-
- public function getRangeMax($name)
- {
- if ($this->primitives[$name]->getValue() instanceof Range)
- return $this->primitives[$name]->getValue()->getMax();
-
- return null;
- }
-
- public function getRangeMin($name)
- {
- if ($this->primitives[$name]->getValue() instanceof Range)
- return $this->primitives[$name]->getValue()->getMin();
-
- return null;
- }
-
- public function getActualRangeMax($name)
- {
- if ($this->primitives[$name]->getActualValue() instanceof Range)
- return $this->primitives[$name]->getActualValue()->getMax();
-
- return null;
- }
-
- public function getActualRangeMin($name)
- {
- if ($this->primitives[$name]->getActualValue() instanceof Range)
- return $this->primitives[$name]->getActualValue()->getMin();
-
- return null;
- }
-
- public function dropAllErrors()
- {
- $this->errors = array();
-
- return $this;
- }
-
                 public function import(&$scope)
                 {
                         foreach ($this->primitives as $name => $prm)
@@ -260,9 +134,6 @@
                 
                 public function importOne($primitiveName, &$scope)
                 {
- if (!$this->primitiveExist($primitiveName))
- throw new WrongArgumentException("{$name} doesn't exist in form");
-
                         $this->importPrimitive($scope, $this->get($primitiveName));
                         
                         return $this;
@@ -283,16 +154,5 @@
                         else
                                 $this->errors[$name] = self::WRONG;
                 }
-
- /**
- * Returns list of primitive names
- *
- * @access public
- * @return array
- **/
- public function getPrimitives()
- {
- return array_keys($this->primitives);
- }
         }
-?>
+?>
\ No newline at end of file

Added: src/classes/Form/PlainForm.class.php
===================================================================
--- src/classes/Form/PlainForm.class.php 2005-06-09 12:44:31 UTC (rev 453)
+++ src/classes/Form/PlainForm.class.php 2005-06-09 15:59:09 UTC (rev 454)
@@ -0,0 +1,135 @@
+<?php
+/***************************************************************************
+ * Copyright (C) 2005 by Konstantin V. Arkhipov, Anton Lebedevich *
+ * voxusAT@ATgentoo.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+/* $Id$ */
+
+ abstract class PlainForm
+ {
+ protected $aliases = array();
+ protected $primitives = array();
+
+ public function addAlias($primitiveName, $alias)
+ {
+ if (!isset($this->primitives[$primitiveName]))
+ throw new ObjectNotFoundException("{$primitiveName} does not exist");
+
+ $this->aliases[$alias] = $primitiveName;
+
+ return $this;
+ }
+
+ public function primitiveExist($name)
+ {
+ return
+ (
+ isset($this->primitives[$name]) ||
+ isset($this->aliases[$name])
+ );
+ }
+
+ public function add(BasePrimitive $prm, $alias = null)
+ {
+ $name = &$prm->getName();
+
+ if (isset($this->primitives[$name]))
+ throw new DuplicateObjectException("i'm already exists!");
+
+ $this->primitives[$name] = $prm;
+
+ if ($alias)
+ $this->addAlias($name, $alias);
+
+ return $this;
+ }
+
+ public function get($name)
+ {
+ if (isset($this->aliases[$name], $this->primitives[$this->aliases[$name]]))
+ return $this->primitives[$this->aliases[$name]];
+ elseif (isset($this->primitives[$name]))
+ return $this->primitives[$name];
+
+ throw new ObjectNotFoundException("knows nothing about '{$name}'");
+ }
+
+ public function getValue($name)
+ {
+ return $this->get($name)->getValue();
+ }
+
+ public function getRawValue($name)
+ {
+ return $this->get($name)->getRawValue();
+ }
+
+ public function getActualValue($name)
+ {
+ return $this->get($name)->getActualValue();
+ }
+
+ public function getChoiceValue($name)
+ {
+ $prm = &$this->get($name);
+ $list = &$prm->getList();
+ $value = &$prm->getValue();
+
+ if ($value !== null)
+ return $list[$value]; // TODO: handle multilist (value is array)
+
+ return null;
+ }
+
+ public function getRangeMax($name)
+ {
+ $range = &$this->get($name)->getValue();
+
+ return
+ $range instanceof Range
+ ? $range->getMax()
+ : null;
+ }
+
+ public function getRangeMin($name)
+ {
+ $range = &$this->get($name)->getValue();
+
+ return
+ $range instanceof Range
+ ? $range->getMin()
+ : null;
+ }
+
+ public function getActualRangeMax($name)
+ {
+ $range = &$this->get($name)->getActualValue();
+
+ return
+ $range instanceof Range
+ ? $range->getMax()
+ : null;
+ }
+
+ public function getActualRangeMin($name)
+ {
+ $range = &$this->get($name)->getActualValue();
+
+ return
+ $range instanceof Range
+ ? $range->getMin()
+ : null;
+ }
+
+ public function getPrimitiveNames()
+ {
+ return array_keys($this->primitives);
+ }
+ }
+?>
\ No newline at end of file

Property changes on: src/classes/Form/PlainForm.class.php
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: src/classes/Form/Primitive.class.php
===================================================================
--- src/classes/Form/Primitive.class.php 2005-06-09 12:44:31 UTC (rev 453)
+++ src/classes/Form/Primitive.class.php 2005-06-09 15:59:09 UTC (rev 454)
@@ -11,7 +11,7 @@
  ***************************************************************************/
 /* $Id$ */
 
- class Primitive
+ class Primitive /* Factory */
         {
                 public static function integer($name)
                 {
@@ -84,7 +84,7 @@
                 }
         }
 
- class PrimitiveString extends RangedPrimitive
+ class PrimitiveString extends FiltrablePrimitive
         {
                 public function import(&$scope)
                 {
@@ -95,6 +95,8 @@
                                 !($this->max && strlen($scope[$this->name]) > $this->max)
                         ) {
                                 $this->value = (string) $scope[$this->name];
+
+ $this->selfFilter();
 
                                 return true;
                         }
@@ -103,7 +105,7 @@
                 }
         }
         
- class PrimitiveInteger extends RangedPrimitive
+ class PrimitiveInteger extends FiltrablePrimitive
         {
                 public function import(&$scope)
                 {
@@ -115,6 +117,8 @@
                                 !(null !== $this->max && $scope[$this->name] > $this->max)
                         ) {
                                 $this->value = (int) $scope[$this->name];
+
+ $this->selfFilter();
                                 
                                 return true;
                         }

Modified: src/classes/Template/PropertyFilterForm.class.php
===================================================================
--- src/classes/Template/PropertyFilterForm.class.php 2005-06-09 12:44:31 UTC (rev 453)
+++ src/classes/Template/PropertyFilterForm.class.php 2005-06-09 15:59:09 UTC (rev 454)
@@ -96,8 +96,8 @@
                 {
                         if ($this->_form) {
                                 try {
- return $this->_form->getSafeRawValue($primitiveName);
- } catch (WrongArgumentException $e) {
+ return $this->_form->getRawValue($primitiveName);
+ } catch (ObjectNotFoundException $e) {
                                         return null;
                                 }
                         }
Received on Thu Jun 09 2005 - 19:59:10 MSD

This archive was generated by hypermail 2.2.0 : Sat Oct 27 2007 - 19:26:13 MSD