AbstractObject.php 2.9 KB

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