/** * Transforms Record to two-dimensional array of tables and fields. * * @param Record $record * * @return array */ public function tabelizeRecord(Record $record) { $dataArray = $record->__toArray(null, 1, false); $extensionArray = []; /** * Holds all available fields in database table cache. */ $keys = [$this->table => $this->repository->getCache()->getTableFields($this->table)]; foreach (get_class_methods($this) as $method) { /** * Get extension's fields. */ if ($method != 'getFields' && substr($method, 0, 3) == 'get' && substr($method, -6) == 'Fields') { $suffix = $this->{'get' . substr($method, 3, -6) . 'TableSuffix'}(); if (substr($this->table, strlen($this->table) - strlen($suffix)) != $suffix && $this->repository->getCache()->hasTable($this->table . $suffix)) { $keys[$this->table . $suffix] = $this->{$method}(); } } /** * Get extension's foreign key values. */ if ($method != 'getForeignKeys' && substr($method, 0, 3) == 'get' && substr($method, -11) == 'ForeignKeys') { $suffix = $this->{'get' . substr($method, 3, -11) . 'TableSuffix'}(); if (substr($this->table, strlen($this->table) - strlen($suffix)) != $suffix && $this->repository->getCache()->hasTable($this->table . $suffix)) { // base table $extensionArray[$this->table . $suffix] = $this->{$method}($record); } elseif (strrpos($this->table, $suffix) == strlen($this->table) - strlen($suffix) && $this->repository->getCache()->hasTable($this->table)) { // extendee table $extensionArray[$this->table] = $this->{$method}($record); } } } // fill array with tables and fields $values = []; foreach ($keys as $table => $fields) { foreach ($fields as $field) { /** * Add value if field exists in data array and repository has that field. */ if ($this->repository->getCache()->tableHasField($table, $field)) { if (isset($extensionArray[$table]) && array_key_exists($field, $extensionArray[$table])) { $values[$table][$field] = $extensionArray[$table][$field]; } elseif (array_key_exists($field, $dataArray)) { $values[$table][$field] = $dataArray[$field]; } } } } return $values; }