00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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 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 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 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 ?>