Extbase:
[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 * @version $Id: Value.php 2191 2009-05-07 19:49:06Z k-fish $
34 * @scope prototype
35 */
36 class Tx_Extbase_Persistence_Value implements Tx_Extbase_Persistence_ValueInterface {
37
38 /**
39 * @var mixed
40 */
41 protected $value;
42
43 /**
44 * @var integer
45 */
46 protected $type;
47
48 /**
49 * Constructs a Value object from the given $value and $type arguments
50 *
51 * @param mixed $value The value of the Value object
52 * @param integer $type A type, see constants in \F3\PHPCR\PropertyType
53 * @return void
54 */
55 public function __construct($value, $type) {
56 $this->value = $value;
57 $this->type = $type;
58 }
59
60 /**
61 * Returns a string representation of this value. For Value objects being
62 * of type DATE the string will conform to ISO8601 format.
63 *
64 * @return string A String representation of the value of this property.
65 */
66 public function getString() {
67 switch ($this->type) {
68 case Tx_Extbase_Persistence_PropertyType::DATE:
69 if (is_a($this->value, 'DateTime')) {
70 return date_format($this->value, DATE_ISO8601);
71 } else {
72 return date_format(new DateTime($this->value), DATE_ISO8601);
73 }
74 case Tx_Extbase_Persistence_PropertyType::BOOLEAN:
75 return (string)(int)$this->value;
76 default:
77 return (string)$this->value;
78 }
79 }
80
81 /**
82 * Returns the value as string, alias for getString()
83 *
84 * @return string
85 */
86 public function __toString() {
87 return $this->getString();
88 }
89
90 /**
91 * Returns a Binary representation of this value. The Binary object in turn provides
92 * methods to access the binary data itself. Uses the standard conversion to binary
93 * (see JCR specification).
94 *
95 * @return \F3\TYPO3CR\Binary A Binary representation of this value.
96 * @throws \F3\PHPCR\RepositoryException if another error occurs.
97 */
98 public function getBinary() {
99 throw new Tx_Extbase_Persistence_Exception_UnsupportedMethod('Method not yet implemented, sorry!', 1217843676);
100 }
101
102 /**
103 * Returns a long (integer) representation of this value.
104 *
105 * @return string A long representation of the value of this property.
106 * @throws \F3\PHPCR\ValueFormatException if conversion to a long is not possible.
107 * @throws \F3\PHPCR\RepositoryException if another error occurs.
108 */
109 public function getLong() {
110 return (int)$this->value;
111 }
112
113 /**
114 * Returns a BigDecimal representation of this value (aliased to getDouble()).
115 *
116 * @return float A double representation of the value of this property.
117 * @throws \F3\PHPCR\ValueFormatException if conversion is not possible.
118 * @throws \F3\PHPCR\RepositoryException if another error occurs.
119 */
120 public function getDecimal() {
121 return $this->getDouble();
122 }
123
124 /**
125 * Returns a double (floating point) representation of this value.
126 *
127 * @return float A double representation of the value of this property.
128 * @throws \F3\PHPCR\ValueFormatException if conversion to a double is not possible.
129 * @throws \F3\PHPCR\RepositoryException if another error occurs.
130 */
131 public function getDouble() {
132 return (double)$this->value;
133 }
134
135 /**
136 * Returns a \DateTime representation of this value.
137 *
138 * @return \DateTime A \DateTime representation of the value of this property.
139 * @throws \F3\PHPCR\ValueFormatException if conversion to a \DateTime is not possible.
140 * @throws \F3\PHPCR\RepositoryException if another error occurs.
141 */
142 public function getDate() {
143 if (is_a($this->value, 'DateTime')) {
144 return clone($this->value);
145 }
146
147 try {
148 return new DateTime($this->value);
149 } catch (Exception $e) {
150 throw new Tx_Extbase_Persistence_Exception_ValueFormatException('Conversion to a \DateTime object is not possible. Cause: ' . $e->getMessage(), 1190034628);
151 }
152 }
153
154 /**
155 * Returns a boolean representation of this value.
156 *
157 * @return string A boolean representation of the value of this property.
158 * @throws \F3\PHPCR\ValueFormatException if conversion to a boolean is not possible.
159 * @throws \F3\PHPCR\RepositoryException if another error occurs.
160 */
161 public function getBoolean() {
162 return (boolean)$this->value;
163 }
164
165 /**
166 * Returns the type of this Value. One of:
167 * * \F3\PHPCR\PropertyType::STRING
168 * * \F3\PHPCR\PropertyType::DATE
169 * * \F3\PHPCR\PropertyType::BINARY
170 * * \F3\PHPCR\PropertyType::DOUBLE
171 * * \F3\PHPCR\PropertyType::DECIMAL
172 * * \F3\PHPCR\PropertyType::LONG
173 * * \F3\PHPCR\PropertyType::BOOLEAN
174 * * \F3\PHPCR\PropertyType::NAME
175 * * \F3\PHPCR\PropertyType::PATH
176 * * \F3\PHPCR\PropertyType::REFERENCE
177 * * \F3\PHPCR\PropertyType::WEAKREFERENCE
178 * * \F3\PHPCR\PropertyType::URI
179 *
180 * The type returned is that which was set at property creation.
181 * @return integer The type of the value
182 */
183 public function getType() {
184 return $this->type;
185 }
186
187 }
188
189 ?>