root/trunk/src/main/php/net/stubbles/xml/serializer/annotations/stubXMLMatcherAnnotation.php

Revision 1389, 3.0 kB (checked in by mikey, 2 months ago)

implemented enhancement #128: improved performance of XMLSerializer
Serializing a list of instances of the same object is much faster now.

Line 
1 <?php
2 /**
3  * Annotation for XMLSerializer
4  *
5  * @author      Stephan Schmidt <schst@stubbles.net>
6  * @package     stubbles
7  * @subpackage  xml_serializer_annotations
8  */
9 stubClassLoader::load('net::stubbles::reflection::annotations::stubAnnotation',
10                       'net::stubbles::reflection::annotations::stubAbstractAnnotation',
11                       'net::stubbles::xml::stubXMLException',
12                       'net::stubbles::xml::serializer::annotations::stubXMLMethodsAnnotation',
13                       'net::stubbles::xml::serializer::annotations::stubXMLPropertiesAnnotation'
14 );
15
16 /**
17  * Annotation for XMLSerializer
18  *
19  * Use this annotation to define, which properties/methods of a class should be serialized
20  *
21  * Properties of the annotation are:
22  * - pattern
23  *
24  * @package     stubbles
25  * @subpackage  xml_serializer_annotations
26  */
27 class stubXMLMatcherAnnotation extends stubAbstractAnnotation implements stubAnnotation, stubXMLPropertiesAnnotation, stubXMLMethodsAnnotation
28 {
29     /**
30      * Pattern of the properties, that should be serialized
31      *
32      * @var  string
33      */
34     protected $pattern;
35
36     /**
37      * Set the pattern
38      *
39      * @param  string  $pattern
40      */
41     public function setPattern($pattern)
42     {
43         $this->pattern = $pattern;
44     }
45
46     /**
47      * Get the name of the tag to use for a property
48      *
49      * @param   stubReflectionProperty  $property
50      * @return  string|false
51      * @throws  stubXMLException
52      */
53     public function getTagnameForProperty(stubReflectionProperty $property)
54     {
55         $matches = array();
56         $success = @preg_match($this->pattern, $property->getName(), $matches);
57         if (false === $success) {
58             throw new stubXMLException("Syntax error in regular expression '{$this->pattern}': {$php_errormsg}");
59         }
60         
61         if (empty($matches) === true) {
62             return false;
63         }
64         
65         if (isset($matches[1]) === true) {
66             return $matches[1];
67         }
68         
69         return $matches[0];
70     }
71
72     /**
73      * Get the name of the tag to use for a method
74      *
75      * @param   stubReflectionMethod  $method
76      * @return  string|bool
77      * @throws  stubXMLException
78      */
79     public function getTagnameForMethod(stubReflectionMethod $method)
80     {
81         $matches = array();
82         $success = preg_match($this->pattern, $method->getName(), $matches);
83         if (false === $success) {
84             throw new stubXMLException("Syntax error in regular expression '{$this->pattern}': {$php_errormsg}");
85         }
86         
87         if (empty($matches) === true) {
88             return false;
89         }
90         
91         if (isset($matches[1]) === true) {
92             $name = $matches[1];
93         } else {
94             $name = $matches[0];
95         }
96         
97         return strtolower($name{0}) . substr($name, 1);
98     }
99
100     /**
101      * Returns the target of the annotation as bitmap.
102      *
103      * @return  int
104      */
105     public function getAnnotationTarget()
106     {
107         return stubAnnotation::TARGET_CLASS;
108     }
109 }
110 ?>
Note: See TracBrowser for help on using the browser.