ModelProxy::getTableField()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace N7olkachev\ComputedProperties;
4
5
use Illuminate\Database\Query\Expression;
6
7
class ModelProxy
8
{
9
    protected $model;
10
11
    protected $runningInQuery;
12
13
    public function __construct($model, $runningInQuery)
14
    {
15
        $this->model = $model;
16
        $this->runningInQuery = $runningInQuery;
17
    }
18
19
    public function __get($key)
20
    {
21
        return $this->runningInQuery ? $this->getTableField($key) : $this->getModelAttribute($key);
22
    }
23
24
    protected function getTableField($key)
25
    {
26
        return new Expression($this->model->getTable() . '.' . $key);
27
    }
28
29
    protected function getModelAttribute($key)
30
    {
31
        return $this->model->$key;
32
    }
33
}
34