ZendDbSchema
Schema management for Zend Framework
 All Classes Namespaces Functions Pages
AbstractSchema.php
1 <?php
30  implements IteratorAggregate
31 {
32  const NAME_KEY = 'name';
33 
39  protected $_params = array();
40 
44  protected $_name;
45 
51  abstract protected function _getCleanSchema();
52 
58  abstract protected function _getAlterSql();
59 
65  abstract protected function _getCreateSql();
66 
72  abstract protected function _getDropSql();
73 
79  public function isExist()
80  {
81  $schema = $this->getCleanSchema();
82  return !empty($schema);
83  }
84 
91  public function isDirty($param = null)
92  {
93  $clean = $this->getCleanSchema();
94 
95  if ($param) {
96  if (!array_key_exists($param, $clean)) {
97  return $this->__isset($param);
98  }
99  return $this->getCleanSchema($param) != $this->__get($param);
100  }
101 
102  if (count($clean) != count($this->_params)) {
103  return true;
104  }
105  foreach ($this->_params as $paramName => $paramValue) {
106  if ($this->isDirty($paramName)) {
107  return true;
108  }
109  }
110 
111  return false;
112  }
113 
120  public function getCleanSchema($param = null)
121  {
122  $schema = $this->_getCleanSchema();
123  if ($param) {
124  if (isset($schema[$param])) {
125  return $schema[$param];
126  }
127  return null;
128  }
129  return $schema;
130  }
131 
138  public function refresh($param = null)
139  {
140  if ($param) {
141  $this->__set($param, $this->getCleanSchema($param));
142  } else {
143  $this->setFromArray($this->getCleanSchema());
144  }
145  return $this;
146  }
147 
154  public function __construct($name = null)
155  {
156  if ($name) {
157  $this->_name = (string) $name;
158  }
159 
160  $this->refresh();
161 
162  $this->init();
163  }
164 
172  public function init()
173  {
174  }
175 
181  public function __toString()
182  {
183  return $this->toSql();
184  }
185 
193  public function __get($name)
194  {
195  if (!$this->__isset($name)) {
196  return null;
197  }
198  return $this->_params[$name];
199  }
200 
207  public function __isset($name)
208  {
209  return array_key_exists($name, $this->_params);
210  }
211 
218  public function __set($name, $value)
219  {
220  $this->_params[$name] = $value;
221  }
222 
230  public function __unset($name)
231  {
232  if ($this->__isset($name)) {
233  unset($this->_params[$name]);
234  }
235  }
236 
241  public function __clone()
242  {
243  $this->_name = null;
244 
245  $this->__set(self::NAME_KEY, null);
246 
247  $this->reload();
248  }
249 
257  public function getIterator()
258  {
259  return new ArrayIterator($this->toArray());
260  }
261 
268  public function setFromArray(array $schema)
269  {
270  foreach ($schema as $keyName => $value) {
271  $this->__set($keyName, $value);
272  }
273 
274  return $this;
275  }
276 
282  public function toArray()
283  {
284  return $this->_params;
285  }
286 
293  public function setName($name)
294  {
295  $this->__set(self::NAME_KEY, (string) $name);
296  return $this;
297  }
298 
304  public function getName()
305  {
306  return $this->__get(self::NAME_KEY);
307  }
308 
314  public function getOriginName()
315  {
316  if ($this->_name) {
317  return $this->_name;
318  }
319  return $this->getName();
320  }
321 
327  public function getDropSql()
328  {
329  if (!$this->isExist()) {
330  throw new ZendDbSchema_Db_Schema_Exception('Definition is not exist');
331  }
332  return $this->_getDropSql();
333  }
334 
341  public function toSql($alter = false)
342  {
343  if ($alter) {
344  if ($this->isDirty() && $this->isExist()) {
345  return $this->_getAlterSql();
346  }
347  return '';
348  }
349  return $this->_getCreateSql();
350  }
351 }