[!!!][~API] Extbase (Persistence): $query->execute() now returns a plain array of...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Value.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * A generic holder for the value of a property. A Value object can be used
30 * without knowing the actual property type (STRING, DOUBLE, BINARY etc.).
31 *
32 * @package Extbase
33 * @subpackage Persistence
34 * @version $Id$
35 * @scope prototype
36 */
37 class Tx_Extbase_Persistence_Value implements Tx_Extbase_Persistence_ValueInterface {
38
39 /**
40 * @var mixed
41 */
42 protected $value;
43
44 /**
45 * @var integer
46 */
47 protected $type;
48
49 /**
50 * Constructs a Value object from the given $value and $type arguments
51 *
52 * @param mixed $value The value of the Value object
53 * @param integer $type A type, see constants in \F3\PHPCR\PropertyType
54 * @return void
55 */
56 public function __construct($value, $type) {
57 $this->value = $value;
58 $this->type = $type;
59 }
60
61 /**
62 * Returns a string representation of this value. For Value objects being
63 * of type DATE the string will conform to ISO8601 format.
64 *
65 * @return string A String representation of the value of this property.
66 */
67 public function getString() {
68 if ($this->value === NULL) return NULL;
69 if (is_array($this->value)) return $this->value;
70 if ($this->value instanceof Tx_Extbase_DomainObject_AbstractDomainObject) {
71 return (string)$this->value->getUid();
72 }
73
74 switch ($this->type) {
75 case Tx_Extbase_Persistence_PropertyType::DATE:
76 if (is_a($this->value, 'DateTime')) {
77 return $this->value->format('U');
78 } else {
79 $this->value = new DateTime($this->value);
80 return $this->value->format('U');
81 }
82 case Tx_Extbase_Persistence_PropertyType::BOOLEAN:
83 return (string)(int)$this->value;
84 default:
85 return (string)$this->value;
86 }
87 }
88
89 /**
90 * Returns the value as string, alias for getString()
91 *
92 * @return string
93 */
94 public function __toString() {
95 return $this->getString();
96 }
97
98 /**
99 * Returns a Binary representation of this value. The Binary object in turn provides
100 * methods to access the binary data itself. Uses the standard conversion to binary
101 * (see JCR specification).
102 *
103 * @return \F3\TYPO3CR\Binary A Binary representation of this value.
104 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
105 */
106 public function getBinary() {
107 throw new Tx_Extbase_Persistence_Exception_UnsupportedMethod('Method not yet implemented, sorry!', 1217843676);
108 }
109
110 /**
111 * Returns a long (integer) representation of this value.
112 *
113 * @return string A long representation of the value of this property.
114 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException if conversion to a long is not possible.
115 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
116 */
117 public function getLong() {
118 return (int)$this->value;
119 }
120
121 /**
122 * Returns a BigDecimal representation of this value (aliased to getDouble()).
123 *
124 * @return float A double representation of the value of this property.
125 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException if conversion is not possible.
126 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
127 */
128 public function getDecimal() {
129 return $this->getDouble();
130 }
131
132 /**
133 * Returns a double (floating point) representation of this value.
134 *
135 * @return float A double representation of the value of this property.
136 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException if conversion to a double is not possible.
137 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
138 */
139 public function getDouble() {
140 return (double)$this->value;
141 }
142
143 /**
144 * Returns a DateTime representation of this value.
145 *
146 * @return DateTime A DateTime representation of the value of this property.
147 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException if conversion to a \DateTime is not possible.
148 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
149 */
150 public function getDate() {
151 if (is_a($this->value, 'DateTime')) {
152 return clone($this->value);
153 }
154
155 try {
156 return new DateTime($this->value);
157 } catch (Exception $e) {
158 throw new Tx_Extbase_Persistence_Exception_ValueFormatException('Conversion to a DateTime object is not possible. Cause: ' . $e->getMessage(), 1190034628);
159 }
160 }
161
162 /**
163 * Returns a boolean representation of this value.
164 *
165 * @return string A boolean representation of the value of this property.
166 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException if conversion to a boolean is not possible.
167 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
168 */
169 public function getBoolean() {
170 return (boolean)$this->value;
171 }
172
173 /**
174 * Returns the type of this Value. One of:
175 * * \F3\PHPCR\PropertyType::STRING
176 * * \F3\PHPCR\PropertyType::DATE
177 * * \F3\PHPCR\PropertyType::BINARY
178 * * \F3\PHPCR\PropertyType::DOUBLE
179 * * \F3\PHPCR\PropertyType::DECIMAL
180 * * \F3\PHPCR\PropertyType::LONG
181 * * \F3\PHPCR\PropertyType::BOOLEAN
182 * * \F3\PHPCR\PropertyType::NAME
183 * * \F3\PHPCR\PropertyType::PATH
184 * * \F3\PHPCR\PropertyType::REFERENCE
185 * * \F3\PHPCR\PropertyType::WEAKREFERENCE
186 * * \F3\PHPCR\PropertyType::URI
187 *
188 * The type returned is that which was set at property creation.
189 * @return integer The type of the value
190 */
191 public function getType() {
192 return $this->type;
193 }
194
195 }
196
197 ?>