[CLEANUP] Adjust code to coding guidelines
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / IdentityMap.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * This class is a backport of the corresponding class of TYPO3 Flow.
8 * All credits go to the TYPO3 Flow team.
9 * All rights reserved
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 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * An identity mapper to map nodes to objects
32 *
33 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
34 * @see \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
35 * @see \TYPO3\CMS\Extbase\Persistence\Generic\Backend
36 */
37 class IdentityMap implements \TYPO3\CMS\Core\SingletonInterface {
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage
41 */
42 protected $objectMap;
43
44 /**
45 * @var array
46 */
47 protected $uuidMap = array();
48
49 /**
50 * Constructs a new Identity Map
51 *
52 * @author Karsten Dambekalns <karsten@typo3.org>
53 */
54 public function __construct() {
55 $this->objectMap = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
56 }
57
58 /**
59 * Checks whether the given object is known to the identity map
60 *
61 * @param object $object
62 * @return boolean
63 * @author Karsten Dambekalns <karsten@typo3.org>
64 */
65 public function hasObject($object) {
66 return $this->objectMap->contains($object);
67 }
68
69 /**
70 * Checks whether the given UUID is known to the identity map
71 *
72 * @param string $uuid
73 * @param string $className
74 * @return boolean
75 */
76 public function hasIdentifier($uuid, $className) {
77 if (is_array($this->uuidMap[$className])) {
78 return array_key_exists($uuid, $this->uuidMap[$className]);
79 } else {
80 return FALSE;
81 }
82 }
83
84 /**
85 * Returns the object for the given UUID
86 *
87 * @param string $uuid
88 * @param string $className
89 * @return object
90 */
91 public function getObjectByIdentifier($uuid, $className) {
92 return $this->uuidMap[$className][$uuid];
93 }
94
95 /**
96 * Returns the node identifier for the given object
97 *
98 * @param object $object
99 * @throws \InvalidArgumentException
100 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
101 * @return string
102 * @author Karsten Dambekalns <karsten@typo3.org>
103 */
104 public function getIdentifierByObject($object) {
105 if (!is_object($object)) {
106 throw new \InvalidArgumentException('Object expected, ' . gettype($object) . ' given.', 1246892972);
107 }
108 if (!isset($this->objectMap[$object])) {
109 throw new \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException('The given object (class: ' . get_class($object) . ') is not registered in this Identity Map.', 1246892970);
110 }
111 return $this->objectMap[$object];
112 }
113
114 /**
115 * Register a node identifier for an object
116 *
117 * @param object $object
118 * @param string $uuid
119 * @author Karsten Dambekalns <karsten@typo3.org>
120 */
121 public function registerObject($object, $uuid) {
122 $this->objectMap[$object] = $uuid;
123 $this->uuidMap[get_class($object)][$uuid] = $object;
124 }
125
126 /**
127 * Unregister an object
128 *
129 * @param object $object
130 * @return void
131 * @author Karsten Dambekalns <karsten@typo3.org>
132 */
133 public function unregisterObject($object) {
134 unset($this->uuidMap[get_class($object)][$this->objectMap[$object]]);
135 $this->objectMap->detach($object);
136 }
137 }
138
139 ?>