Extbase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / ValueFactory.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 ValueFactory, used to create Value objects.
30 *
31 * @package Extbase
32 * @version $Id: ValueFactory.php 2191 2009-05-07 19:49:06Z k-fish $
33 * @scope prototype
34 */
35 class Tx_Extbase_Persistence_ValueFactory implements Tx_Extbase_Persistence_ValueFactoryInterface {
36
37 /**
38 * Returns a Value object with the specified value. If $type is given,
39 * conversion is attempted before creating the Value object.
40 *
41 * If no type is given, the value is stored as is, i.e. it's type is
42 * preserved. Exceptions are:
43 * * if the given $value is a Node object, it's Identifier is fetched for the
44 * Value object and the type of that object will be REFERENCE
45 * * if the given $value is a Node object, it's Identifier is fetched for the
46 * Value object and the type of that object will be WEAKREFERENCE if $weak
47 * is set to TRUE
48 * * if the given $Value is a \DateTime object, the Value type will be DATE.
49 *
50 * @param mixed $value The value to use when creating the Value object
51 * @param integer $type Type request for the Value object
52 * @param boolean $weak When a Node is given as $value this can be given as TRUE to create a WEAKREFERENCE, $type is ignored in that case!
53 * @return \F3\PHPCR\ValueInterface
54 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException is thrown if the specified value cannot be converted to the specified type.
55 * @throws Tx_Extbase_Persistence_Exception_RepositoryException if the specified Node is not referenceable, the current Session is no longer active, or another error occurs.
56 * @throws \IllegalArgumentException if the specified DateTime value cannot be expressed in the ISO 8601-based format defined in the JCR 2.0 specification and the implementation does not support dates incompatible with that format.
57 */
58 public function createValue($value, $type = Tx_Extbase_Persistence_PropertyType::UNDEFINED, $weak = FALSE) {
59 if (is_array($value)) {
60 $value = $value['uid'];
61 }
62
63 if ($type === Tx_Extbase_Persistence_PropertyType::UNDEFINED) {
64 return t3lib_div::makeInstance('Tx_Extbase_Persistence_Value', $value, self::guessType($value));
65 } else {
66 return $this->createValueWithGivenType($value, $type);
67 }
68 }
69
70 /**
71 * Returns a Value object with the specified value. Conversion from string
72 * is attempted before creating the Value object.
73 *
74 * @param mixed $value
75 * @param integer $type
76 * @return \F3\PHPCR\ValueInterface
77 * @throws Tx_Extbase_Persistence_Exception_ValueFormatException is thrown if the specified value cannot be converted to the specified type.
78 */
79 protected function createValueWithGivenType($value, $type) {
80 switch ($type) {
81 case Tx_Extbase_Persistence_PropertyType::DATE:
82 try {
83 $value = new DateTime($value);
84 } catch (Exception $e) {
85 throw new Tx_Extbase_Persistence_Exception_ValueFormatException('The given value could not be converted to a DateTime object.', 1211372741);
86 }
87 break;
88 case Tx_Extbase_Persistence_PropertyType::BINARY:
89 // we do not do anything here, getBinary on Value objects does the hard work
90 break;
91 case Tx_Extbase_Persistence_PropertyType::DECIMAL:
92 case Tx_Extbase_Persistence_PropertyType::DOUBLE:
93 $value = (float)$value;
94 break;
95 case Tx_Extbase_Persistence_PropertyType::BOOLEAN:
96 $value = (boolean)$value;
97 break;
98 case Tx_Extbase_Persistence_PropertyType::LONG:
99 $value = (int)$value;
100 break;
101 }
102 return t3lib_div::makeInstance('Tx_Persistence_Value', $value, $type);
103 }
104
105 /**
106 * Guesses the type for the given value
107 *
108 * @param mixed $value
109 * @return integer
110 * @todo Check type guessing/conversion when we go for PHP6
111 */
112 public static function guessType($value) {
113 $type = Tx_Extbase_Persistence_PropertyType::UNDEFINED;
114
115 if ($value instanceof DateTime) {
116 $type = Tx_Extbase_Persistence_PropertyType::DATE;
117 } elseif (is_double($value)) {
118 $type = Tx_Extbase_Persistence_PropertyType::DOUBLE;
119 } elseif (is_bool($value)) {
120 $type = Tx_Extbase_Persistence_PropertyType::BOOLEAN;
121 } elseif (is_long($value)) {
122 $type = Tx_Extbase_Persistence_PropertyType::LONG;
123 } elseif (is_string($value)) {
124 $type = Tx_Extbase_Persistence_PropertyType::STRING;
125 }
126
127 return $type;
128 }
129 }
130
131 ?>