FieldModel::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Hechoenlaravel\JarvisFoundation\FieldGenerator;
4
5
use Hechoenlaravel\JarvisFoundation\FieldGenerator\Transformers\FieldTransformer;
6
use Illuminate\Database\Eloquent\Model;
7
use League\Fractal\Manager;
8
use League\Fractal\Resource\Item;
9
10
/**
11
 * Class FieldModel
12
 * @package Hechoenlaravel\JarvisFoundation\FieldGenerator
13
 */
14
class FieldModel extends Model
15
{
16
    /**
17
     * database Table
18
     * @var string
19
     */
20
    protected $table = "app_entities_fields";
21
22
    /**
23
     * Fillable columns
24
     * @var array
25
     */
26
    protected $fillable = [
27
        'entity_id',
28
        'namespace',
29
        'name',
30
        'slug',
31
        'description',
32
        'type',
33
        'default',
34
        'required',
35
        'locked',
36
        'create_field',
37
        'options',
38
        'order'
39
    ];
40
41
    /**
42
     * Related entity
43
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
44
     */
45
    public function entity()
46
    {
47
        return $this->belongsTo('Hechoenlaravel\JarvisFoundation\EntityGenerator\EntityModel');
48
    }
49
50
    /**
51
     * Returns a FieldType Class for the field
52
     * @return \Illuminate\Foundation\Application|mixed
53
     */
54
    public function getType()
55
    {
56
        $types = app('field.types');
57
        return app($types->types[$this->type]);
58
    }
59
60
    /**
61
     * Filter By Entity ID
62
     * @param $query
63
     * @param $id
64
     * @return mixed
65
     */
66
    public function scopeByEntity($query, $id)
67
    {
68
        return $query->where('entity_id', $id);
69
    }
70
71
    /**
72
     * Return a Fractal Scope Intance for the model.
73
     * @return \League\Fractal\Scope
74
     */
75
    public function transformed()
76
    {
77
        $manager = new Manager();
78
        $resource = new Item($this, new FieldTransformer());
79
        return $manager->createData($resource);
80
    }
81
}
82