AbstractObject.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-10-11)
  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\models;
  20. use MGProvision\Proxmox\v2\Api;
  21. /**
  22. * Description of AbstractObject
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgarden.com>
  25. * @version 1.0.0
  26. */
  27. abstract class AbstractObject
  28. {
  29. protected $api;
  30. public function setAttributes($attributes)
  31. {
  32. foreach ($attributes as $name => $attValue)
  33. {
  34. $methodName = 'set' . ucfirst($name);
  35. if (method_exists($this, $methodName))
  36. $this->{$methodName}($attValue);
  37. }
  38. return $this;
  39. }
  40. public function setApi($api)
  41. {
  42. $this->api = $api;
  43. return $this;
  44. }
  45. /**
  46. *
  47. * @return \MGProvision\Proxmox\v2\Api
  48. */
  49. protected function api()
  50. {
  51. if ($this->api !== null)
  52. return $this->api;
  53. return $this->api = \MGProvision\Proxmox\v2\Api::getInstance();
  54. }
  55. /**
  56. * @return Api
  57. */
  58. public function getApi(){
  59. return $this->api;
  60. }
  61. public static function asArray($proxmoxConfig)
  62. {
  63. $result = array();
  64. $config = explode(",", $proxmoxConfig);
  65. foreach ($config as $c)
  66. {
  67. list($key, $val) = explode("=", $c);
  68. $result[$key] = $val;
  69. }
  70. return $result;
  71. }
  72. public function toArray()
  73. {
  74. if (method_exists($this, 'getAttributes'))
  75. {
  76. return $this->getAttributes();
  77. }
  78. $fields = get_class_vars(get_called_class());
  79. $data = array();
  80. foreach ($fields as $name => $defult)
  81. {
  82. $methodName = 'get' . ucfirst($name);
  83. if (method_exists($this, $methodName))
  84. {
  85. $data[$name] = $this->{$methodName}();
  86. }
  87. }
  88. return $data;
  89. }
  90. public function toConfig($fields, &$config)
  91. {
  92. foreach ($fields as $field)
  93. {
  94. $methodName = 'get' . ucfirst($field);
  95. if (method_exists($this, $methodName) && ( $this->{$methodName}() || $this->{$methodName}() ===0 || $this->{$methodName}() ==="0") )
  96. {
  97. $config[] = "{$field}=" . $this->{$methodName}();
  98. }
  99. }
  100. }
  101. public function toString()
  102. {
  103. return print_r($this->getAttributes(), true);
  104. }
  105. public function getHashCode()
  106. {
  107. return hash('ripemd160',$this->toString());
  108. }
  109. }