Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00016 final class PrimitivePolymorphicIdentifier extends PrimitiveIdentifier
00017 {
00018 const WRONG_CID_FORMAT = 201;
00019 const WRONG_CLASS = 202;
00020
00021 const DELIMITER = '.';
00022
00023 private $baseClassName = null;
00024
00025 public static function export($value)
00026 {
00027 if ($value === null)
00028 return null;
00029
00030 Assert::isInstance($value, 'Identifiable');
00031
00032 return get_class($value).self::DELIMITER.$value->getId();
00033 }
00034
00038 public function of($class)
00039 {
00040 throw new WrongStateException(
00041 'of() must not be called directly, use ofBase()'
00042 );
00043 }
00044
00049 public function ofBase($className)
00050 {
00051 Assert::classExists($className);
00052
00053 Assert::isInstance(
00054 $className,
00055 'DAOConnected',
00056 "class '{$className}' must implement DAOConnected interface"
00057 );
00058
00059 $this->baseClassName = $className;
00060
00061 return $this;
00062 }
00063
00064 public function getBaseClassName()
00065 {
00066 return $this->baseClassName;
00067 }
00068
00072 public function setValue($value)
00073 {
00074 Assert::isInstance($value, $this->baseClassName);
00075
00076 parent::of(get_class($value));
00077
00078 return parent::setValue($value);
00079 }
00080
00081 public function exportValue()
00082 {
00083 if ($this->value === null)
00084 return null;
00085
00086 return self::export($this->value);
00087 }
00088
00089 public function importValue($value)
00090 {
00091 return $this->import(
00092 array(
00093 $this->getName() => self::export($value)
00094 )
00095 );
00096 }
00097
00098 public function import($scope)
00099 {
00100 $savedRaw = null;
00101
00102 if (isset($scope[$this->name]) && $scope[$this->name]) {
00103 $savedRaw = $scope[$this->name];
00104
00105 $this->customError = null;
00106
00107 try {
00108
00109 list($class, $id) = explode(self::DELIMITER, $savedRaw, 2);
00110
00111 } catch (BaseException $e) {
00112
00113 $this->customError = self::WRONG_CID_FORMAT;
00114
00115 }
00116
00117 if (
00118 !$this->customError
00119 && !ClassUtils::isInstanceOf($class, $this->baseClassName)
00120 ) {
00121
00122 $this->customError = self::WRONG_CLASS;
00123
00124 }
00125
00126
00127 if (!$this->customError) {
00128 parent::of($class);
00129
00130 $scope[$this->name] = $id;
00131 }
00132
00133 } else {
00134
00135 parent::of($this->baseClassName);
00136 }
00137
00138 if (!$this->customError)
00139 $result = parent::import($scope);
00140 else {
00141 $this->value = null;
00142 $result = false;
00143 }
00144
00145 if ($savedRaw)
00146 $this->raw = $savedRaw;
00147
00148 return $result;
00149 }
00150 }
00151 ?>