Issues (43)

src/BaseModel.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Alive2212\LaravelSmartRestful;
4
5
use DeepCopy\Reflection\ReflectionHelper;
6
use Laravel\Scout\Searchable;
7
use Illuminate\Database\Eloquent\Model;
8
9
class BaseModel extends Model
10
{
11
    use Searchable;
12
13
    /**
14
     * default relations of method
15
     *
16
     * @var array
17
     */
18
    protected $relation = [];
19
20
    /**
21
     * searchable columns in this model
22
     *
23
     * @var array
24
     */
25
    protected $searchableColumns = [];
26
27
    /**
28
     * searchable columns of one model into this model
29
     *
30
     * @var array
31
     */
32
    protected $searchableInnerColumns = [];
33
34
    /**
35
     * searchable columns of many model into this model
36
     *
37
     * @var array
38
     */
39
    protected $searchableManyInnerColumns = [];
40
41
    /**
42
     * Get the indexable data array for the model.
43
     *
44
     * @return array
45
     */
46
    public function toSearchableArray()
47
    {
48
        if (!sizeof($this->searchableColumns)) {
49
            $result = collect($this);
50
        } else {
51
            $result = collect([
52
                'id' => $this['id'],
53
            ]);
54
            foreach ($this->searchableColumns as $searchableColumn) {
55
                $result->put($searchableColumn, collect($this)->get($searchableColumn));
56
            }
57
        }
58
        if (sizeof($this->searchableInnerColumns)) {
59
            foreach ($this->searchableInnerColumns as $searchableInnerColumnKey => $searchableInnerColumnValue) {
60
                $searchableInnerColumnKey = explode('-', $searchableInnerColumnKey)[0];
61
                $innerModels = explode('.', $searchableInnerColumnKey);
62
                $values = $this;
63
                foreach ($innerModels as $innerModel) {
64
                    $values = $values->{$innerModel};
65
                }
66
                $values = collect($values);
67
                $result->put($searchableInnerColumnKey . '-' . $searchableInnerColumnValue,
68
                    collect($values)->get($searchableInnerColumnValue));
69
            }
70
        }
71
        if (sizeof($this->searchableManyInnerColumns)) {
72
            foreach ($this->searchableManyInnerColumns as $searchableInnerColumnKey => $searchableInnerColumnValue) {
73
                $searchableInnerColumnKey = explode('-', $searchableInnerColumnKey)[0];
74
                $innerModels = explode('.', $searchableInnerColumnKey);
75
                $values = $this;
76
                foreach ($innerModels as $innerModel) {
77
                    $values = $values->{$innerModel};
78
                }
79
                $values = collect($values);
80
                $counter = 0;
81
                foreach ($values as $value) {
82
                    $result->put($searchableInnerColumnKey . '-' . $searchableInnerColumnValue . '-' . $counter,
83
                        collect($value)->get($searchableInnerColumnValue));
84
                    $counter++;
85
                }
86
            }
87
        }
88
        return $result->toArray();
89
    }
90
91
    /**
92
     * get methods names of child class in array type
93
     *
94
     * @return array
95
     */
96
    public function getMethods()
97
    {
98
        return (new ReflectionHelper())->getClassMethodsNames($this,\ReflectionMethod::IS_PUBLIC);
0 ignored issues
show
The method getClassMethodsNames() does not exist on DeepCopy\Reflection\ReflectionHelper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        return (new ReflectionHelper())->/** @scrutinizer ignore-call */ getClassMethodsNames($this,\ReflectionMethod::IS_PUBLIC);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getRelatedMethods()
105
    {
106
        return (new ReflectionHelper())->getClassRelationMethodsNames($this);
0 ignored issues
show
The method getClassRelationMethodsNames() does not exist on DeepCopy\Reflection\ReflectionHelper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        return (new ReflectionHelper())->/** @scrutinizer ignore-call */ getClassRelationMethodsNames($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112
    public function withRelation()
113
    {
114
        return $this->with($this->getRelatedMethods());
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120
    public function loadRelation()
121
    {
122
        return $this->load($this->getRelatedMethods());
123
    }
124
}
125