Changeset 5
- Timestamp:
- 08/26/06 10:36:54 (2 years ago)
- Files:
-
- trunk/XJConf/converters/factories (added)
- trunk/XJConf/converters/factories/FactoryMethodValueConverterFactory.php (added)
- trunk/XJConf/converters/factories/ObjectValueConverterFactory.php (added)
- trunk/XJConf/converters/factories/PrimitiveValueConverterFactory.php (added)
- trunk/XJConf/converters/factories/ValueConverterFactory.php (added)
- trunk/XJConf/converters/factories/ValueConverterFactoryChain.php (added)
- trunk/XJConf/definitions/CDataDefinition.php (modified) (9 diffs)
- trunk/XJConf/definitions/ChildDefinition.php (modified) (9 diffs)
- trunk/XJConf/definitions/ConstructorDefinition.php (modified) (10 diffs)
- trunk/XJConf/definitions/Definition.php (modified) (4 diffs)
- trunk/XJConf/definitions/FactoryMethodDefinition.php (modified) (9 diffs)
- trunk/XJConf/definitions/TagDefinition.php (modified) (34 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/XJConf/definitions/CDataDefinition.php
r2 r5 6 6 /** 7 7 * Definition for the character data inside a tag. 8 * 8 * 9 9 * This is used to pass the character data to the constructor 10 10 * of the tag, while casting it to the desired class. 11 * 11 * 12 12 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 13 13 */ … … 28 28 /** 29 29 * Converter used to convert the character data 30 * 30 * 31 31 * @var ValueConverter 32 32 */ 33 33 private $valueConverter; 34 34 35 35 /** 36 36 * Create a new CDataDefinition for any other type 37 * 38 * @param string $type optional type of the content 37 * 38 * @param string $type optional type of the content 39 39 */ 40 40 public function __construct($type = null) … … 56 56 /** 57 57 * Set the setter method 58 * 58 * 59 59 * If no setter method is specified, the standard 60 * name "setAttributename()" will be used instead. 61 * 60 * name "setAttributename()" will be used instead. 61 * 62 62 * @param string $setter name of the setter method 63 63 * @see getSetterMethod() … … 67 67 $this->setter = $setter; 68 68 } 69 69 70 70 /** 71 71 * Get the setter method, which is setData() by default 72 * 72 * 73 73 * @return string 74 74 * @see setSetterMethod() … … 78 78 return $this->setter; 79 79 } 80 80 81 81 /** 82 82 * Character data cannot have any child definitions … … 84 84 public function addChildDefinition(Definition $def) 85 85 { 86 // Character data can not have any children. 86 // Character data can not have any children. 87 87 } 88 88 89 89 /** 90 90 * Convert the character data to any type 91 * 91 * 92 92 * @param Tag $tag 93 93 * @return mixed concerted value … … 106 106 /** 107 107 * get the name under which the data will be stored 108 * 108 * 109 109 * @return string 110 110 */ … … 116 116 /** 117 117 * Get the type of the cdata 118 * 118 * 119 119 * @param Tag $tag 120 120 * @return string … … 123 123 return $this->valueConverter->getType(); 124 124 } 125 126 125 126 /** 127 * get the type of the data 128 * 129 * @return string 130 */ 131 public function getType() 132 { 133 return $this->type; 134 } 127 135 } 128 136 ?> trunk/XJConf/definitions/ChildDefinition.php
r2 r5 8 8 /** 9 9 * Definition to access the child of the tag. 10 * 10 * 11 11 * This can be used to pass the child to the constructor. 12 * 12 * 13 13 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 14 14 */ … … 17 17 /** 18 18 * Name of the child to access 19 * 19 * 20 20 * @var string 21 21 */ 22 22 private $name = null; 23 23 24 24 /** 25 25 * Create a new child definition 26 * 26 * 27 27 * @param string $name name of child 28 28 * @throws XJConfException … … 33 33 throw new XJConfException('ChildDefinition needs a name.'); 34 34 } 35 35 36 36 $this->name = $name; 37 37 } 38 38 39 39 /** 40 40 * Get the name of the child. 41 * 41 * 42 42 * @return string 43 43 */ … … 49 49 /** 50 50 * Convert the value 51 * 51 * 52 52 * @param Tag $tag 53 53 * @return mixed concerted value … … 60 60 throw new ValueConversionException('Child element "' . $this->getName() . '" does not exist.'); 61 61 } 62 62 63 63 return $child->getConvertedValue(); 64 64 } … … 66 66 /** 67 67 * Get the type of the child 68 * 68 * 69 69 * @param Tag $tag 70 70 * @return string … … 77 77 throw new ValueConversionException('Child element "' . $this->getName() . '" does not exist.'); 78 78 } 79 79 80 80 return $child->getValueType($tag); 81 81 } 82 82 83 83 /** 84 84 * This does not provide a setter method. 85 * 85 * 86 86 * @return null 87 87 */ … … 93 93 /** 94 94 * It's not possible to add a new child. 95 * 95 * 96 96 * @param Definition $def 97 97 */ … … 100 100 // Character data can not have any children. 101 101 } 102 103 /** 104 * get the type of the child 105 * 106 * @return string 107 */ 108 public function getType() 109 { 110 return null; 111 } 102 112 } 103 113 ?> trunk/XJConf/definitions/ConstructorDefinition.php
r2 r5 3 3 /** 4 4 * Definition for the constructor of a class 5 * 5 * 6 6 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 7 7 */ … … 10 10 /** 11 11 * Parameters of the constructor 12 * 12 * 13 13 * @var array<Definition> 14 14 */ … … 17 17 /** 18 18 * Add a new child definition (equals a parameter of the constructor) 19 * 19 * 20 20 * @param Definition $def 21 21 */ … … 27 27 /** 28 28 * Get the parameters of the constructor 29 * 29 * 30 30 * @return array 31 31 */ … … 37 37 /** 38 38 * Convert the constructor. 39 * 39 * 40 40 * This does not do anything! 41 * 41 * 42 42 * @param Tag $tag 43 43 * @return null … … 47 47 return null; 48 48 } 49 50 /** 49 50 /** 51 51 * Get the name under which it will be stored 52 * 52 * 53 53 * @return string 54 54 */ … … 60 60 /** 61 61 * Get the type of the constructor 62 * 62 * 63 63 * @param Tag $tag 64 64 * @return null … … 68 68 return null; 69 69 } 70 70 71 71 /** 72 72 * Get the setter method 73 * 73 * 74 74 * @return null 75 75 */ … … 82 82 * Get the names of all child elements that are used in 83 83 * the constructor. 84 * 84 * 85 85 * These children are not used, when adding them using 86 86 * setter-methods. 87 * 87 * 88 88 * @return array 89 89 */ … … 96 96 } 97 97 } 98 98 99 99 return $childrenNames; 100 100 } 101 102 /** 103 * get the type of the constructor 104 * 105 * @return string 106 */ 107 public function getType() 108 { 109 return null; 110 } 101 111 } 102 112 ?> trunk/XJConf/definitions/Definition.php
r2 r5 2 2 /** 3 3 * Interface for tag and attribute definitions 4 * 4 * 5 5 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 6 6 */ … … 10 10 * Get the name under which the information 11 11 * will be stored. 12 * 12 * 13 13 * @return string name of the value 14 14 */ … … 17 17 /** 18 18 * Get the converted value. 19 * 19 * 20 20 * XJConf will pass the complete tag to this method 21 * 21 * 22 22 * @param Tag $tag value 23 23 * @return mixed 24 24 */ 25 25 public function convertValue(Tag $tag); 26 26 27 27 /** 28 28 * Get the type of the converted value … … 31 31 */ 32 32 public function getValueType(Tag $tag); 33 33 34 34 /** 35 35 * Get the name of the setter method 36 * 36 * 37 37 * @return string 38 38 */ 39 39 public function getSetterMethod(); 40 40 41 41 /** 42 42 * Add a child definition of any type 43 * 43 * 44 44 * @param Definition def 45 45 */ 46 46 public function addChildDefinition(Definition $def); 47 48 /** 49 * Get the type of the definition 50 * 51 * @return string 52 */ 53 public function getType(); 47 54 } 48 55 ?> trunk/XJConf/definitions/FactoryMethodDefinition.php
r2 r5 15 15 /** 16 16 * Parameters of the factory method 17 * 17 * 18 18 * @var array 19 19 */ … … 21 21 /** 22 22 * name of factory method 23 * 23 * 24 24 * @var string 25 25 */ 26 26 private $name; 27 27 28 28 /** 29 29 * construcor … … 38 38 /** 39 39 * Get the parameters of the factory method 40 * 40 * 41 41 * @return array 42 42 */ … … 48 48 /** 49 49 * Add a new child definition (equals a parameter of the factory method) 50 * 50 * 51 51 * @param Definition $def 52 52 */ … … 55 55 array_push($this->params, $def); 56 56 } 57 58 /** 57 58 /** 59 59 * Get the name under which it will be stored 60 * 60 * 61 61 * @return string 62 62 */ … … 65 65 return $this->name; 66 66 } 67 67 68 68 /** 69 69 * Convert the factory method. 70 * 70 * 71 71 * @param Tag $tag 72 72 * @throws UnsupportedOperationException … … 76 76 throw new UnsupportedOperationException(); 77 77 } 78 78 79 79 /** 80 80 * Get the type of the factory method. 81 * 81 * 82 82 * @param Tag $tag 83 83 * @throws UnsupportedOperationException … … 87 87 throw new UnsupportedOperationException(); 88 88 } 89 89 90 90 /** 91 91 * Get the setter method 92 * 92 * 93 93 * @throws UnsupportedOperationException 94 94 */ … … 97 97 throw new UnsupportedOperationException(); 98 98 } 99 100 /** 101 * get the type of the factory method 102 * 103 * @return string 104 */ 105 public function getType() 106 { 107 return null; 108 } 99 109 } 100 110 ?> trunk/XJConf/definitions/TagDefinition.php
r2 r5 1 1 <?php 2 XJConfLoader::load('converters.FactoryMethodValueConverter', 3 'converters.ObjectValueConverter', 4 'converters.PrimitiveValueConverter', 2 XJConfLoader::load('converters.factories.ValueConverterFactoryChain', 5 3 'definitions.AttributeDefinition', 6 4 'definitions.CDataDefinition', … … 13 11 /** 14 12 * Defintion of an XML tag 15 * 13 * 16 14 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 17 15 */ … … 50 48 /** 51 49 * name of attribute that contains the name 52 * 50 * 53 51 * @var string 54 52 */ … … 56 54 /** 57 55 * definition of how to construct the object 58 * 56 * 59 57 * @var ConstructorDefinition 60 58 */ … … 62 60 /** 63 61 * definition of factory that is able to construct the object 64 * 62 * 65 63 * @var FactoryMethodDefinition 66 64 */ … … 68 66 /** 69 67 * converts the value 70 * 68 * 71 69 * @var ValueConverter 72 70 */ … … 74 72 /** 75 73 * definition of tag content 76 * 74 * 77 75 * @var CDataDefinition 78 76 * @todo Eventually call the setter method for the cdata … … 82 80 /** 83 81 * Create a new tag definition 84 * 82 * 85 83 * @param string $name name of the tag 86 84 * @param string $type type of the tag … … 103 101 /** 104 102 * Set the type of the tag 105 * 103 * 106 104 * @param string $type 107 105 */ … … 110 108 $this->type = $type; 111 109 } 112 110 113 111 /** 114 112 * Add a new child definition 115 * 113 * 116 114 * Possible definitions are: 117 115 * - AttributeDefinition 118 116 * - ConstrcutorDefinition 119 117 * - CDataDefinition 120 * 118 * 121 119 * @param Definition $def 122 120 */ … … 127 125 return; 128 126 } 129 127 130 128 if ($def instanceof FactoryMethodDefinition) { 131 129 $this->factoryMethod = $def; 132 130 return; 133 131 } 134 132 135 133 if ($def instanceof ConstructorDefinition) { 136 134 $this->constructor = $def; 137 135 return; 138 136 } 139 137 140 138 if ($def instanceof CDataDefinition) { 141 139 $this->cdata = $def; … … 146 144 /** 147 145 * Add an attribute to the tag 148 * 146 * 149 147 * @param AttributeDefinition $att 150 148 */ … … 153 151 array_push($this->atts, $att); 154 152 } 155 153 156 154 /** 157 155 * set the name of the value 158 * 156 * 159 157 * @param string $name 160 158 */ … … 166 164 /** 167 165 * Set the name of the tag 168 * 166 * 169 167 * @param name 170 168 */ … … 173 171 $this->tagName = $name; 174 172 } 175 173 176 174 /** 177 175 * Set the attribute that will be used as key. 178 * 176 * 179 177 * @return name of the value 180 178 */ … … 184 182 $this->nameAttribute = $att; 185 183 } 186 184 187 185 /** 188 186 * get the name of the value 189 * 187 * 190 188 * @return string 191 189 */ … … 194 192 return $this->name; 195 193 } 196 194 197 195 /** 198 196 * get the name of the tag 199 * 197 * 200 198 * @return string 201 199 */ … … 204 202 return $this->tagName; 205 203 } 206 204 207 205 /** 208 206 * get the name of the tag 209 * 207 * 210 208 * @return string 211 209 */ … … 215 213 return $tag->getAttribute($this->nameAttribute); 216 214 } 217 215 218 216 return $this->name; 219 217 } … … 221 219 /** 222 220 * get the type of the tag 223 * 221 * 224 222 * @return string 225 223 */ … … 231 229 /** 232 230 * Get the type of the tag 233 * 231 * 234 232 * @return string 235 233 */ … … 238 236 return $this->getValueConverter()->getType(); 239 237 } 240 238 241 239 /** 242 240 * Set the setter method 243 * 241 * 244 242 * @param string $setter name of the setter method 245 243 */ … … 248 246 $this->setter = $setter; 249 247 } 250 248 251 249 /** 252 250 * Get the name of the setter method that should be used 253 * 251 * 254 252 * @return string 255 253 */ … … 259 257 return $this->setter; 260 258 } 261 259 262 260 // no name, the parent should be a collection 263 261 if ('__none' == $this->name) { 264 262 return null; 265 263 } 266 264 267 265 return 'set' . ucfirst($this->name); 268 266 } … … 270 268 /** 271 269 * Convert the value of the tag. 272 * 270 * 273 271 * @param Tag $tag tag that will be converted 274 272 * @return mixed converted value … … 293 291 } 294 292 } 295 293 296 294 $conParams = array(); 297 295 if (null != $this->factoryMethod) { … … 300 298 $conParams = $this->constructor->getParams(); 301 299 } 302 300 303 301 $cParams = array(); 304 302 // get all values and their types … … 307 305 } 308 306 $instance = $this->getValueConverter()->convertValue($cParams); 309 307 310 308 // could not call setter methods on a non-object 311 309 if (is_object($instance) == false) { 312 310 return $instance; 313 311 } 314 312 315 313 // add attributes and child elements 316 314 $this->addAttributesToValue($instance, $tag); … … 321 319 /** 322 320 * Add all attributes using the appropriate setter methods 323 * 321 * 324 322 * @param mixed instance 325 323 * @param Tag $tag … … 349 347 /** 350 348 * Add all children to the created instance 351 * 349 * 352 350 * @param mixed instance 353 351 * @param Tag $tag … … 357 355 { 358 356 // traverse all children 359 $children = $tag->getChildren(); 357 $children = $tag->getChildren(); 360 358 if (count($children) == 0) { 361 359 return; 362 360 } 363 361 364 362 $ignore = $this->constructor->getUsedChildrenNames(); 365 363 $class = new ReflectionClass(get_class($instance)); 366 364 367 365 foreach ($children as $child) { 368 366 if (in_array($child->getName(), $ignore) == true) { 369 367 continue; 370 368 } 371 369 372 370 try { 373 371 $class->getMethod($child->getSetterMethod())->invoke($instance, array($child->getConvertedValue())); … … 380 378 /** 381 379 * Check, whether the value supports indexed children 382 * 380 * 383 381 * @return boolean 384 382 */ … … 389 387 return true; 390 388 } 391 389 392 390 return false; 393 391 } … … 395 393 /** 396 394 * Get the value converter for this tag 397 * 395 * 398 396 * @return ValueConverter 399 397 */ 400 398 private function getValueConverter() 401 399 { 402 if (null == $this->valueConverter) { 403 if (in_array($this->getType(), get_declared_classes()) == false) { 404 $this->valueConverter = new PrimitiveValueConverter($this->getType()); 405 } else { 406 if (null != $this->factoryMethod) { 407 $this->valueConverter = new FactoryMethodValueConverter($this->getType(), $this->factoryMethod->getName()); 408 } else { 409 $this->valueConverter = new ObjectValueConverter($this->getType()); 410 } 411 } 412 } 413 414 return $this->valueConverter; 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; 415 412 } 416 413 }
