http://modulesgarden.com * CONTACT -> contact@modulesgarden.com * * * This software is furnished under a license and may be used and copied * only in accordance with the terms of such license and with the * inclusion of the above copyright notice. This software or any other * copies thereof may not be provided or otherwise made available to any * other person. No title to and ownership of the software is hereby * transferred. * * * ******************************************************************** */ namespace MGProvision\Proxmox\v2\models; /** * Description of AbstractObject * * @author Pawel Kopec * @version 1.0.0 */ abstract class AbstractObject { protected $api; public function setAttributes($attributes) { foreach ($attributes as $name => $attValue) { $methodName = 'set' . ucfirst($name); if (method_exists($this, $methodName)) $this->{$methodName}($attValue); } return $this; } public function setApi($api) { $this->api = $api; return $this; } /** * * @return \MGProvision\Proxmox\v2\Api */ protected function api() { if ($this->api !== null) return $this->api; return $this->api = \MGProvision\Proxmox\v2\Api::getInstance(); } public static function asArray($proxmoxConfig) { $result = array(); $config = explode(",", $proxmoxConfig); foreach ($config as $c) { list($key, $val) = explode("=", $c); $result[$key] = $val; } return $result; } public function toArray() { if (method_exists($this, 'getAttributes')) { return $this->getAttributes(); } $fields = get_class_vars(get_called_class()); $data = array(); foreach ($fields as $name => $defult) { $methodName = 'get' . ucfirst($name); if (method_exists($this, $methodName)) { $data[$name] = $this->{$methodName}(); } } return $data; } public function toConfig($fields, &$config) { foreach ($fields as $field) { $methodName = 'get' . ucfirst($field); if (method_exists($this, $methodName) && ( $this->{$methodName}() || $this->{$methodName}() ===0 || $this->{$methodName}() ==="0") ) { $config[] = "{$field}=" . $this->{$methodName}(); } } } public function toString() { return print_r($this->getAttributes(), true); } public function getHashCode() { return hash('ripemd160',$this->toString()); } }