MappedForm.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: MappedForm.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class MappedForm
00017     {
00018         private $form = null;
00019         private $type = null;
00020         
00021         private $map = array();
00022         
00026         public static function create(Form $form)
00027         {
00028             return new self($form);
00029         }
00030         
00031         public function __construct(Form $form)
00032         {
00033             $this->form = $form;
00034         }
00035         
00039         public function getForm()
00040         {
00041             return $this->form;
00042         }
00043         
00047         public function setDefaultType(RequestType $type)
00048         {
00049             $this->type = $type;
00050             
00051             return $this;
00052         }
00053         
00057         public function addSource($primitiveName, RequestType $type)
00058         {
00059             $this->checkExistence($primitiveName);
00060             
00061             $this->map[$primitiveName][] = $type;
00062             
00063             return $this;
00064         }
00065         
00066         /* void */ public function importOne($name, HttpRequest $request)
00067         {
00068             $this->checkExistence($name);
00069             
00070             $scopes = array();
00071             
00072             if (isset($this->map[$name])) {
00073                 foreach ($this->map[$name] as $type) {
00074                     $scopes[] = $request->getByType($type);
00075                 }
00076             } elseif ($this->type) {
00077                 $scopes[] = $request->getByType($this->type);
00078             }
00079             
00080             $first = true;
00081             foreach ($scopes as $scope) {
00082                 if ($first) {
00083                     $this->form->importOne($name, $scope);
00084                     $first = false;
00085                 } else
00086                     $this->form->importOneMore($name, $scope);
00087             }
00088         }
00089         
00090         /* void */ public function import(HttpRequest $request)
00091         {
00092             foreach ($this->form->getPrimitiveList() as $prm) {
00093                 $this->importOne($prm->getName(), $request);
00094             }
00095             
00096             $this->form->checkRules();
00097         }
00098         
00099         /* void */ private function checkExistence($name)
00100         {
00101             if (!$this->form->primitiveExists($name))
00102                 throw new MissingElementException(
00103                     "there is no '{$name}' primitive"
00104                 );
00105         }
00106     }
00107 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4