ZendDbSchema
Schema management for Zend Framework
 All Classes Namespaces Functions Pages
AbstractManager.php
1 <?php
30  implements IteratorAggregate, Countable
31 {
35  protected $_data = array();
36 
40  protected $_table;
41 
47  abstract protected function _getItemClass();
48 
54  abstract protected function _getSchemaKey();
55 
62  public function __construct(ZendDbSchema_Db_Schema_Table $table)
63  {
64  $this->_table = $table;
65 
66  $this->refresh();
67  }
68 
74  public function refresh()
75  {
76  $itemClass = $this->_getItemClass();
77 
78  $key = $this->_getSchemaKey();
79 
80  $data = $this->_table->getCleanSchema();
81  $schema = array();
82  if (!empty($data[$key])) {
83  $schema = $data[$key];
84  }
85  $this->setFromArray($schema);
86  return $this;
87  }
88 
95  public function setFromArray(array $config)
96  {
97  $classname = $this->_getItemClass();
98 
99  foreach ($config as $itemName => $itemConfig) {
100  if (!$this->has($itemName)) {
101  $this->add(new $classname($itemName, $this->_table));
102  }
103  $this->get($itemName)->setFromArray($itemConfig);
104  }
105  foreach ($this->_data as $i => $item) {
106  if (!isset($config[$item->getOriginName()])) {
107  $item->markDeleted();
108  }
109  }
110  return $this;
111  }
112 
120  {
121  $classname = $this->_getItemClass();
122  if (!$item instanceof $classname) {
124  "Definition must be instance of '{$classname}'"
125  );
126  }
127  $name = $item->getOriginName();
128  if ($name && $this->has($name)) {
130  "Definition by name '$name' already exists in " . get_class($this)
131  );
132  }
133  $item = new $classname($name, $this->_table);
134 
135  $this->_data[] = $item;
136 
137  return $this;
138  }
139 
145  public function isDirty()
146  {
147  foreach ($this->_data as $item) {
148  if ($item->isDirty()) {
149  return true;
150  }
151  }
152  return false;
153  }
154 
160  public function toArray()
161  {
162  $data = array();
163  foreach ($this->_data as $item) {
164  $data[$item->getName()] = $item->toArray();
165  }
166  return $data;
167  }
168 
174  public function getIterator()
175  {
176  return new ArrayIterator($this->_data);
177  }
178 
185  public function has($name)
186  {
187  return null !== $this->get($name);
188  }
189 
196  public function get($name)
197  {
198  foreach ($this->_data as $i => $item) {
199  if ($item->getName() == $name) {
200  return $item;
201  }
202  }
203  return null;
204  }
205 
211  public function count()
212  {
213  return count($this->_data);
214  }
215 }