for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Rougin\Wildfire\Traits;
/**
* Object Trait
*
* @package Wildfire
* @author Rougin Royce Gutib <[email protected]>
* @property \Rougin\Describe\Describe $describe
*/
trait ObjectTrait
{
* @var array
protected $tables = [];
* Creates an object from the specified table and row.
* @param string $table
* @param object $row
* @return array
protected function createObject($table, $row)
$newTable = ucfirst(singular($table));
$model = new $newTable;
if ( ! array_key_exists($newTable, $this->tables)) {
$tableInfo = $this->describe->getTable($newTable);
$this->tables[$newTable] = $tableInfo;
} else {
$tableInfo = $this->tables[$newTable];
}
foreach ($row as $key => $value) {
foreach ($tableInfo as $column) {
if ($column->getField() != $key) {
continue;
$model->$key = $value;
if ($column->isForeignKey()) {
$foreignColumn = $column->getReferencedField();
$foreignTable = $column->getReferencedTable();
$delimiters = [ $foreignColumn => $value ];
$foreignData = $this->find($foreignTable, $delimiters);
$newColumn = singular($foreignTable);
$model->$newColumn = $foreignData;
return $model;
* Finds the row from the specified ID or with the list of delimiters from
* the specified table.
* @param array|integer $delimiters
* @return object|boolean
abstract protected function find($table, $delimiters = []);