|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de> 00006 * All rights reserved 00007 * 00008 * This class is a backport of the corresponding class of FLOW3. 00009 * All credits go to the v5 team. 00010 * 00011 * This script is part of the TYPO3 project. The TYPO3 project is 00012 * free software; you can redistribute it and/or modify 00013 * it under the terms of the GNU General Public License as published by 00014 * the Free Software Foundation; either version 2 of the License, or 00015 * (at your option) any later version. 00016 * 00017 * The GNU General Public License can be found at 00018 * http://www.gnu.org/copyleft/gpl.html. 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00027 00028 /** 00029 * A composite of controller arguments 00030 * 00031 * @package Extbase 00032 * @subpackage MVC\Controller 00033 * @version $ID:$ 00034 * @scope prototype 00035 */ 00036 class Tx_Extbase_MVC_Controller_Arguments extends ArrayObject { 00037 /** 00038 * @var Tx_Extbase_Object_ObjectManagerInterface 00039 */ 00040 protected $objectManager; 00041 00042 /** 00043 * @var array Names of the arguments contained by this object 00044 */ 00045 protected $argumentNames = array(); 00046 00047 /** 00048 * Constructor. If this one is removed, reflection breaks. 00049 */ 00050 public function __construct() { 00051 parent::__construct(); 00052 } 00053 00054 /** 00055 * Injects the object manager 00056 * 00057 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager 00058 * @return void 00059 */ 00060 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) { 00061 $this->objectManager = $objectManager; 00062 } 00063 00064 /** 00065 * Adds or replaces the argument specified by $value. The argument's name is taken from the 00066 * argument object itself, therefore the $offset does not have any meaning in this context. 00067 * 00068 * @param mixed $offset Offset - not used here 00069 * @param mixed $value The argument 00070 * @return void 00071 * @throws InvalidArgumentException if the argument is not a valid Controller Argument object 00072 */ 00073 public function offsetSet($offset, $value) { 00074 if (!$value instanceof Tx_Extbase_MVC_Controller_Argument) throw new InvalidArgumentException('Controller arguments must be valid Tx_Extbase_MVC_Controller_Argument objects.', 1187953786); 00075 00076 $argumentName = $value->getName(); 00077 parent::offsetSet($argumentName, $value); 00078 $this->argumentNames[$argumentName] = TRUE; 00079 } 00080 00081 /** 00082 * Sets an argument, aliased to offsetSet() 00083 * 00084 * @param mixed $value The value 00085 * @return void 00086 * @throws InvalidArgumentException if the argument is not a valid Controller Argument object 00087 */ 00088 public function append($value) { 00089 if (!$value instanceof Tx_Extbase_MVC_Controller_Argument) throw new InvalidArgumentException('Controller arguments must be valid Tx_Extbase_MVC_Controller_Argument objects.', 1187953786); 00090 $this->offsetSet(NULL, $value); 00091 } 00092 00093 /** 00094 * Unsets an argument 00095 * 00096 * @param mixed $offset Offset 00097 * @return void 00098 */ 00099 public function offsetUnset($offset) { 00100 $translatedOffset = $this->translateToLongArgumentName($offset); 00101 parent::offsetUnset($translatedOffset); 00102 00103 unset($this->argumentNames[$translatedOffset]); 00104 if ($offset != $translatedOffset) { 00105 unset($this->argumentShortNames[$offset]); 00106 } 00107 } 00108 00109 /** 00110 * Returns whether the requested index exists 00111 * 00112 * @param mixed $offset Offset 00113 * @return boolean 00114 */ 00115 public function offsetExists($offset) { 00116 $translatedOffset = $this->translateToLongArgumentName($offset); 00117 return parent::offsetExists($translatedOffset); 00118 } 00119 00120 /** 00121 * Returns the value at the specified index 00122 * 00123 * @param mixed $offset Offset 00124 * @return Tx_Extbase_MVC_Controller_Argument The requested argument object 00125 * @throws Tx_Extbase_MVC_Exception_NoSuchArgument if the argument does not exist 00126 */ 00127 public function offsetGet($offset) { 00128 $translatedOffset = $this->translateToLongArgumentName($offset); 00129 if ($translatedOffset === '') throw new Tx_Extbase_MVC_Exception_NoSuchArgument('The argument "' . $offset . '" does not exist.', 1216909923); 00130 return parent::offsetGet($translatedOffset); 00131 } 00132 00133 /** 00134 * Creates, adds and returns a new controller argument to this composite object. 00135 * If an argument with the same name exists already, it will be replaced by the 00136 * new argument object. 00137 * 00138 * @param string $name Name of the argument 00139 * @param string $dataType Name of one of the built-in data types 00140 * @param boolean $isRequired TRUE if this argument should be marked as required 00141 * @param mixed $defaultValue Default value of the argument. Only makes sense if $isRequired==FALSE 00142 * @return Tx_Extbase_MVC_Controller_Argument The new argument 00143 */ 00144 public function addNewArgument($name, $dataType = 'Text', $isRequired = FALSE, $defaultValue = NULL) { 00145 $argument = $this->objectManager->create('Tx_Extbase_MVC_Controller_Argument', $name, $dataType); 00146 $argument->setRequired($isRequired); 00147 $argument->setDefaultValue($defaultValue); 00148 $this->addArgument($argument); 00149 return $argument; 00150 } 00151 00152 /** 00153 * Adds the specified controller argument to this composite object. 00154 * If an argument with the same name exists already, it will be replaced by the 00155 * new argument object. 00156 * 00157 * Note that the argument will be cloned, not referenced. 00158 * 00159 * @param Tx_Extbase_MVC_Controller_Argument $argument The argument to add 00160 * @return void 00161 */ 00162 public function addArgument(Tx_Extbase_MVC_Controller_Argument $argument) { 00163 $this->offsetSet(NULL, $argument); 00164 } 00165 00166 /** 00167 * Returns an argument specified by name 00168 * 00169 * @param string $argumentName Name of the argument to retrieve 00170 * @return Tx_Extbase_MVC_Controller_Argument 00171 * @throws Tx_Extbase_MVC_Exception_NoSuchArgument 00172 */ 00173 public function getArgument($argumentName) { 00174 if (!$this->offsetExists($argumentName)) throw new Tx_Extbase_MVC_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist.', 1195815178); 00175 return $this->offsetGet($argumentName); 00176 } 00177 00178 /** 00179 * Checks if an argument with the specified name exists 00180 * 00181 * @param string $argumentName Name of the argument to check for 00182 * @return boolean TRUE if such an argument exists, otherwise FALSE 00183 * @see offsetExists() 00184 */ 00185 public function hasArgument($argumentName) { 00186 return $this->offsetExists($argumentName); 00187 } 00188 00189 /** 00190 * Returns the names of all arguments contained in this object 00191 * 00192 * @return array Argument names 00193 */ 00194 public function getArgumentNames() { 00195 return array_keys($this->argumentNames); 00196 } 00197 00198 /** 00199 * Returns the short names of all arguments contained in this object that have one. 00200 * 00201 * @return array Argument short names 00202 */ 00203 public function getArgumentShortNames() { 00204 $argumentShortNames = array(); 00205 foreach ($this as $argument) { 00206 $argumentShortNames[$argument->getShortName()] = TRUE; 00207 } 00208 return array_keys($argumentShortNames); 00209 } 00210 00211 /** 00212 * Magic setter method for the argument values. Each argument 00213 * value can be set by just calling the setArgumentName() method. 00214 * 00215 * @param string $methodName Name of the method 00216 * @param array $arguments Method arguments 00217 * @return void 00218 */ 00219 public function __call($methodName, array $arguments) { 00220 if (substr($methodName, 0, 3) !== 'set') throw new LogicException('Unknown method "' . $methodName . '".', 1210858451); 00221 00222 $firstLowerCaseArgumentName = $this->translateToLongArgumentName(strtolower($methodName{3}) . substr($methodName, 4)); 00223 $firstUpperCaseArgumentName = $this->translateToLongArgumentName(ucfirst(substr($methodName, 3))); 00224 00225 if (in_array($firstLowerCaseArgumentName, $this->getArgumentNames())) { 00226 $argument = parent::offsetGet($firstLowerCaseArgumentName); 00227 $argument->setValue($arguments[0]); 00228 } elseif (in_array($firstUpperCaseArgumentName, $this->getArgumentNames())) { 00229 $argument = parent::offsetGet($firstUpperCaseArgumentName); 00230 $argument->setValue($arguments[0]); 00231 } 00232 } 00233 00234 /** 00235 * Translates a short argument name to its corresponding long name. If the 00236 * specified argument name is a real argument name already, it will be returned again. 00237 * 00238 * If an argument with the specified name or short name does not exist, an empty 00239 * string is returned. 00240 * 00241 * @param string argument name 00242 * @return string long argument name or empty string 00243 */ 00244 protected function translateToLongArgumentName($argumentName) { 00245 if (in_array($argumentName, $this->getArgumentNames())) return $argumentName; 00246 00247 foreach ($this as $argument) { 00248 if ($argumentName === $argument->getShortName()) return $argument->getName(); 00249 } 00250 return ''; 00251 } 00252 00253 /** 00254 * Remove all arguments and resets this object 00255 * 00256 * @return void 00257 */ 00258 public function removeAll() { 00259 foreach ($this->argumentNames as $argumentName => $booleanValue) { 00260 parent::offsetUnset($argumentName); 00261 } 00262 $this->argumentNames = array(); 00263 } 00264 } 00265 ?>
1.8.0