root/trunk/src/main/php/net/stubbles/lang/stubBaseObject.php

Revision 1301, 6.3 kB (checked in by richi, 4 months ago)

coding standards: fixed coding standards issues (function docblock)

Line 
1 <?php
2 /**
3  * base class for all other stubbles classes except static ones and classes
4  * extending php built-in classes
5  *
6  * @author      Frank Kleine  <mikey@stubbles.net>
7  * @package     stubbles
8  * @subpackage  lang
9  */
10 /**
11  * base class for all other stubbles classes except static ones and classes
12  * extending php built-in classes
13  *
14  * @package     stubbles
15  * @subpackage  lang
16  */
17 class stubBaseObject implements stubObject
18 {
19     /**
20      * returns class informations
21      *
22      * @return  stubReflectionObject
23      * @XMLIgnore
24      */
25     public function getClass()
26     {
27         stubClassLoader::load('net::stubbles::reflection::stubReflectionObject');
28         $refObject = new stubReflectionObject($this);
29         return $refObject;
30     }
31
32     /**
33      * returns package informations
34      *
35      * @return  stubReflectionPackage
36      * @XMLIgnore
37      */
38     public function getPackage()
39     {
40          stubClassLoader::load('net::stubbles::reflection::stubReflectionPackage');
41          $refPackage = new stubReflectionPackage(stubClassLoader::getPackageName($this->getClassName()));
42          return $refPackage;
43     }
44
45     /**
46      * returns the full qualified class name
47      *
48      * @return  string
49      * @XMLIgnore
50      */
51     public function getClassName()
52     {
53         return stubClassLoader::getFullQualifiedClassName(get_class($this));
54     }
55
56     /**
57      * returns the name of the package where the class is inside
58      *
59      * @return  string
60      * @XMLIgnore
61      */
62     public function getPackageName()
63     {
64         return stubClassLoader::getPackageName($this->getClassName());
65     }
66
67     /**
68      * returns a unique hash code for the class
69      *
70      * @return  string
71      * @XMLIgnore
72      */
73     public function hashCode()
74     {
75         return spl_object_hash($this);
76     }
77
78     /**
79      * checks whether a value is equal to the class
80      *
81      * @param   mixed  $compare
82      * @return  bool
83      * @XMLIgnore
84      */
85     public function equals($compare)
86     {
87         if ($compare instanceof stubObject) {
88             return ($this->hashCode() == $compare->hashCode());
89         }
90
91         return false;
92     }
93
94     /**
95      * returns a string representation of the class
96      *
97      * The result is a short but informative representation about the class and
98      * its values. Per default, this method returns:
99      * [fully-qualified-class-name] ' {' [members-and-value-list] '}'
100      * <code>
101      * example::MyClass {
102      *     foo(string): hello
103      *     bar(example::AnotherClass): example::AnotherClass {
104      *         baz(int): 5
105      *     }
106      * }
107      * </code>
108      *
109      * @return  string
110      * @XMLIgnore
111      */
112     public function __toString()
113     {
114         return self::getStringRepresentationOf($this, self::_extractProperties($this));
115     }
116
117     /**
118      * helper method to extract all properties regardless of their visibility
119      *
120      * This is a workaround for the problem that as of PHP 5.2.4 get_object_vars()
121      * is not any more capable of retrieving private properties from child classes.
122      * See http://stubbles.org/archives/32-Subtle-BC-break-in-PHP-5.2.4.html.
123      *
124      * @param   mixed  $object
125      * @return  array<string,mixed>
126      */
127     protected static function _extractProperties($object)
128     {
129         $properties      = (array) $object;
130         $fixedProperties = array();
131         foreach ($properties as $propertyName => $propertyValue) {
132             if (strstr($propertyName, "\0") === false) {
133                 $fixedProperties[$propertyName] = $propertyValue;
134                 continue;
135             }
136             
137             $fixedProperties[substr($propertyName, strrpos($propertyName, "\0") + 1)] = $propertyValue;
138         }
139         
140         return $fixedProperties;
141     }
142
143     /**
144      * returns a string representation of the class
145      *
146      * The result is a short but informative representation about the class and
147      * its values. Per default, this method returns:
148      * [fully-qualified-class-name] ' {' [members-and-value-list] '}'
149      * <code>
150      * example::MyClass {
151      *     foo(string): hello
152      *     bar(example::AnotherClass): example::AnotherClass {
153      *         baz(int): 5
154      *     }
155      * }
156      * </code>
157      * Please note that protected and private properties of the class wil only be
158      * in the result if the second argument contains a list of the properties and
159      * its values. If not set only public properties can be extracted due to the
160      * behaviour of get_object_vars().
161      *
162      * @param   stubObject  $object      the object to convert to a string
163      * @param   array       $properties  optional  the properties, if not set they will be retrieved
164      * @return  string
165      * @XMLIgnore
166      */
167     public static function getStringRepresentationOf(stubObject $object, array $properties = null)
168     {
169         if (null === $properties) {
170             $properties = self::_extractProperties($object);
171         }
172
173         $string = $object->getClassName() . " {\n";
174         foreach ($properties as $name => $value) {
175             if ('_serializedProperties' == $name) {
176                 continue;
177             }
178
179             $string .= '    ' . $name . '(' . self::_determineType($value) . '): ';
180             if (($value instanceof self) == false) {
181                 $string .= $value . "\n";
182                 continue;
183             }
184
185             $lines       = explode("\n", (string) $value);
186             $lineCounter = 0;
187             foreach ($lines as $line) {
188                 if (empty($line) == true) {
189                     continue;
190                 }
191
192                 if (0 != $lineCounter) {
193                     $string .= '    ' . $line . "\n";
194                 } else {
195                     $string .= $line . "\n";
196                 }
197
198                 $lineCounter++;
199             }
200         }
201
202         $string .= "}\n";
203         return $string;
204     }
205
206     /**
207      * determines the correct type of a value
208      *
209      * @param   mixed   &$value
210      * @return  string
211      */
212     private static function _determineType(&$value)
213     {
214         if (is_object($value) === false) {
215             if (is_resource($value) === false) {
216                 return gettype($value);
217             }
218
219             return 'resource[' . get_resource_type($value) . ']';
220         }
221
222         if ($value instanceof stubObject) {
223             return $value->getClassName();
224         }
225
226         return get_class($value);
227     }
228 }
229 ?>
Note: See TracBrowser for help on using the browser.