[~BUG] Extbase (Persistence): The findByFooBar maps the property name to column name...
[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: Value.php 2191 2009-05-07 19:49:06Z k-fish $
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 switch ($this->type) {
70 case Tx_Extbase_Persistence_PropertyType::DATE:
71 if (is_a($this->value, 'DateTime')) {
72 return $this->value->format('U');
73 } else {
74 $this->value = new DateTime($this->value);
75 return $this->value->format('U');
76 }
77 case Tx_Extbase_Persistence_PropertyType::BOOLEAN:
78 return (string)(int)$this->value;
79 default:
80 return (string)$this->value;
81 }
82 }
83
84 /**
85 * Returns the value as string, alias for getString()
86 *
87 * @return string
88 */
89 public function __toString() {
90 return $this->getString();
91 }
92
93 /**
94 * Returns a Binary representation of this value. The Binary object in turn provides
95 * methods to access the binary data itself. Uses the standard conversion to binary
96 * (see JCR specification).
97 *
98 * @return \F3\TYPO3CR\Binary A Binary representation of this value.
99 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
100 */
101 public function getBinary() {
102 throw new Tx_Extbase_Persistence_Exception_UnsupportedMethod('Method not yet implemented, sorry!', 1217843676);
103 }
104
105 /**
106 * Returns a long (integer) representation of this value.
107 *
108 * @return string A long representation of the value of this property.
109 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException if conversion to a long is not possible.
110 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
111 */
112 public function getLong() {
113 return (int)$this->value;
114 }
115
116 /**
117 * Returns a BigDecimal representation of this value (aliased to getDouble()).
118 *
119 * @return float A double representation of the value of this property.
120 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException if conversion is not possible.
121 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
122 */
123 public function getDecimal() {
124 return $this->getDouble();
125 }
126
127 /**
128 * Returns a double (floating point) representation of this value.
129 *
130 * @return float A double representation of the value of this property.
131 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException if conversion to a double is not possible.
132 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
133 */
134 public function getDouble() {
135 return (double)$this->value;
136 }
137
138 /**
139 * Returns a DateTime representation of this value.
140 *
141 * @return DateTime A DateTime representation of the value of this property.
142 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException if conversion to a \DateTime is not possible.
143 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
144 */
145 public function getDate() {
146 if (is_a($this->value, 'DateTime')) {
147 return clone($this->value);
148 }
149
150 try {
151 return new DateTime($this->value);
152 } catch (Exception $e) {
153 throw new Tx_Extbase_Persistence_Exception_ValueFormatException('Conversion to a DateTime object is not possible. Cause: ' . $e->getMessage(), 1190034628);
154 }
155 }
156
157 /**
158 * Returns a boolean representation of this value.
159 *
160 * @return string A boolean representation of the value of this property.
161 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException if conversion to a boolean is not possible.
162 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if another error occurs.
163 */
164 public function getBoolean() {
165 return (boolean)$this->value;
166 }
167
168 /**
169 * Returns the type of this Value. One of:
170 * * \F3\PHPCR\PropertyType::STRING
171 * * \F3\PHPCR\PropertyType::DATE
172 * * \F3\PHPCR\PropertyType::BINARY
173 * * \F3\PHPCR\PropertyType::DOUBLE
174 * * \F3\PHPCR\PropertyType::DECIMAL
175 * * \F3\PHPCR\PropertyType::LONG
176 * * \F3\PHPCR\PropertyType::BOOLEAN
177 * * \F3\PHPCR\PropertyType::NAME
178 * * \F3\PHPCR\PropertyType::PATH
179 * * \F3\PHPCR\PropertyType::REFERENCE
180 * * \F3\PHPCR\PropertyType::WEAKREFERENCE
181 * * \F3\PHPCR\PropertyType::URI
182 *
183 * The type returned is that which was set at property creation.
184 * @return integer The type of the value
185 */
186 public function getType() {
187 return $this->type;
188 }
189
190 }
191
192 ?>