e4783f69367c5ea4ba7f5bffee8e7afd281616a0
[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 Idetity Map for Domain Objects
30 *
31 * @package Extbase
32 * @subpackage Persistence
33 * @version $ID:$
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 $uidMap = array();
46
47 /**
48 * Constructs a new Identity Map
49 */
50 public function __construct() {
51 $this->objectMap = new Tx_Extbase_Persistence_ObjectStorage();
52 }
53
54 /**
55 * Checks whether the given object is known to the identity map
56 *
57 * @param object $object
58 * @return boolean
59 */
60 public function hasObject($object) {
61 return $this->objectMap->contains($object);
62 }
63
64 /**
65 * Checks whether the given uiduid is known to the identity map
66 *
67 * @param string $className
68 * @param string $uid
69 * @return boolean
70 */
71 public function hasUid($className, $uid) {
72 if (is_array($this->uidMap[$className])) {
73 return array_key_exists($uid, $this->uidMap[$className]);
74 } else {
75 return FALSE;
76 }
77 }
78
79 /**
80 * Returns the object for the given uid
81 *
82 * @param string $className
83 * @param string $uid
84 * @return object
85 */
86 public function getObjectByUid($className, $uid) {
87 return $this->uidMap[$className][$uid];
88 }
89
90 /**
91 * Returns the node identifier for the given object
92 *
93 * @param object $object
94 * @return string
95 */
96 public function getIdentifierByObject($object) {
97 return $this->objectMap[$object];
98 }
99
100 /**
101 * Register a node identifier for an object
102 *
103 * @param object $object
104 * @param string $uid
105 */
106 public function registerObject($object, $uid) {
107 $this->objectMap[$object] = $uid;
108 $this->uidMap[get_class($object)][$uid] = $object;
109 }
110
111 /**
112 * Unregister an object
113 *
114 * @param string $object
115 * @return void
116 */
117 public function unregisterObject($object) {
118 unset($this->uidMap[get_class($object)][$this->objectMap[$object]]);
119 $this->objectMap->detach($object);
120 }
121
122 }
123
124 ?>