AbstractRepository.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-10-06)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace MGProvision\Proxmox\v2\repository;
  20. /**
  21. * Description of AbstractRepository
  22. *
  23. * @author Pawel Kopec <pawelk@modulesgarden.com>
  24. * @version 1.0.0
  25. */
  26. Abstract class AbstractRepository
  27. {
  28. protected $api;
  29. protected $fetch;
  30. public function setApi($api)
  31. {
  32. $this->api = $api;
  33. }
  34. /**
  35. *
  36. * @return \MGProvision\Proxmox\v2\Api
  37. */
  38. protected function api()
  39. {
  40. if ($this->api !== null)
  41. return $this->api;
  42. return $this->api = \MGProvision\Proxmox\v2\Api::getInstance();
  43. }
  44. abstract public function fetch();
  45. public function fetchArray()
  46. {
  47. $data = array();
  48. foreach ($this->fetch() as $entity)
  49. {
  50. $data[] = $entity->toArray();
  51. }
  52. return $data;
  53. }
  54. public function reset()
  55. {
  56. $this->fetch = null;
  57. return $this;
  58. }
  59. public function count()
  60. {
  61. return count((array)$this->fetch());
  62. }
  63. }