Add Extbase 1.0.1 to TYPO3core. Do NOT make changes inside! See misc/core_svn_rules...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Reflection / PropertyReflection.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Christopher Hlubek <hlubek@networkteam.com>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Extended version of the ReflectionProperty
27 *
28 * @package Extbase
29 * @subpackage Reflection
30 * @version $Id: PropertyReflection.php 1052 2009-08-05 21:51:32Z sebastian $
31 */
32 class Tx_Extbase_Reflection_PropertyReflection extends ReflectionProperty {
33
34 /**
35 * @var Tx_Extbase_Reflection_DocCommentParser: An instance of the doc comment parser
36 */
37 protected $docCommentParser;
38
39 /**
40 * The constructor, initializes the reflection class
41 *
42 * @param string $className Name of the property's class
43 * @param string $propertyName Name of the property to reflect
44 * @return void
45 */
46 public function __construct($className, $propertyName) {
47 parent::__construct($className, $propertyName);
48 }
49
50 /**
51 * Checks if the doc comment of this property is tagged with
52 * the specified tag
53 *
54 * @param string $tag Tag name to check for
55 * @return boolean TRUE if such a tag has been defined, otherwise FALSE
56 */
57 public function isTaggedWith($tag) {
58 $result = $this->getDocCommentParser()->isTaggedWith($tag);
59 return $result;
60 }
61
62 /**
63 * Returns an array of tags and their values
64 *
65 * @return array Tags and values
66 */
67 public function getTagsValues() {
68 return $this->getDocCommentParser()->getTagsValues();
69 }
70
71 /**
72 * Returns the values of the specified tag
73 *
74 * @return array Values of the given tag
75 */
76 public function getTagValues($tag) {
77 return $this->getDocCommentParser()->getTagValues($tag);
78 }
79
80 /**
81 * Returns the value of the reflected property - even if it is protected.
82 *
83 * @param object $object Instance of the declaring class Tx_Extbase_Reflection_to read the value from
84 * @return mixed Value of the property
85 * @throws Tx_Extbase_Reflection_Exception
86 * @todo Maybe support private properties as well, as of PHP 5.3.0 we can do
87 * $obj = new Foo;
88 * $prop = new ReflectionProperty('Foo', 'y'); // y is private member
89 * $prop->setAccessible(true);
90 * var_dump($prop->getValue($obj)); // int(2)
91 */
92 public function getValue($object = NULL) {
93 if (!is_object($object)) throw new Tx_Extbase_Reflection_Exception('$object is of type ' . gettype($object) . ', instance of class ' . $this->class . ' expected.', 1210859212);
94 if ($this->isPublic()) return parent::getValue($object);
95 if ($this->isPrivate()) throw new Tx_Extbase_Reflection_Exception('Cannot return value of private property "' . $this->name . '.', 1210859206);
96
97 parent::setAccessible(TRUE);
98 return parent::getValue($object);
99 }
100
101 /**
102 * Returns an instance of the doc comment parser and
103 * runs the parse() method.
104 *
105 * @return Tx_Extbase_Reflection_DocCommentParser
106 */
107 protected function getDocCommentParser() {
108 if (!is_object($this->docCommentParser)) {
109 $this->docCommentParser = new Tx_Extbase_Reflection_DocCommentParser;
110 $this->docCommentParser->parseDocComment($this->getDocComment());
111 }
112 return $this->docCommentParser;
113 }
114 }
115
116 ?>