Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00015 class PrimitiveIdentifier extends IdentifiablePrimitive
00016 {
00017 private $methodName = 'getById';
00018
00023 public function of($class)
00024 {
00025 $className = $this->guessClassName($class);
00026
00027 Assert::classExists($className);
00028
00029 Assert::isInstance(
00030 $className,
00031 'DAOConnected',
00032 "class '{$className}' must implement DAOConnected interface"
00033 );
00034
00035 $this->className = $className;
00036
00037 return $this;
00038 }
00039
00043 public function dao()
00044 {
00045 Assert::isNotNull(
00046 $this->className,
00047 'specify class name first of all'
00048 );
00049
00050 return call_user_func(array($this->className, 'dao'));
00051 }
00052
00056 public function setMethodName($methodName)
00057 {
00058 if (strpos($methodName, '::') === false) {
00059 $dao = $this->dao();
00060
00061 Assert::isTrue(
00062 method_exists($dao, $methodName),
00063 "knows nothing about '".get_class($dao)
00064 ."::{$methodName}' method"
00065 );
00066 } else
00067 ClassUtils::checkStaticMethod($methodName);
00068
00069 $this->methodName = $methodName;
00070
00071 return $this;
00072 }
00073
00074 public function importValue($value)
00075 {
00076 if ($value instanceof Identifiable) {
00077 try {
00078 Assert::isInstance($value, $this->className);
00079
00080 return
00081 $this->import(
00082 array($this->getName() => $value->getId())
00083 );
00084
00085 } catch (WrongArgumentException $e) {
00086 return false;
00087 }
00088 }
00089
00090 return parent::importValue($value);
00091 }
00092
00093 public function import($scope)
00094 {
00095 if (!$this->className)
00096 throw new WrongStateException(
00097 "no class defined for PrimitiveIdentifier '{$this->name}'"
00098 );
00099
00100 $className = $this->className;
00101
00102 if (
00103 isset($scope[$this->name])
00104 && $scope[$this->name] instanceof $className
00105 ) {
00106 $value = $scope[$this->name];
00107
00108 $this->raw = $value->getId();
00109 $this->setValue($value);
00110
00111 return $this->imported = true;
00112 }
00113
00114 $result = parent::import($scope);
00115
00116 if ($result === true) {
00117 try {
00118 $result = $this->actualImportValue($this->value);
00119
00120 Assert::isInstance($result, $className);
00121
00122 $this->value = $result;
00123
00124 return true;
00125
00126 } catch (WrongArgumentException $e) {
00127
00128 } catch (ObjectNotFoundException $e) {
00129
00130 }
00131
00132 $this->value = null;
00133
00134 return false;
00135 }
00136
00137 return $result;
00138 }
00139
00140 protected function actualImportValue($value)
00141 {
00142 return
00143 (strpos($this->methodName, '::') === false)
00144 ? $this->dao()->{$this->methodName}($value)
00145 : ClassUtils::callStaticMethod(
00146 $this->methodName, $value
00147 );
00148 }
00149 }
00150 ?>