PageRenderTime 42ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/experimental/couch/CouchDocument.php

http://zoop.googlecode.com/
PHP | 94 lines | 78 code | 16 blank | 0 comment | 9 complexity | 8e4ae6fed4ab860e099cfeed893d9756 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. class CouchDocument
  3. {
  4. private $id;
  5. private $rev;
  6. private $data;
  7. static function findAll()
  8. {
  9. return CouchModule::getConnection(self::getConnectionName())->getAllDocuments();
  10. }
  11. function __construct($id, $rev = null)
  12. {
  13. $this->id = $id;
  14. $this->rev = $rev;
  15. }
  16. static private function getConnectionName()
  17. {
  18. return 'default';
  19. }
  20. private function getDb()
  21. {
  22. return CouchModule::getConnection(self::getConnectionName());
  23. }
  24. public function getId()
  25. {
  26. return $this->id;
  27. }
  28. public function getRev()
  29. {
  30. return $this->rev;
  31. }
  32. public function load()
  33. {
  34. $dbName = $this->getDb()->getdbName();
  35. $resp = $this->getDb()->getHttp()->send("GET", "/$dbName/{$this->id}");
  36. foreach($resp as $key => $value)
  37. {
  38. if($key == '_id')
  39. $this->id = $value;
  40. else if($key == '_rev')
  41. $this->rev = $value;
  42. else
  43. {
  44. $this->data->$key = $value;
  45. }
  46. }
  47. }
  48. public function save()
  49. {
  50. $dbName = $this->getDb()->getDbName();
  51. $data->_id = $this->id;
  52. if($this->rev)
  53. $data->_rev = $this->rev;
  54. foreach($this->data as $key => $val)
  55. {
  56. $data->$key = $val;
  57. }
  58. $data = json_encode($data);
  59. $resp = $this->getDb()->getHttp()->send("PUT", "/$dbName/$this->id", $data);
  60. if($resp->ok != true)
  61. trigger_error("saving document '{$this->id}:{$this->rev}' failed");
  62. $this->rev = $resp->rev;
  63. }
  64. public function getData()
  65. {
  66. return $this->data();
  67. }
  68. function __get($varname)
  69. {
  70. if(!isset($this->data->$varname))
  71. trigger_error("field does not exist: $varname");
  72. return $this->data->$varname;
  73. }
  74. function __set($varname, $value)
  75. {
  76. $this->data->$varname = $value;
  77. }
  78. }