PageRenderTime 23ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/core/classes/DTO.php

http://github.com/caferrari/vorticephp
PHP | 155 lines | 73 code | 13 blank | 69 comment | 13 complexity | b6e4dcabf20a730034338592fee71cac MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright (c) 2008, Carlos André Ferrari <[carlos@]ferrari.eti.br>; Luan Almeida <[luan@]luan.eti.br>
  4. * All rights reserved.
  5. */
  6. /**
  7. * DTO class, Base for records objects
  8. *
  9. * @version 1
  10. * @package Database
  11. * @author Luan Almeida <luanlmd@gmail.com>
  12. */
  13. class DTO
  14. {
  15. protected static $dbInstance = 'default';
  16. /**
  17. * Set a record value
  18. *
  19. * @param string $name new Field name
  20. * @param string|mixed $value new Field name
  21. * @return array
  22. */
  23. public function __set($name, $value){
  24. if (method_exists($this, $name)){
  25. $metodo = 'set' . ucfirst($name);
  26. $this->$metodo($value);
  27. }else
  28. $this->$name = $value;
  29. }
  30. /**
  31. * Get a record value
  32. *
  33. * @param string $name Field name
  34. * @return array
  35. */
  36. public function __get($name){
  37. if (isset($this->$name))
  38. return $this->$name;
  39. if ($this->$name == NULL)
  40. return '';
  41. throw new UndefinedPropertyException($name);
  42. }
  43. /**
  44. * Escape the object params
  45. *
  46. * @return void
  47. */
  48. public static function escape(){
  49. if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) return;
  50. foreach (get_object_vars($this) as $p) $this->$p = addslashes($this->$p);
  51. }
  52. /**
  53. * Unescape the object params
  54. *
  55. * @return void
  56. */
  57. public static function unescape(){
  58. foreach (get_object_vars($this) as $p) $this->$p = stripslashes($this->$p);
  59. }
  60. /**
  61. * Create an array with the given properties
  62. *
  63. * @return array
  64. */
  65. public function toArray($fields=null){
  66. $fields = explode(',', preg_replace('@[ ]+@', '', $fields));
  67. $tmp = array();
  68. foreach ($fields as $f){
  69. if (isset($this->$f)) $tmp[] = $this->$f;
  70. else $tmp[]=null;
  71. }
  72. return $tmp;
  73. }
  74. /**
  75. * Insert data into the Database
  76. *
  77. * @return int
  78. */
  79. public function insert($fields, $table=null, $instance='default'){
  80. //$table = uncamelize(($table == null) ? (isset($this->_table) ? $this->_table : get_class($this)) : $table);
  81. $table = uncamelize(($table ? $table : get_called_class()));
  82. $values = substr(str_repeat("?,", count(explode(",", $fields))), 0, -1);
  83. $sql = 'INSERT INTO ' . $table . ' (' . $fields . ') VALUES (' . $values . ');';
  84. $id = Database::getInstance($instance)->exec($sql, $this->toArray($fields));
  85. return $id;
  86. }
  87. /**
  88. * Update data in the database
  89. *
  90. * @return dto
  91. */
  92. public function save($fields, $table=null, $instance='default'){
  93. if (!isset($this->id) || !is_numeric($this->id)) return $this->insert($fields, $table, $instance);
  94. //$table = uncamelize(($table == null) ? (isset($this->_table) ? $this->_table : get_class($this)) : $table);
  95. $table = uncamelize(($table ? $table : get_called_class()));
  96. $id = $this->id;
  97. unset($this->id);
  98. $values = $this->toArray($fields);
  99. $values[] = $this->id = $id;
  100. $tmp = array();
  101. $fields = explode(",", $fields);
  102. foreach ($fields as $k=>$f) $tmp[] = $fields[$k] . '=?';
  103. $sql = 'UPDATE ' . $table . ' SET ' . implode($tmp, ', ') . ' WHERE id=?';
  104. Database::getInstance($instance)->exec($sql, $values);
  105. return $this;
  106. }
  107. /**
  108. * Delete data from the database
  109. *
  110. * @return int
  111. */
  112. public function delete($table=null, $instance='default'){
  113. //$table = uncamelize(($table == null) ? (isset($this->_table) ? $this->_table : get_class($this)) : $table);
  114. $table = uncamelize(($table ? $table : get_called_class()));
  115. if (!isset($this->id) || !is_numeric($this->id)) return false;
  116. return Database::getInstance($instance)->exec('DELETE FROM ' . $table . ' WHERE id=?', array($this->id));
  117. }
  118. /**
  119. * List the table data
  120. *
  121. * @return array
  122. */
  123. public static function all($table=null, $instance='default'){
  124. //$table = uncamelize(($table == null) ? (isset($this->_table) ? $this->_table : preg_replace('@Controller$@', '', get_class($this))) : $table);
  125. $table = uncamelize(($table ? $table : get_called_class()));
  126. $class = camelize($table);
  127. return Database::getInstance($instance)->query('SELECT * FROM ' . $table, $class);
  128. }
  129. /**
  130. * Load a record data
  131. *
  132. * @return dto
  133. */
  134. public static function load($id, $table=null, $instance='default'){
  135. if (!is_numeric($id)) throw new IntegerRequiredException('id must be an integer');
  136. //$table = uncamelize(($table == null) ? (isset($this->_table) ? $this->_table : preg_replace('@Controller$@', '', get_class($this))) : $table);
  137. $table = uncamelize(($table ? $table : get_called_class()));
  138. $class = camelize($table);
  139. $sql = "SELECT * FROM $table WHERE id=$id";
  140. return Database::getInstance($instance)->queryOne($sql, $class);
  141. }
  142. }