$v) { $obj->{$k}($v); } return $obj; } /** * Defines a generic constructor to extract information of the object. * * @param string $table Table. * @param array|null $filters Filters, for instance ['id' => $id]. * @param string|null $enterprise_class Enterprise class name. * * @throws \Exception On error. */ public function __construct( string $table, ?array $filters=null, ?string $enterprise_class=null ) { if (empty($table) === true) { throw new \Exception( get_class($this).' error, table name is not defined' ); } $this->table = $table; if (is_array($filters) === true) { // New one. $data = \db_get_row_filter($this->table, $filters); if ($data === false) { throw new \Exception( get_class($this).' error, entity not found' ); } // Map fields. foreach ($data as $k => $v) { $this->fields[$k] = $v; } } else { // Empty one. $data = \db_get_all_rows_sql( sprintf( 'SHOW COLUMNS FROM %s', $this->table ) ); foreach ($data as $row) { $this->fields[$row['Field']] = null; } } if (\enterprise_installed() === true && $enterprise_class !== null ) { $this->enterprise = new $enterprise_class($this); } } /** * Dynamically call methods in this object. * * @param string $methodName Name of target method or attribute. * @param array $params Arguments for target method. * * @return mixed Return of method. * @throws \Exception On error. */ public function __call(string $methodName, ?array $params=null) { // Prioritize written methods over dynamic ones. if (method_exists($this, $methodName) === true) { return call_user_func_array( $this->{$methodName}, $params ); } // Enterprise capabilities. if (\enterprise_installed() === true && $this->enterprise !== null && method_exists($this->enterprise, $methodName) === true ) { return call_user_func_array( [ $this->enterprise, $methodName, ], $params ); } if (array_key_exists($methodName, $this->fields) === true) { if (empty($params) === true) { return $this->fields[$methodName]; } else { $this->fields[$methodName] = $params[0]; } return null; } throw new \Exception( get_class($this).' error, method '.$methodName.' does not exist' ); } /** * Returns current object as array. * * @return array Of fields. */ public function toArray() { return $this->fields; } /** * Connects to current nodeId target. * If no nodeId is defined, then returns without doing anything. * * @return void * @throws \Exception On error. */ public function connectNode() { if ($this->nodeId === null) { return; } \enterprise_include_once('include/functions_metaconsole.php'); $r = \enterprise_hook( 'metaconsole_connect', [ null, $this->nodeId, ] ); if ($r !== NOERR) { throw new \Exception( __('Cannot connect to node %d', $this->nodeId) ); } $this->connected = true; } /** * Restore connection after connectNode. * * @return void */ public function restoreConnection() { if ($this->connected === true) { \enterprise_include_once('include/functions_metaconsole.php'); \enterprise_hook('metaconsole_restore_db'); } } /** * Saves current object definition to database. * * @return boolean Success or not. */ public abstract function save(); }