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