This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Container.php
181 lines (155 loc) · 4.03 KB
/
Container.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace ext\activedocument;
use \CComponent;
abstract class Container extends CComponent {
/**
* @var \ext\activedocument\Adapter
*/
protected $_adapter;
/**
* @var \ext\activedocument\Connection
*/
protected $_connection;
/**
* @var string
*/
protected $_name;
protected $_containerInstance;
/**
* @var bool
*/
protected $_propertiesFetched = false;
/**
* @var array
*/
protected $_properties;
abstract protected function loadContainerInstance();
/**
* @abstract
* @return array
*/
abstract protected function loadProperties();
/**
* @abstract
* @return array
*/
abstract public function getKeys();
/**
* @abstract
* @return bool
*/
abstract public function delete();
/**
* @abstract
* @param array $keys
* @return bool
*/
abstract public function deleteKeys(array $keys);
/**
* @abstract
* @param string $key Optional Key to initialize object. Default null
* @param mixed $data Optional Data to initialize object. Default null
* @param bool $new Optional Whether object is new. Default false
* @return \ext\activedocument\Object
*/
abstract public function getObject($key = null, $data = null, $new = false);
/**
* @param \ext\activedocument\Adapter $adapter
* @param string $name
*/
public function __construct(Adapter $adapter, $name) {
$this->_adapter = $adapter;
$this->_connection = $adapter->getConnection();
$this->_name = $name;
$this->initContainer();
}
protected function initContainer() {
$this->_containerInstance = $this->loadContainerInstance();
$this->_properties = $this->loadProperties();
}
/**
* @return \ext\activedocument\Connection
*/
public function getConnection() {
return $this->_connection;
}
/**
* @return \ext\activedocument\Adapter
*/
public function getAdapter() {
return $this->_adapter;
}
public function getContainerInstance() {
return $this->_containerInstance;
}
/**
* @return string
*/
public function getName() {
return $this->_name;
}
/**
* @param string $key
* @param mixed $value
*/
public function setProperty($key, $value) {
$this->_properties[$key] = $value;
}
/**
* @param string $key
* @return mixed|null
*/
public function getProperty($key) {
if (!array_key_exists($key, $this->_properties))
return null;
return $this->_properties[$key];
}
/**
* @return array
*/
public function getProperties() {
return $this->_properties;
}
/**
* @param array $properties
*/
public function setProperties(array $properties) {
$this->_properties = $properties;
foreach ($this->_properties as $k => $v)
$this->setProperty($k, $v);
}
/**
* @param array $config
*/
public function setConfig(array $config) {
$this->setProperties($config);
}
/**
* @param \ext\activedocument\Criteria|null $criteria optional
* @return int
*/
public function count(Criteria $criteria=null) {
if ($criteria === null)
$criteria = new Criteria;
$criteria->container = $this->_name;
return $this->_adapter->count($criteria);
}
/**
* @param \ext\activedocument\Criteria|null $criteria optional
* @return \ext\activedocument\Object[]
*/
public function find(Criteria $criteria=null) {
if ($criteria === null)
$criteria = new Criteria;
$criteria->container = $this->_name;
return $this->_adapter->find($criteria);
}
/**
* @param string $key
* @param mixed $data
* @return \ext\activedocument\Object
*/
public function createObject($key=null, $data=null) {
return $this->getObject($key, $data, true);
}
}