Raised DBAL version from 1.1.5 to 1.1.6
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / IdentityMap.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 * An identity mapper to map nodes to objects
30 *
31 * @version $Id: IdentityMap.php 1814 2010-02-06 20:25:48Z jocrau $
32 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
33 * @see \F3\TYPO3CR\FLOW3\Persistence\DataMapper, \F3\TYPO3CR\FLOW3\Persistence\Backend
34 */
35 class Tx_Extbase_Persistence_IdentityMap {
36
37 /**
38 * @var Tx_Extbase_Persistence_ObjectStorage
39 */
40 protected $objectMap;
41
42 /**
43 * @var array
44 */
45 protected $uuidMap = array();
46
47 /**
48 * Constructs a new Identity Map
49 *
50 * @author Karsten Dambekalns <karsten@typo3.org>
51 */
52 public function __construct() {
53 $this->objectMap = new Tx_Extbase_Persistence_ObjectStorage();
54 }
55
56 /**
57 * Checks whether the given object is known to the identity map
58 *
59 * @param object $object
60 * @return boolean
61 * @author Karsten Dambekalns <karsten@typo3.org>
62 */
63 public function hasObject($object) {
64 return $this->objectMap->contains($object);
65 }
66
67 /**
68 * Checks whether the given UUID is known to the identity map
69 *
70 * @param string $uuid
71 * @param string $className
72 * @return boolean
73 */
74 public function hasIdentifier($uuid, $className) {
75 if (is_array($this->uuidMap[$className])) {
76 return array_key_exists($uuid, $this->uuidMap[$className]);
77 } else {
78 return FALSE;
79 }
80 }
81
82 /**
83 * Returns the object for the given UUID
84 *
85 * @param string $uuid
86 * @param string $className
87 * @return object
88 */
89 public function getObjectByIdentifier($uuid, $className) {
90 return $this->uuidMap[$className][$uuid];
91 }
92
93 /**
94 * Returns the node identifier for the given object
95 *
96 * @param object $object
97 * @return string
98 * @author Karsten Dambekalns <karsten@typo3.org>
99 */
100 public function getIdentifierByObject($object) {
101 if (!is_object($object)) throw new InvalidArgumentException('Object expected, ' . gettype($object) . ' given.', 1246892972);
102 if (!isset($this->objectMap[$object])) {
103 throw new Tx_Extbase_Persistence_Exception_UnknownObjectException('The given object (class: ' . get_class($object) . ') is not registered in this Identity Map.', 1246892970);
104 }
105 return $this->objectMap[$object];
106 }
107
108 /**
109 * Register a node identifier for an object
110 *
111 * @param object $object
112 * @param string $uuid
113 * @author Karsten Dambekalns <karsten@typo3.org>
114 */
115 public function registerObject($object, $uuid) {
116 $this->objectMap[$object] = $uuid;
117 $this->uuidMap[get_class($object)][$uuid] = $object;
118 }
119
120 /**
121 * Unregister an object
122 *
123 * @param string $object
124 * @return void
125 * @author Karsten Dambekalns <karsten@typo3.org>
126 */
127 public function unregisterObject($object) {
128 unset($this->uuidMap[get_class($object)][$this->objectMap[$object]]);
129 $this->objectMap->detach($object);
130 }
131
132 }
133 ?>