Includable::setColumns()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * User: xuan
6
 * Date: 9/24/15
7
 * Time: 4:07 PM.
8
 */
9
namespace PHPHub\Transformers\IncludeManager;
10
11
class Includable
12
{
13
    private $default_columns = [];
14
    private $allow_columns = [];
15
    private $columns = [];
16
    private $relation;
17
    private $nested = false;
18
    private $name;
19
    private $foreign_key;
20
    private $limit;
21
    private $parent_name;
22
    private $children = [];
23
    private $with_trashed = false;
24
25
    public function __construct($name)
26
    {
27
        $this->name = $name;
28
    }
29
30
    /**
31
     * 获取这个引入项的字段.
32
     *
33
     * @return array
34
     *
35
     * @throws \Exception
36
     */
37
    public function getColumns()
38
    {
39
        if (! $this->columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
            throw new \Exception("You must set includable's columns");
41
        }
42
43
        return $this->columns;
44
    }
45
46
    /**
47
     * 设置这个引入项的字段.
48
     *
49
     * @param array $columns
50
     *
51
     * @return $this
52
     */
53
    public function setColumns($columns)
54
    {
55
        $this->columns = $columns;
56
57
        return $this;
58
    }
59
60
    /**
61
     * 获取这个引入项默认引入的字段.
62
     *
63
     * @return array
64
     *
65
     * @throws \Exception
66
     */
67
    public function getDefaultColumns()
68
    {
69
        if (! $this->default_columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->default_columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
70
            throw new \Exception("You must set includable's default columns");
71
        }
72
73
        return $this->default_columns;
74
    }
75
76
    /**
77
     * 设置这个引入项默认引入的字段.
78
     *
79
     * @param array|string $default_columns
80
     *
81
     * @return $this
82
     */
83
    public function setDefaultColumns($default_columns)
84
    {
85
        $this->default_columns = (array) $default_columns;
86
87
        return $this;
88
    }
89
90
    /**
91
     * 获取这个引入项的名称.
92
     *
93
     * @return string
94
     *
95
     * @throws \Exception
96
     */
97
    public function getName()
98
    {
99
        if (! $this->name) {
100
            throw new \Exception("You must set includable's name");
101
        }
102
103
        return $this->name;
104
    }
105
106
    /**
107
     * 设置这个引入项的字段.
108
     *
109
     * @param string $name
110
     *
111
     * @return $this
112
     */
113
    public function setName($name)
114
    {
115
        $this->name = $name;
116
117
        return $this;
118
    }
119
120
    /**
121
     * 获取这个引入项关联的外键,仅 belongTo 关系需要设置.
122
     *
123
     * @return string
124
     *
125
     * @throws \Exception
126
     */
127
    public function getForeignKey()
128
    {
129
        if (! $this->nested && ! $this->foreign_key && ! $this->limit) {
130
            throw new \Exception("You must set includable's limit or foreign key");
131
        }
132
133
        return $this->foreign_key;
134
    }
135
136
    /**
137
     * 获取这个引入项关联的外键.
138
     *
139
     * @param string $foreign_key
140
     *
141
     * @return $this
142
     */
143
    public function setForeignKey($foreign_key)
144
    {
145
        $this->foreign_key = $foreign_key;
146
147
        return $this;
148
    }
149
150
    /**
151
     * 获取引入条数限制.
152
     *
153
     * @return int
154
     *
155
     * @throws \Exception
156
     */
157
    public function getLimit()
158
    {
159
        if (! $this->foreign_key && ! $this->limit) {
160
            throw new \Exception("You must set includable's limit or foreign key");
161
        }
162
163
        return $this->limit;
164
    }
165
166
    /**
167
     * 设置引入条数限制, 仅对多关系时设置(hasMany, belongsToMany, etc..).
168
     *
169
     * @param int $limit
170
     *
171
     * @return $this
172
     */
173
    public function setLimit($limit)
174
    {
175
        $this->limit = $limit;
176
177
        return $this;
178
    }
179
180
    /**
181
     * 获取允许被客户端引入的字段.
182
     *
183
     * @return array
184
     */
185
    public function getAllowColumns()
186
    {
187
        return $this->allow_columns;
188
    }
189
190
    /**
191
     * 设置允许被客户端引入的字段.
192
     *
193
     * @param mixed $allow_columns
194
     *
195
     * @return $this
196
     */
197
    public function setAllowColumns(array $allow_columns)
198
    {
199
        $this->allow_columns = $allow_columns;
200
201
        return $this;
202
    }
203
204
    /**
205
     * 计算出需要哪些字段.
206
     */
207
    public function figureOutWhichColumns()
208
    {
209
        $legal_columns = array_intersect($this->columns, $this->allow_columns);
210
211
        return array_merge($legal_columns, $this->default_columns);
212
    }
213
214
    /**
215
     * 获取对应于 model 的关联方法.
216
     *
217
     * @return string
218
     */
219
    public function getRelation()
220
    {
221
        return $this->relation ?: camel_case($this->name);
222
    }
223
224
    /**
225
     * 设置对应于 model 的关联方法.
226
     *
227
     * @param mixed $relation
228
     *
229
     * @return $this
230
     */
231
    public function setRelation($relation)
232
    {
233
        $this->relation = $relation;
234
235
        return $this;
236
    }
237
238
    /**
239
     * 设置这个引入项目是否是嵌套的.
240
     *
241
     * @param $parent string 父引入项名
242
     *
243
     * @return $this
244
     */
245
    public function nested($parent)
246
    {
247
        $this->parent_name = $parent;
248
        $this->nested = true;
249
250
        return $this;
251
    }
252
253
    /**
254
     * 同时查询软删除的数据.
255
     *
256
     * @return $this
257
     */
258
    public function withTrashed()
259
    {
260
        $this->with_trashed = true;
261
262
        return $this;
263
    }
264
265
    /**
266
     * 是否要查询软删除的数据.
267
     *
268
     * @return bool
269
     */
270
    public function isWithTrashed()
271
    {
272
        return $this->with_trashed;
273
    }
274
275
    /**
276
     * 获取这个引入项目是否是嵌套的.
277
     *
278
     * @return bool
279
     */
280
    public function isNested()
281
    {
282
        return $this->nested;
283
    }
284
285
    /**
286
     * 获取一个新的自身的实例.
287
     *
288
     * @param $name
289
     *
290
     * @return Includable
291
     */
292
    public static function make($name)
293
    {
294
        return new self($name);
295
    }
296
297
    /**
298
     * 获取父引入项名.
299
     *
300
     * @return mixed
301
     */
302
    public function getParentName()
303
    {
304
        return $this->parent_name;
305
    }
306
307
    /**
308
     * 获取所有子引入项.
309
     *
310
     * @return Includable[]
311
     */
312
    public function getChildren()
313
    {
314
        return $this->children;
315
    }
316
317
    /**
318
     * 添加一个子引入项目.
319
     *
320
     * @param mixed $children
321
     */
322
    public function addChildren(Includable $children)
323
    {
324
        $this->children[$children->getName()] = $children;
325
    }
326
}
327