Changeset 1385
- Timestamp:
- 02/27/08 17:47:38 (4 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/reflection/matcher (added)
- trunk/src/main/php/net/stubbles/reflection/matcher/stubMethodMatcher.php (added)
- trunk/src/main/php/net/stubbles/reflection/matcher/stubPropertyMatcher.php (added)
- trunk/src/main/php/net/stubbles/reflection/stubBaseReflectionClass.php (modified) (3 diffs)
- trunk/src/main/php/net/stubbles/reflection/stubReflectionClass.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/reflection/stubReflectionObject.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/xml/serializer/matcher (added)
- trunk/src/main/php/net/stubbles/xml/serializer/matcher/stubXMLSerializerMethodPropertyMatcher.php (added)
- trunk/src/main/php/net/stubbles/xml/serializer/stubXMLSerializer.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/reflection/stubBaseReflectionClass.php
r1281 r1385 8 8 */ 9 9 stubClassLoader::load('net::stubbles::reflection::annotations::stubAnnotatable', 10 'net::stubbles::reflection::stubReflectionType' 10 'net::stubbles::reflection::stubReflectionType', 11 'net::stubbles::reflection::matcher::stubMethodMatcher', 12 'net::stubbles::reflection::matcher::stubPropertyMatcher' 11 13 ); 12 14 /** … … 59 61 60 62 /** 63 * returns a list of all methods which satify the given matcher 64 * 65 * @param stubMethodMatcher $methodMatcher 66 * @return array<stubReflectionMethod> 67 */ 68 public function getMethodsByMatcher(stubMethodMatcher $methodMatcher); 69 70 /** 61 71 * returns the specified property or null if it does not exist 62 72 * … … 72 82 */ 73 83 public function getProperties(); 84 85 /** 86 * returns a list of all properties which satify the given matcher 87 * 88 * @param stubPropertyMatcher $propertyMatcher 89 * @return array<stubReflectionProperty> 90 */ 91 public function getPropertiesByMatcher(stubPropertyMatcher $propertyMatcher); 74 92 75 93 /** trunk/src/main/php/net/stubbles/reflection/stubReflectionClass.php
r1281 r1385 173 173 174 174 /** 175 * returns a list of all methods which satify the given matcher 176 * 177 * @param stubMethodMatcher $methodMatcher 178 * @return array<stubReflectionMethod> 179 */ 180 public function getMethodsByMatcher(stubMethodMatcher $methodMatcher) 181 { 182 $methods = parent::getMethods(); 183 $stubMethods = array(); 184 foreach ($methods as $method) { 185 if ($methodMatcher->matchesMethod($method) === true) { 186 $stubMethod = new stubReflectionMethod($this, $method->getName()); 187 if ($methodMatcher->matchesAnnotatableMethod($stubMethod) === true) { 188 $stubMethods[] = $stubMethod; 189 } 190 } 191 } 192 193 return $stubMethods; 194 } 195 196 /** 175 197 * returns the specified property or null if it does not exist 176 198 * … … 205 227 206 228 /** 229 * returns a list of all properties which satify the given matcher 230 * 231 * @param stubPropertyMatcher $propertyMatcher 232 * @return array<stubReflectionProperty> 233 */ 234 public function getPropertiesByMatcher(stubPropertyMatcher $propertyMatcher) 235 { 236 $properties = parent::getProperties(); 237 $stubProperties = array(); 238 foreach ($properties as $property) { 239 if ($propertyMatcher->matchesProperty($property) === true) { 240 $stubProperty = new stubReflectionProperty($this, $property->getName()); 241 if ($propertyMatcher->matchesAnnotatableProperty($stubProperty) === true) { 242 $stubProperties[] = $stubProperty; 243 } 244 } 245 } 246 247 return $stubProperties; 248 } 249 250 /** 207 251 * returns a list of all interfaces 208 252 * trunk/src/main/php/net/stubbles/reflection/stubReflectionObject.php
r1281 r1385 190 190 191 191 /** 192 * returns a list of all methods which satify the given matcher 193 * 194 * @param stubMethodMatcher $methodMatcher 195 * @return array<stubReflectionMethod> 196 */ 197 public function getMethodsByMatcher(stubMethodMatcher $methodMatcher) 198 { 199 $methods = parent::getMethods(); 200 $stubMethods = array(); 201 foreach ($methods as $method) { 202 if ($methodMatcher->matchesMethod($method) === true) { 203 $stubMethod = new stubReflectionMethod($this, $method->getName()); 204 if ($methodMatcher->matchesAnnotatableMethod($stubMethod) === true) { 205 $stubMethods[] = $stubMethod; 206 } 207 } 208 } 209 210 return $stubMethods; 211 } 212 213 /** 192 214 * returns the specified property or null if it does not exist 193 215 * … … 222 244 223 245 /** 246 * returns a list of all properties which satify the given matcher 247 * 248 * @param stubPropertyMatcher $propertyMatcher 249 * @return array<stubReflectionProperty> 250 */ 251 public function getPropertiesByMatcher(stubPropertyMatcher $propertyMatcher) 252 { 253 $properties = parent::getProperties(); 254 $stubProperties = array(); 255 foreach ($properties as $property) { 256 if ($propertyMatcher->matchesProperty($property) === true) { 257 $stubProperty = new stubReflectionProperty($this, $property->getName()); 258 if ($propertyMatcher->matchesAnnotatableProperty($stubProperty) === true) { 259 $stubProperties[] = $stubProperty; 260 } 261 } 262 } 263 264 return $stubProperties; 265 } 266 267 /** 224 268 * returns a list of all interfaces 225 269 * trunk/src/main/php/net/stubbles/xml/serializer/stubXMLSerializer.php
r1301 r1385 15 15 'net::stubbles::xml::serializer::annotations::stubXMLMatcherAnnotation', 16 16 'net::stubbles::xml::serializer::annotations::stubXMLStrategyAnnotation', 17 'net::stubbles::xml::serializer::matcher::stubXMLSerializerMethodPropertyMatcher', 17 18 'net::stubbles::reflection::reflection' 18 19 ); … … 68 69 */ 69 70 private $opts; 71 /** 72 * the matcher to be used for methods and properties 73 * 74 * @var stubXMLSerializerMethodPropertyMatcher 75 */ 76 protected $methodAndPropertyMatcher; 77 78 /** 79 * constructor 80 */ 81 public function __construct() 82 { 83 $this->methodAndPropertyMatcher = new stubXMLSerializerMethodPropertyMatcher(); 84 } 70 85 71 86 /** … … 142 157 * Serialize an object 143 158 * 144 * @param object $object 145 * @param stubXMLStreamWriter $xmlWriter 146 * @param string $tagName 147 * @throws stubXMLException 148 * @todo fetch a strategy from the annotation 149 * @todo Refactor this method into smaller methods for improved readability 159 * @param object $object 160 * @param stubXMLStreamWriter $xmlWriter 161 * @param string $tagName 162 * @todo Refactor this method into smaller methods for improved readability 150 163 */ 151 164 protected function serializeObject($object, stubXMLStreamWriter $xmlWriter, $tagName) 152 165 { 153 166 $clazz = new stubReflectionClass(get_class($object)); 154 155 if ($tagName === null) { 156 if ($clazz->hasAnnotation('XMLTag')) { 157 $xmlTag = $clazz->getAnnotation('XMLTag'); 158 $tagName = $xmlTag->getTagName(); 167 if (null === $tagName) { 168 if ($clazz->hasAnnotation('XMLTag') === true) { 169 $tagName = $clazz->getAnnotation('XMLTag')->getTagName(); 159 170 } else { 160 171 $tagName = get_class($object); … … 163 174 164 175 $xmlWriter->writeStartElement($tagName); 165 166 176 if ($clazz->hasAnnotation('XMLStrategy')) { 167 177 $strategy = $clazz->getAnnotation('XMLStrategy')->getValue(); … … 171 181 172 182 // export props 173 $properties = $clazz->getProperties ();174 183 $properties = $clazz->getPropertiesByMatcher($this->methodAndPropertyMatcher); 184 $matcher = null; 175 185 // get the property-matcher 176 186 if ($clazz->hasAnnotation('XMLProperties')) { 177 187 $matcher = $clazz->getAnnotation('XMLProperties'); 178 } else {179 $matcher = null;180 188 } 181 189 182 190 foreach ($properties as $property) { 183 if (!$property->isPublic()) { 191 $propValue = $property->getValue($object); 192 if ($this->writeAnnotatedElement($property, $xmlWriter, $propValue) === true) { 184 193 continue; 185 194 } 186 if ($property->hasAnnotation('XMLIgnore')) { 187 continue; 188 } 189 $propValue = $property->getValue($object); 190 191 if ($property->hasAnnotation('XMLAttribute') && $property->hasAnnotation('XMLTag')) { 192 throw new stubXMLException('It is not possible specify @XMLAttribute and @XMLTag for a property.'); 193 } 194 if ($property->hasAnnotation('XMLAttribute')) { 195 $xmlAttribute = $property->getAnnotation('XMLAttribute'); 196 if ((string) $propValue === '' && $xmlAttribute->shouldSkipEmpty()) { 197 continue; 198 } 199 $xmlWriter->writeAttribute($xmlAttribute->getAttributeName(), (string) $propValue); 200 continue; 201 } elseif ($property->hasAnnotation('XMLFragment')) { 202 $xmlFragment = $property->getAnnotation('XMLFragment'); 203 $tagName = $xmlFragment->getTagName(); 204 if ($tagName != null) { 205 $xmlWriter->writeStartElement($tagName); 206 $xmlWriter->writeXmlFragment($propValue); 207 $xmlWriter->writeEndElement(); 208 } else { 209 $xmlWriter->writeXmlFragment($propValue); 210 } 211 continue; 212 } elseif ($property->hasAnnotation('XMLTag')) { 213 $xmlTag = $property->getAnnotation('XMLTag'); 214 $tagName = $xmlTag->getTagName(); 195 196 if ($property->hasAnnotation('XMLTag') === true) { 197 $xmlTag = $property->getAnnotation('XMLTag'); 198 $tagName = $xmlTag->getTagName(); 215 199 $elementName = $xmlTag->getElementTagName(); 216 200 } else { 217 201 if (null !== $matcher) { 218 202 $tagName = $matcher->getTagnameForProperty($property, $propValue); 219 if ( $tagName === false) {203 if (false === $tagName) { 220 204 continue; 221 205 } … … 224 208 continue; 225 209 } 226 if ($property->isStatic() && $this->opts[self::OPT_STATIC] === false) { 227 continue; 228 } 210 211 if ($this->opts[self::OPT_STATIC] === false && $property->isStatic() === true) { 212 continue; 213 } 214 229 215 $tagName = $property->getName(); 230 216 } 231 217 $elementName = null; 232 218 } 233 if (is_array($propValue)) { 219 220 if (is_array($propValue) === true) { 234 221 $this->serializeArray($propValue, $xmlWriter, $tagName, $elementName); 235 222 } else { … … 239 226 240 227 // export methods 241 $methods = $clazz->getMethods ();242 228 $methods = $clazz->getMethodsByMatcher($this->methodAndPropertyMatcher); 229 $matcher = null; 243 230 // get the method-matcher 244 231 if ($clazz->hasAnnotation('XMLMethods')) { 245 232 $matcher = $clazz->getAnnotation('XMLMethods'); 246 } else { 247 $matcher = null; 248 } 233 } 234 249 235 foreach ($methods as $method) { 250 if (!$method->isPublic()) { 236 $returnValue = $method->invoke($object); 237 if ($this->writeAnnotatedElement($method, $xmlWriter, $returnValue) === true) { 251 238 continue; 252 239 } 253 if ($method->isConstructor() || $method->isDestructor()) { 254 continue; 255 } 256 if (0 == strncmp($method->getName(), '__', 2)) { 257 continue; 258 } 259 if (0 != $method->getNumberOfParameters()) { 260 continue; 261 } 262 if ($method->hasAnnotation('XMLIgnore')) { 263 continue; 264 } 265 266 $returnValue = $method->invoke($object); 267 if ($method->hasAnnotation('XMLAttribute') && $method->hasAnnotation('XMLTag')) { 268 throw new stubXMLException('It is not possible specify @XMLAttribute and @XMLTag for a method.'); 269 } 270 if ($method->hasAnnotation('XMLAttribute')) { 271 $xmlAttribute = $method->getAnnotation('XMLAttribute'); 272 if ((string) $returnValue === '' && $xmlAttribute->shouldSkipEmpty()) { 273 continue; 274 } 275 $xmlWriter->writeAttribute($xmlAttribute->getAttributeName(), (string) $returnValue); 276 continue; 277 } elseif ($method->hasAnnotation('XMLFragment')) { 278 $xmlFragment = $method->getAnnotation('XMLFragment'); 279 $tagName = $xmlFragment->getTagName(); 280 if ($tagName != null) { 281 $xmlWriter->writeStartElement($tagName); 282 $xmlWriter->writeXmlFragment($returnValue); 283 $xmlWriter->writeEndElement(); 284 } else { 285 $xmlWriter->writeXmlFragment($returnValue); 286 } 287 continue; 288 } elseif ($method->hasAnnotation('XMLTag')) { 289 $xmlTag = $method->getAnnotation('XMLTag'); 290 $tagName = $xmlTag->getTagName(); 240 241 if ($method->hasAnnotation('XMLTag') === true) { 242 $xmlTag = $method->getAnnotation('XMLTag'); 243 $tagName = $xmlTag->getTagName(); 291 244 $elementName = $xmlTag->getElementTagName(); 292 245 } else { 293 246 if (null !== $matcher) { 294 247 $tagName = $matcher->getTagnameForMethod($method, $returnValue); 295 if ( $tagName === false) {248 if (false === $tagName) { 296 249 continue; 297 250 } … … 300 253 continue; 301 254 } 302 if ($method->isStatic() && $this->opts[self::OPT_STATIC] === false) { 255 256 if ($this->opts[self::OPT_STATIC] === false && $method->isStatic() === true) { 303 257 continue; 304 258 } … … 306 260 $tagName = $method->getName(); 307 261 } 262 308 263 $elementName = null; 309 264 } 310 265 311 if (is_array($returnValue) ) {266 if (is_array($returnValue) === true) { 312 267 $this->serializeArray($returnValue, $xmlWriter, $tagName, $elementName); 313 268 } else { … … 315 270 } 316 271 } 272 317 273 $xmlWriter->writeEndElement(); 274 } 275 276 /** 277 * serializes annotated element 278 * 279 * Returns true if element was serialized, else false. 280 * 281 * @param stubAnnotatable $annotatable the annotatable element to serialize 282 * @param stubXMLStreamWriter $xmlWriter the xml writer to use 283 * @param mixed $value the value to serialize 284 * @return bool 285 */ 286 protected function writeAnnotatedElement(stubAnnotatable $annotatable, stubXMLStreamWriter $xmlWriter, $value) 287 { 288 if ($annotatable->hasAnnotation('XMLAttribute') === true) { 289 $xmlAttribute = $annotatable->getAnnotation('XMLAttribute'); 290 if ('' === (string) $value && $xmlAttribute->shouldSkipEmpty() === true) { 291 return true; 292 } 293 294 $xmlWriter->writeAttribute($xmlAttribute->getAttributeName(), (string) $value); 295 return true; 296 } elseif ($annotatable->hasAnnotation('XMLFragment') === true) { 297 $tagName = $annotatable->getAnnotation('XMLFragment')->getTagName(); 298 if (null != $tagName) { 299 $xmlWriter->writeStartElement($tagName); 300 $xmlWriter->writeXmlFragment($value); 301 $xmlWriter->writeEndElement(); 302 } else { 303 $xmlWriter->writeXmlFragment($value); 304 } 305 306 return true; 307 } 308 309 return false; 318 310 } 319 311
