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