# MappedForm.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2006-2008 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
    00015     final class MappedForm
    00016     {
    00017         private $form = null;
    00018         private $type = null;
    00019
    00020         private $map = array();
    00021
    00025         public static function create(Form $form)
    00026         {
    00027             return new self($form);
    00028         }
    00029
    00030         public function __construct(Form $form)
    00031         {
    00032             $this->form = $form;
    00033         }
    00034
    00038         public function getForm()
    00039         {
    00040             return $this->form;
    00041         }
    00042
    00046         public function setDefaultType(RequestType $type)
    00047         {
    00048             $this->type = $type;
    00049
    00050             return $this;
    00051         }
    00052
    00056         public function addSource($primitiveName, RequestType $type)
    00057         {
    00058             $this->checkExistence($primitiveName);
    00059
    00060             $this->map[$primitiveName][] = $type;
    00061
    00062             return $this;
    00063         }
    00064
    00068         public function importOne($name, HttpRequest $request)
    00069         {
    00070             $this->checkExistence($name);
    00071
    00072             $scopes = array();
    00073
    00074             if (isset($this->map[$name])) {
    00075                 foreach ($this->map[$name] as $type) {
    00076                     $scopes[] = $request->getByType($type);
    00077                 }
    00078             } elseif ($this->type) {
    00079                 $scopes[] = $request->getByType($this->type);
    00080             }
    00081
    00082             $first = true;
    00083             foreach ($scopes as $scope) {
    00084                 if ($first) {
    00085                     $this->form->importOne($name, $scope);
    00086                     $first = false;
    00087                 } else
    00088                     $this->form->importOneMore($name, $scope);
    00089             }
    00090
    00091             return $this;
    00092         }
    00093
    00097         public function import(HttpRequest $request)
    00098         {
    00099             foreach ($this->form->getPrimitiveNames() as $name) {
    00100                 $this->importOne($name, $request);
    00101             }
    00102
    00103             $this->form->checkRules();
    00104
    00105             return $this;
    00106         }
    00107
    00111         public function export(RequestType $type)
    00112         {
    00113             $result = array();
    00114
    00115             $default = ($this->type == $type);
    00116
    00117             foreach ($this->form->getPrimitiveList() as $name => $prm) {
    00118                 if (
    00119                     (
    00120                         isset($this->map[$name])
    00121                         && in_array($type, $this->map[$name])
    00122                     )
    00123                     || (
    00124                         !isset($this->map[$name])
    00125                         && $default
    00126                     )
    00127                 ) {
    00128                     if ($prm->getValue())
    00129                         $result[$name] = $prm->exportValue();
    00130                 }
    00131             }
    00132
    00133             return $result;
    00134         }
    00135
    00139         private function checkExistence($name)
    00140         {
    00141             if (!$this->form->primitiveExists($name))
    00142                 throw new MissingElementException(
    00143                     "there is no '{$name}' primitive"
    00144                 );
    00145
    00146             return $this;
    00147         }
    00148     }
    00149 ?>