Changeset 6
- Timestamp:
- 08/26/06 10:51:25 (2 years ago)
- Files:
-
- trunk/XJConf/converters/factories/FactoryMethodValueConverterFactory.php (modified) (1 diff)
- trunk/XJConf/converters/factories/ObjectValueConverterFactory.php (modified) (1 diff)
- trunk/XJConf/converters/factories/PrimitiveValueConverterFactory.php (modified) (1 diff)
- trunk/XJConf/converters/factories/ValueConverterFactory.php (modified) (2 diffs)
- trunk/XJConf/converters/factories/ValueConverterFactoryChain.php (modified) (1 diff)
- trunk/XJConf/definitions/AttributeDefinition.php (modified) (16 diffs)
- trunk/XJConf/definitions/CDataDefinition.php (modified) (1 diff)
- trunk/XJConf/definitions/ChildDefinition.php (modified) (1 diff)
- trunk/XJConf/definitions/ConstructorDefinition.php (modified) (1 diff)
- trunk/XJConf/definitions/Definition.php (modified) (1 diff)
- trunk/XJConf/definitions/FactoryMethodDefinition.php (modified) (1 diff)
- trunk/XJConf/definitions/TagDefinition.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/XJConf/converters/factories/FactoryMethodValueConverterFactory.php
r5 r6 4 4 class FactoryMethodValueConverterFactory implements ValueConverterFactory { 5 5 6 public function isResponsible( TagDefinition $def) {7 return ($ def->getFactoryMethod() != null);6 public function isResponsible(Definition $def) { 7 return ($this->getFactoryMethod($def) != null); 8 8 } 9 9 10 public function createValueConverter(TagDefinition $def) { 11 $converter = new FactoryMethodValueConverter($def->getType(), $def->getFactoryMethod()->getName()); 10 public function createValueConverter(Definition $def) { 11 $factoryMethod = $this->getFactoryMethod($def); 12 if ($factoryMethod == null) { 13 return null; 14 } 15 $converter = new FactoryMethodValueConverter($def->getType(), $factoryMethod->getName()); 12 16 return $converter; 17 } 18 19 /** 20 * Get the fatcory method of the definition, if available 21 * 22 * @param Definition $def 23 * @return FactoryMethodDefinition 24 */ 25 protected function getFactoryMethod(Definition $def) { 26 foreach ($def->getChildDefinitions() as $child) { 27 if ($child instanceof FactoryMethodDefinition) { 28 return $child; 29 } 30 } 31 return null; 13 32 } 14 33 } trunk/XJConf/converters/factories/ObjectValueConverterFactory.php
r5 r6 4 4 class ObjectValueConverterFactory implements ValueConverterFactory { 5 5 6 public function isResponsible( TagDefinition $def) {6 public function isResponsible(Definition $def) { 7 7 return class_exists($def->getType()); 8 8 } 9 9 10 public function createValueConverter( TagDefinition $def) {10 public function createValueConverter(Definition $def) { 11 11 $converter = new ObjectValueConverter($def->getType()); 12 12 return $converter; trunk/XJConf/converters/factories/PrimitiveValueConverterFactory.php
r5 r6 8 8 ); 9 9 10 public function isResponsible( TagDefinition $def) {10 public function isResponsible(Definition $def) { 11 11 return in_array($def->getType(), $this->primitives); 12 12 } 13 13 14 public function createValueConverter( TagDefinition $def) {14 public function createValueConverter(Definition $def) { 15 15 $converter = new PrimitiveValueConverter($def->getType()); 16 16 return $converter; trunk/XJConf/converters/factories/ValueConverterFactory.php
r5 r6 19 19 * @return boolean 20 20 */ 21 public function isResponsible( TagDefinition $def);21 public function isResponsible(Definition $def); 22 22 23 23 /** … … 27 27 * @return ValueConverter 28 28 */ 29 public function createValueConverter( TagDefinition $def);29 public function createValueConverter(Definition $def); 30 30 } 31 31 ?> trunk/XJConf/converters/factories/ValueConverterFactoryChain.php
r5 r6 12 12 } 13 13 14 public static function getFactory( TagDefinition $def) {14 public static function getFactory(Definition $def) { 15 15 foreach (self::$factories as $factory) { 16 16 if ($factory->isResponsible($def)) { trunk/XJConf/definitions/AttributeDefinition.php
r2 r6 1 1 <?php 2 XJConfLoader::load('converters.ObjectValueConverter', 3 'converters.PrimitiveValueConverter', 2 XJConfLoader::load('converters.factories.ValueConverterFactoryChain', 4 3 'definitions.Definition', 5 4 'exceptions.MissingAttributeException', … … 8 7 /** 9 8 * Definition container of an attribute. 10 * 9 * 11 10 * This class is used to store information on how 12 11 * an attribute of a specific tag should be handled. 13 * 12 * 14 13 * Options include 15 14 * - Type of the Attribute … … 17 16 * - Setter method to set the attribute 18 17 * - Whether the attribute is required, or not 19 * 18 * 20 19 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 21 20 */ 22 21 class AttributeDefinition implements Definition 23 22 { 24 23 25 24 /** 26 25 * name of the attribute … … 29 28 */ 30 29 private $name = null; 31 30 32 31 /** 33 32 * Type of the attribute 34 * 33 * 35 34 * @var string 36 35 */ 37 36 private $type = null; 38 37 39 38 /** 40 39 * Name of the setter method 41 * 40 * 42 41 * @var string 43 42 */ 44 43 private $setter = null; 45 44 46 45 /** 47 46 * Default value 48 * 47 * 49 48 * @var string 50 49 */ 51 50 private $defaultValue = null; 52 51 53 52 /** 54 53 * Whether the attribute is required 55 * 54 * 56 55 * @var boolean 57 56 */ 58 57 private $required = false; 59 58 60 59 /** 61 60 * Converter used to convert the attribute 62 * 61 * 63 62 * @param ValueConverter 64 63 */ 65 64 private $valueConverter; 66 65 67 66 /** 68 67 * create a new attribute definition for a String attribute 69 * 68 * 70 69 * @param string $name name of the attribute 71 70 * @param string $type optional type of the tag … … 77 76 throw new XJConfException('TagDefinition needs a name.'); 78 77 } 79 78 80 79 $this->name = $name; 81 80 if (null == $type || strlen($type) == 0) { … … 84 83 $this->type = $type; 85 84 } 86 87 if (in_array($this->getType(), get_declared_classes()) == false) { 88 $this->valueConverter = new PrimitiveValueConverter($this->type); 89 } else { 90 $this->valueConverter = new ObjectValueConverter($this->type); 91 } 92 } 93 85 } 86 94 87 /** 95 88 * Set the default value for the attribute. 96 * 89 * 97 90 * @param defaultValue default value that will be used, if a tag does not provide the attribute 98 91 * @see getDefault() … … 105 98 /** 106 99 * Get the default value of the attribute. 107 * 100 * 108 101 * @return string 109 102 * @see setDefault() … … 113 106 return $this->defaultValue; 114 107 } 115 108 116 109 /** 117 110 * returns whether the attribute is required or not 118 * 111 * 119 112 * @return boolean 120 113 */ … … 125 118 /** 126 119 * set if attribute is required 127 * 120 * 128 121 * @param boolean $required 129 122 */ … … 135 128 /** 136 129 * Set the setter method 137 * 130 * 138 131 * If no setter method is specified, the standard 139 * name "setAttributename()" will be used instead. 140 * 132 * name "setAttributename()" will be used instead. 133 * 141 134 * @param string $setter name of the setter method 142 135 * @see getSetterMethod() … … 146 139 $this->setter = $setter; 147 140 } 148 149 /** 141 142 /** 150 143 * Get the name of the setter method that should be used 151 144 * to set the attribute value in the parent container 152 * 145 * 153 146 * @return string 154 147 * @see setSetterMethod() … … 159 152 return 'set' . ucfirst($this->name); 160 153 } 161 154 162 155 return $this->setter; 163 156 } 164 157 165 158 /** 166 159 * Get the name of the attribute. 167 * 160 * 168 161 * @return string 169 162 */ … … 172 165 return $this->name; 173 166 } 174 167 175 168 /** 176 169 * Get the type of the attribute 177 * 170 * 178 171 * @param Tag $tag 179 172 * @return string … … 186 179 /** 187 180 * Get the type of the attribute 188 * 181 * 189 182 * @return string 190 183 */ … … 193 186 return $this->type; 194 187 } 195 188 196 189 /** 197 190 * Convert a value to the defined type 198 * 191 * 199 192 * The value you pass in will be cast to a 200 193 * String before it is converted to the defined 201 194 * type. 202 * 195 * 203 196 * The type of the returned value can be specified in 204 197 * the constructor using the type argument. … … 220 213 throw new MissingAttributeException("The attribute '" + this.name + "' is required for the tag '" + tag.getName() + "'."); 221 214 } 222 215 223 216 // it's useless to create an instance passing null to the constructor. 224 217 return null; 225 218 } 226 227 $instance = $this-> valueConverter->convertValue(array($value));219 220 $instance = $this->getValueConverter()->convertValue(array($value)); 228 221 return $instance; 229 222 } 230 223 231 224 /** 225 * Get the value converter for this tag 226 * 227 * @return ValueConverter 228 */ 229 private function getValueConverter() 230 { 231 if ($this->valueConverter == null) { 232 $this->valueConverter = ValueConverterFactoryChain::getFactory($this)->createValueConverter($this); 233 } 234 return $this->valueConverter; 235 } 236 237 /** 232 238 * Add a child definition 233 239 * 234 240 * Attributes can not have any children. 235 * 241 * 236 242 * @param Definition $def 237 243 */ 238 244 public function addChildDefinition(Definition $def) 239 245 { 240 // attributes can not have any children. 246 // attributes can not have any children. 247 } 248 249 /** 250 * Return all child definitions. 251 * 252 * Currently, it is not possible to add any child 253 * definitions to an attribute 254 * 255 * @return array 256 */ 257 public function getChildDefinitions() { 258 return array(); 241 259 } 242 260 } trunk/XJConf/definitions/CDataDefinition.php
r5 r6 133 133 return $this->type; 134 134 } 135 136 /** 137 * Return all child definitions. 138 * 139 * Currently, it is not possible to add any child 140 * definitions to cdata 141 * 142 * @return array 143 */ 144 public function getChildDefinitions() { 145 return array(); 146 } 135 147 } 136 148 ?> trunk/XJConf/definitions/ChildDefinition.php
r5 r6 101 101 } 102 102 103 /** 104 * Return all child definitions. 105 * 106 * Currently, it is not possible to add any child 107 * definitions to a child 108 * 109 * @return array 110 */ 111 public function getChildDefinitions() { 112 return array(); 113 } 114 103 115 /** 104 116 * get the type of the child trunk/XJConf/definitions/ConstructorDefinition.php
r5 r6 100 100 } 101 101 102 /** 103 * Return all child definitions. 104 * 105 * @return array 106 */ 107 public function getChildDefinitions() { 108 return $this->params; 109 } 110 102 111 /** 103 112 * get the type of the constructor trunk/XJConf/definitions/Definition.php
r5 r6 52 52 */ 53 53 public function getType(); 54 55 /** 56 * Get all child definitions of the definition 57 * 58 * @return array 59 */ 60 public function getChildDefinitions(); 54 61 } 55 62 ?> trunk/XJConf/definitions/FactoryMethodDefinition.php
r5 r6 98 98 } 99 99 100 /** 101 * Return all child definitions. 102 * 103 * @return array 104 */ 105 public function getChildDefinitions() { 106 return $this->params; 107 } 108 100 109 /** 101 110 * get the type of the factory method trunk/XJConf/definitions/TagDefinition.php
r5 r6 392 392 393 393 /** 394 * Return all child definitions. 395 * 396 * @return array 397 */ 398 public function getChildDefinitions() { 399 $children = $this->atts; 400 if ($this->factoryMethod instanceof Definition) { 401 $children[] = $this->factoryMethod; 402 } 403 if ($this->constructor instanceof Definition) { 404 $children[] = $this->constructor; 405 } 406 if ($this->cdata instanceof Definition) { 407 $children[] = $this->cdata; 408 } 409 return $children; 410 } 411 412 /** 394 413 * Get the value converter for this tag 395 414 * … … 398 417 private function getValueConverter() 399 418 { 400 return ValueConverterFactoryChain::getFactory($this)->createValueConverter($this); 401 402 } 403 404 /** 405 * Get the factory method definition 406 * 407 * @return FactoryMethodDefinition 408 * @todo Find a solution to make this more flexible so the Definition interface provides a way to get all child definitions 409 */ 410 public function getFactoryMethod() { 411 return $this->factoryMethod; 419 if ($this->valueConverter == null) { 420 $this->valueConverter = ValueConverterFactoryChain::getFactory($this)->createValueConverter($this); 421 } 422 return $this->valueConverter; 412 423 } 413 424 }
