PrimitiveForm.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007-2009 by Ivan Y. Khvostishkov                       *
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     class PrimitiveForm extends BasePrimitive
00016     {
00017         protected $proto = null;
00018         
00019         private $composite = false;
00020         
00027         public function of($className)
00028         {
00029             Assert::classExists($className);
00030             
00031             $protoClass = EntityProto::PROTO_CLASS_PREFIX.$className;
00032             
00033             Assert::classExists($protoClass);
00034             
00035             return $this->ofProto(Singleton::getInstance($protoClass));
00036         }
00037         
00042         public function ofProto(EntityProto $proto)
00043         {
00044             $this->proto = $proto;
00045             
00046             return $this;
00047         }
00048 
00049         public function ofAutoProto(AbstractProtoClass $proto)
00050         {
00051             $this->proto = $proto;
00052 
00053             return $this;
00054         }
00055         
00061         public function setComposite($composite = true)
00062         {
00063             $this->composite = ($composite == true);
00064             
00065             return $this;
00066         }
00067         
00068         public function isComposite()
00069         {
00070             return $this->composite;
00071         }
00072         
00073         public function getClassName()
00074         {
00075             return $this->proto->className();
00076         }
00077         
00078         public function getProto()
00079         {
00080             return $this->proto;
00081         }
00082         
00087         public function setValue($value)
00088         {
00089             Assert::isTrue($value instanceof Form);
00090             
00091             return parent::setValue($value);
00092         }
00093         
00098         public function importValue($value)
00099         {
00100             if ($value !== null)
00101                 Assert::isTrue($value instanceof Form);
00102             
00103             if (!$this->value || !$this->composite) {
00104                 $this->value = $value;
00105             } else {
00106                 throw new WrongStateException(
00107                     'composite objects should not be broken'
00108                 );
00109             }
00110             
00111             return ($value->getErrors() ? false : true);
00112         }
00113         
00114         public function exportValue()
00115         {
00116             if (!$this->value)
00117                 return null;
00118             
00119             return $this->value->export();
00120         }
00121         
00122         public function getInnerErrors()
00123         {
00124             if ($this->value)
00125                 return $this->value->getInnerErrors();
00126             
00127             return array();
00128         }
00129         
00130         public function import($scope)
00131         {
00132             return $this->actualImport($scope, true);
00133         }
00134         
00135         public function unfilteredImport($scope)
00136         {
00137             return $this->actualImport($scope, false);
00138         }
00139         
00140         private function actualImport($scope, $importFiltering)
00141         {
00142             if (!$this->proto)
00143                 throw new WrongStateException(
00144                     "no proto defined for PrimitiveForm '{$this->name}'"
00145                 );
00146             
00147             if (!isset($scope[$this->name]))
00148                 return null;
00149             
00150             $this->rawValue = $scope[$this->name];
00151             
00152             if (!$this->value || !$this->composite)
00153                 $this->value = $this->proto->makeForm();
00154             
00155             if (!$importFiltering) {
00156                 $this->value->
00157                     disableImportFiltering()->
00158                     import($this->rawValue)->
00159                     enableImportFiltering();
00160             } else {
00161                 $this->value->import($this->rawValue);
00162             }
00163             
00164             $this->imported = true;
00165             
00166             if ($this->value->getErrors())
00167                 return false;
00168             
00169             return true;
00170         }
00171     }
00172 ?>