Completed
Push — master ( ec3201...5305af )
by Ryan
08:02
created

AssignmentCollection::unlocked()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Anomaly\Streams\Platform\Assignment;
2
3
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
4
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface;
5
use Anomaly\Streams\Platform\Model\EloquentCollection;
6
7
/**
8
 * Class AssignmentCollection
9
 *
10
 * @link    http://anomaly.is/streams-platform
11
 * @author  AnomalyLabs, Inc. <[email protected]>
12
 * @author  Ryan Thompson <[email protected]>
13
 * @package Anomaly\Streams\Platform\Assignment
14
 */
15
class AssignmentCollection extends EloquentCollection
16
{
17
18
    /**
19
     * Find an assignment by it's field slug.
20
     *
21
     * @param  $slug
22
     * @return AssignmentInterface
23
     */
24
    public function findByFieldSlug($slug)
25
    {
26
        foreach ($this->items as $item) {
27
            /* @var AssignmentInterface $item */
28
            if ($item->getFieldSlug() == $slug) {
29
                return $item;
30
            }
31
        }
32
33
        return null;
34
    }
35
36
37
    /**
38
     * Find all fields using
39
     * the provided field type.
40
     *
41
     * @param $namespace
42
     * @return static
43
     */
44
    public function findAllByFieldType($namespace)
45
    {
46
        return new static(
47
            array_filter(
48
                $this->items,
49
                function (AssignmentInterface $assignment) use ($namespace) {
50
                    return $assignment->getFieldTypeValue() == $namespace;
51
                }
52
            )
53
        );
54
    }
55
56
    /**
57
     * Return assignments only included the provided fields.
58
     *
59
     * @param array $fields
60
     * @return AssignmentCollection
61
     */
62
    public function withFields(array $fields)
63
    {
64
        return new static(
65
            array_filter(
66
                array_map(
67
                    function (AssignmentInterface $assignment) use ($fields) {
68
                        return in_array($assignment->getFieldSlug(), $fields) ? $assignment : null;
69
                    },
70
                    $this->items
71
                )
72
            )
73
        );
74
    }
75
76
    /**
77
     * Return assignments not included the provided fields.
78
     *
79
     * @param array $fields
80
     * @return AssignmentCollection
81
     */
82
    public function withoutFields(array $fields)
83
    {
84
        return new static(
85
            array_filter(
86
                array_map(
87
                    function (AssignmentInterface $assignment) use ($fields) {
88
                        return !in_array($assignment->getFieldSlug(), $fields) ? $assignment : null;
89
                    },
90
                    $this->items
91
                )
92
            )
93
        );
94
    }
95
96
    /**
97
     * Return only assignments that have relation fields.
98
     *
99
     * @return AssignmentCollection
100
     */
101
    public function relations()
102
    {
103
        $relations = [];
104
105
        /* @var AssignmentInterface $item */
106
        /* @var FieldType $type */
107
        foreach ($this->items as $item) {
108
109
            $type = $item->getFieldType();
110
111
            if (method_exists($type, 'getRelation')) {
112
                $relations[] = $item;
113
            }
114
        }
115
116
        return self::make($relations);
117
    }
118
119
    /**
120
     * Return only assignments that have date fields.
121
     *
122
     * @return AssignmentCollection
123
     */
124
    public function dates()
125
    {
126
        return $this->filter(
127
            function (AssignmentInterface $assignment) {
128
129
                $type = $assignment->getFieldType();
130
131
                return in_array($type->getColumnType(), ['date', 'datetime']);
132
            }
133
        );
134
    }
135
136
    /**
137
     * Return only assignments that are unique.
138
     *
139
     * @return AssignmentCollection
140
     */
141
    public function indexed()
142
    {
143
        return $this->filter(
144
            function (AssignmentInterface $assignment) {
145
                return $assignment->isUnique();
146
            }
147
        );
148
    }
149
150
    /**
151
     * Return only assignments that are required.
152
     *
153
     * @return AssignmentCollection
154
     */
155
    public function required()
156
    {
157
        return $this->filter(
158
            function (AssignmentInterface $assignment) {
159
                return $assignment->isRequired();
160
            }
161
        );
162
    }
163
164
    /**
165
     * Return only assignments that are translatable.
166
     *
167
     * @return AssignmentCollection
168
     */
169
    public function translatable()
170
    {
171
        return $this->filter(
172
            function (AssignmentInterface $assignment) {
173
                return $assignment->isTranslatable();
174
            }
175
        );
176
    }
177
178
    /**
179
     * Return only assignments that are NOT translatable.
180
     *
181
     * @return AssignmentCollection
182
     */
183
    public function notTranslatable()
184
    {
185
        return $this->filter(
186
            function (AssignmentInterface $assignment) {
187
                return $assignment->isTranslatable() == false;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
188
            }
189
        );
190
    }
191
192
    /**
193
     * Return an array of field slugs.
194
     *
195
     * @param null $prefix
196
     * @return array
197
     */
198
    public function fieldSlugs($prefix = null)
199
    {
200
        $slugs = [];
201
202
        /* @var AssignmentInterface $item */
203
        foreach ($this->items as $item) {
204
            $slugs[] = $prefix . $item->getFieldSlug();
205
        }
206
207
        return $slugs;
208
    }
209
210
    /**
211
     * Return only assignments with locked fields.
212
     *
213
     * @return AssignmentCollection
214
     */
215 View Code Duplication
    public function locked()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
    {
217
        $items = [];
218
219
        foreach ($this->items as $item) {
220
            if ($item instanceof AssignmentInterface && $field = $item->getField()) {
221
                if ($field->isLocked()) {
222
                    $items[] = $item;
223
                }
224
            }
225
        }
226
227
        return new static($items);
228
    }
229
230
    /**
231
     * Return only assignments with fields
232
     * that are not locked.
233
     *
234
     * @return AssignmentCollection
235
     */
236 View Code Duplication
    public function notLocked()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
    {
238
        $items = [];
239
240
        foreach ($this->items as $item) {
241
            if ($item instanceof AssignmentInterface && $field = $item->getField()) {
242
                if (!$field->isLocked()) {
243
                    $items[] = $item;
244
                }
245
            }
246
        }
247
248
        return new static($items);
249
    }
250
251
    /**
252
     * An alias for notLocked();
253
     *
254
     * @return AssignmentCollection
255
     */
256
    public function unlocked()
257
    {
258
        return $this->notLocked();
259
    }
260
}
261