Raised DBAL version from 1.1.5 to 1.1.6
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / LazyObjectStorage.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 proxy that can replace any object and replaces itself in it's parent on
30 * first access (call, get, set, isset, unset).
31 *
32 * @package Extbase
33 * @subpackage Persistence
34 * @version $Id: LazyObjectStorage.php 2208 2010-04-14 13:41:14Z jocrau $
35 */
36 class Tx_Extbase_Persistence_LazyObjectStorage extends Tx_Extbase_Persistence_ObjectStorage implements Tx_Extbase_Persistence_LoadingStrategyInterface {
37
38 /**
39 * The object this property is contained in.
40 *
41 * @var object
42 */
43 protected $parentObject;
44
45 /**
46 * The name of the property represented by this proxy.
47 *
48 * @var string
49 */
50 protected $propertyName;
51
52 /**
53 * The raw field value.
54 *
55 * @var mixed
56 */
57 protected $fieldValue;
58
59 /**
60 *
61 * @var bool
62 */
63 protected $isInitialized = FALSE;
64
65 /**
66 * Constructs this proxy instance.
67 *
68 * @param object $parentObject The object instance this proxy is part of
69 * @param string $propertyName The name of the proxied property in it's parent
70 * @param mixed $fieldValue The raw field value.
71 */
72 public function __construct($parentObject, $propertyName, $fieldValue) {
73 $this->parentObject = $parentObject;
74 $this->propertyName = $propertyName;
75 $this->fieldValue = $fieldValue;
76 }
77
78 /**
79 * This is a function lazy load implementation.
80 *
81 * @return void
82 */
83 protected function initializeStorage() {
84 if (!$this->isInitialized) {
85 $dataMapper = Tx_Extbase_Dispatcher::getPersistenceManager()->getBackend()->getDataMapper();
86 $objects = $dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, FALSE);
87 $storage = array();
88 foreach ($objects as $object) {
89 $storage[spl_object_hash($object)] = $object;
90 }
91 $this->storage = $storage;
92 $this->parentObject->_memorizeCleanState($this->propertyName);
93 $this->isInitialized = TRUE;
94 }
95 }
96
97 /**
98 * Counts the elements in the storage array
99 *
100 * @return void
101 */
102 public function count() {
103 $dataMapper = Tx_Extbase_Dispatcher::getPersistenceManager()->getBackend()->getDataMapper();
104 $columnMap = $dataMapper->getDataMap(get_class($this->parentObject))->getColumnMap($this->propertyName);
105 $numberOfElements = NULL;
106 if ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
107 $numberOfElements = $dataMapper->countRelated($this->parentObject, $this->propertyName, $this->fieldValue);
108 } else {
109 $this->initializeStorage();
110 $numberOfElements = count($this->storage);
111 }
112 if (is_null($numberOfElements)) {
113 throw new Tx_Extbase_Persistence_Exception('The number of elements could not be determined.', 1252514486);
114 }
115 return $numberOfElements;
116 }
117
118 }
119 ?>