PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/Syncroton/lib/Syncroton/Backend/ABackend.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 203 lines | 98 code | 36 blank | 69 comment | 9 complexity | 067c68a6ecacbff9accc73ceabaf0f23 MD5 | raw file
  1. <?php
  2. /**
  3. * Syncroton
  4. *
  5. * @package Syncroton
  6. * @subpackage Backend
  7. * @license http://www.tine20.org/licenses/lgpl.html LGPL Version 3
  8. * @copyright Copyright (c) 2012-2012 Metaways Infosystems GmbH (http://www.metaways.de)
  9. * @author Lars Kneschke <l.kneschke@metaways.de>
  10. */
  11. /**
  12. * class to handle ActiveSync Sync command
  13. *
  14. * @package Syncroton
  15. * @subpackage Backend
  16. */
  17. abstract class Syncroton_Backend_ABackend implements Syncroton_Backend_IBackend
  18. {
  19. /**
  20. * the database adapter
  21. *
  22. * @var Zend_Db_Adapter_Abstract
  23. */
  24. protected $_db;
  25. protected $_tablePrefix;
  26. protected $_tableName;
  27. protected $_modelClassName;
  28. protected $_modelInterfaceName;
  29. /**
  30. * the constructor
  31. *
  32. * @param Zend_Db_Adapter_Abstract $_db
  33. * @param string $_tablePrefix
  34. */
  35. public function __construct(Zend_Db_Adapter_Abstract $_db, $_tablePrefix = 'Syncroton_')
  36. {
  37. $this->_db = $_db;
  38. $this->_tablePrefix = $_tablePrefix;
  39. }
  40. /**
  41. * create new device
  42. *
  43. * @param Syncroton_Model_IDevice $_device
  44. * @return Syncroton_Model_IDevice
  45. */
  46. public function create($model)
  47. {
  48. if (! $model instanceof $this->_modelInterfaceName) {
  49. throw new InvalidArgumentException('$model must be instanace of ' . $this->_modelInterfaceName);
  50. }
  51. $data = $this->_convertModelToArray($model);
  52. $data['id'] = sha1(mt_rand(). microtime());
  53. $this->_db->insert($this->_tablePrefix . $this->_tableName, $data);
  54. return $this->get($data['id']);
  55. }
  56. /**
  57. * convert iteratable object to array
  58. *
  59. * @param unknown $model
  60. * @return array
  61. */
  62. protected function _convertModelToArray($model)
  63. {
  64. $data = array();
  65. foreach ($model as $key => $value) {
  66. if ($value instanceof DateTime) {
  67. $value = $value->format('Y-m-d H:i:s');
  68. } elseif (is_object($value) && isset($value->id)) {
  69. $value = $value->id;
  70. }
  71. $data[$this->_fromCamelCase($key)] = $value;
  72. }
  73. return $data;
  74. }
  75. /**
  76. * @param string $_id
  77. * @throws Syncroton_Exception_NotFound
  78. * @return Syncroton_Model_IDevice
  79. */
  80. public function get($id)
  81. {
  82. $id = $id instanceof $this->_modelInterfaceName ? $id->id : $id;
  83. if (empty($id)) {
  84. throw new Syncroton_Exception_NotFound('id can not be empty');
  85. }
  86. try {
  87. $select = $this->_db->select()
  88. ->from($this->_tablePrefix . $this->_tableName)
  89. ->where('id = ?', $id);
  90. $stmt = $this->_db->query($select);
  91. $data = $stmt->fetch();
  92. $stmt = null; # see https://bugs.php.net/bug.php?id=44081
  93. } catch (Zend_Db_Statement_Exception $zdbse) {
  94. throw new Syncroton_Exception_NotFound('id not found');
  95. }
  96. if ($data === false) {
  97. throw new Syncroton_Exception_NotFound('id not found');
  98. }
  99. return $this->_getObject($data);
  100. }
  101. /**
  102. * convert array to object
  103. *
  104. * @param array $data
  105. * @return object
  106. */
  107. protected function _getObject($data)
  108. {
  109. foreach ($data as $key => $value) {
  110. unset($data[$key]);
  111. if (!empty($value) && preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $value)) { # 2012-08-12 07:43:26
  112. $value = new DateTime($value, new DateTimeZone('utc'));
  113. }
  114. $data[$this->_toCamelCase($key, false)] = $value;
  115. }
  116. return new $this->_modelClassName($data);
  117. }
  118. /**
  119. * (non-PHPdoc)
  120. * @see Syncroton_Backend_IBackend::delete()
  121. */
  122. public function delete($id)
  123. {
  124. $id = $id instanceof $this->_modelInterfaceName ? $id->id : $id;
  125. $result = $this->_db->delete($this->_tablePrefix . $this->_tableName, array('id = ?' => $id));
  126. return (bool) $result;
  127. }
  128. /**
  129. * (non-PHPdoc)
  130. * @see Syncroton_Backend_IBackend::update()
  131. */
  132. public function update($model)
  133. {
  134. if (! $model instanceof $this->_modelInterfaceName) {
  135. throw new InvalidArgumentException('$model must be instanace of ' . $this->_modelInterfaceName);
  136. }
  137. $data = $this->_convertModelToArray($model);
  138. $this->_db->update($this->_tablePrefix . $this->_tableName, $data, array(
  139. 'id = ?' => $model->id
  140. ));
  141. return $this->get($model->id);
  142. }
  143. /**
  144. * convert from camelCase to camel_case
  145. * @param string $string
  146. * @return string
  147. */
  148. protected function _fromCamelCase($string)
  149. {
  150. $string = lcfirst($string);
  151. return preg_replace_callback('/([A-Z])/', function ($string) {return '_' . strtolower($string[0]);}, $string);
  152. }
  153. /**
  154. * convert from camel_case to camelCase
  155. *
  156. * @param string $string
  157. * @param bool $ucFirst
  158. * @return string
  159. */
  160. protected function _toCamelCase($string, $ucFirst = true)
  161. {
  162. if ($ucFirst === true) {
  163. $string = ucfirst($string);
  164. }
  165. return preg_replace_callback('/_([a-z])/', function ($string) {return strtoupper($string[1]);}, $string);
  166. }
  167. }