Completed
Push — master ( 8004d0...4e5693 )
by Rougin
02:29
created

ObjectTrait::setModelFields()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 8.8571
cc 6
eloc 9
nc 9
nop 4
crap 6
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
use Rougin\Describe\Column;
6
7
use Rougin\Wildfire\CodeigniterModel;
8
use Rougin\Wildfire\Helpers\ModelHelper;
9
use Rougin\Wildfire\Helpers\TableHelper;
10
11
/**
12
 * Object Trait
13
 *
14
 * @package Wildfire
15
 * @author  Rougin Royce Gutib <[email protected]>
16
 *
17
 * @property \Rougin\Describe\Describe $describe
18
 * @property string                    $table
19
 */
20
trait ObjectTrait
21
{
22
    /**
23
     * Creates an object from the specified table and row.
24
     *
25
     * @param  string|\Rougin\Wildfire\CodeigniterModel $table
26
     * @param  object                                   $row
27
     * @return array
28
     */
29 30
    protected function createObject($table, $row)
30
    {
31 30
        list($tableName, $model) = ModelHelper::createInstance($table);
32
33 30
        $properties = [];
34
35 30
        if ($model instanceof CodeigniterModel) {
36 30
            $properties = $model->getProperties();
37 30
            $properties = $model->getRelationshipProperties($properties);
38 30
        }
39
40 30
        $tableInfo = $this->describe->getTable($tableName);
41
42 30
        $this->setModelFields($model, $row, $properties, $tableInfo);
43
44 30
        return $model;
45
    }
46
47
    /**
48
     * Finds the row from the specified ID or with the list of delimiters from
49
     * the specified table.
50
     *
51
     * @param  string         $table
52
     * @param  array|integer  $delimiters
53
     * @return object|boolean
54
     */
55
    abstract protected function find($table, $delimiters = []);
56
57
    /**
58
     * Sets the foreign field of the column, if any.
59
     *
60
     * @param  \CI_Model               $model
61
     * @param  \Rougin\Describe\Column $column
62
     * @param  array                   $properties
63
     * @return void
64
     */
65 30
    protected function setForeignField(\CI_Model $model, Column $column, array $properties)
66
    {
67 30
        if (! $column->isForeignKey()) {
68 30
            return;
69
        }
70
71 18
        $columnName = $column->getField();
72
73 18
        $foreignColumn = $column->getReferencedField();
74 18
        $foreignTable  = $column->getReferencedTable();
75
76 18
        if (in_array($foreignTable, $properties['belongs_to'])) {
77 3
            $delimiters = [ $foreignColumn => $model->$columnName ];
78 3
            $tableName  = TableHelper::getModelName($foreignTable);
79
80 3
            $model->$tableName = $this->find($foreignTable, $delimiters);
81 3
        }
82 18
    }
83
84
    /**
85
     * Sets the model values based on the result row.
86
     *
87
     * @param  \CI_Model &$model
88
     * @param  object    $row
89
     * @param  array     $properties
90
     * @param  array     $tableInformation
91
     * @return void
92
     */
93 30
    protected function setModelFields(&$model, $row, $properties, $tableInformation)
94
    {
95 30
        foreach ($tableInformation as $column) {
96 30
            $key = $column->getField();
97
98 30
            $inColumns = ! empty($properties['columns']) && ! in_array($key, $properties['columns']);
99 30
            $inHiddenColumns = ! empty($properties['hidden']) && in_array($key, $properties['hidden']);
100
101 30
            if ($inColumns || $inHiddenColumns) {
102 30
                continue;
103
            }
104
105 30
            $model->$key = $row->$key;
106
107 30
            $this->setForeignField($model, $column, $properties);
108 30
        }
109 30
    }
110
}
111