Filter::succeeded()   A
last analyzed

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
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Copyright 2019 Amin Yazdanpanah<http://www.aminyazdanpanah.com>.
5
 *
6
 * Licensed under the MIT License;
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *      https://opensource.org/licenses/MIT
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace AYazdanpanah\SaveUploadedFiles;
20
21
22
class Filter implements \Countable
23
{
24
    private $extractions;
25
26
    /**
27
     * Filter constructor.
28
     * @param $extractions
29
     */
30
    public function __construct(array $extractions)
31
    {
32
        $this->extractions = $extractions;
33
    }
34
35
    /**
36
     * Returns all uploads and their details.
37
     *
38
     * @return mixed
39
     */
40
    public function all(): array
41
    {
42
        return $this->extractions;
43
    }
44
45
    /**
46
     * Returns specified uploads and their details.
47
     *
48
     * @param array | string $name
49
     * @return array
50
     */
51
    public function get($name = ['*']): array
52
    {
53
        if (!is_array($name)) {
54
            $name = array($name);
55
        }
56
57
        return array_filter($this->extractions, function ($key) use ($name) {
58
            return in_array($key, $name) || current($name) === "*";
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string; however, parameter $array of current() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

58
            return in_array($key, $name) || current(/** @scrutinizer ignore-type */ $name) === "*";
Loading history...
Bug introduced by
It seems like $name can also be of type string; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

58
            return in_array($key, /** @scrutinizer ignore-type */ $name) || current($name) === "*";
Loading history...
59
        }, ARRAY_FILTER_USE_KEY);
60
    }
61
62
    /**
63
     * Returns all uploads and their details except those ones specified.
64
     *
65
     * @param array | string $name
66
     * @return array
67
     */
68
    public function except($name): array
69
    {
70
        if (!is_array($name)) {
71
            $name = array($name);
72
        }
73
74
        return array_filter($this->extractions, function ($key) use ($name) {
75
            return !in_array($key, $name);
76
        }, ARRAY_FILTER_USE_KEY);
77
    }
78
79
    /**
80
     * Returns first upload and it's detail.
81
     *
82
     * @return array
83
     */
84
    public function first(): array
85
    {
86
        return current($this->extractions);
87
    }
88
89
    /**
90
     * Returns all succeeded uploads and their details.
91
     *
92
     * @return array
93
     */
94
    public function succeeded(): array
95
    {
96
        return array_filter($this->extractions, function ($detail) {
97
            return $detail['status'];
98
        });
99
    }
100
101
    /**
102
     * Returns all failed uploads and their details.
103
     *
104
     * @return array
105
     */
106
    public function failed(): array
107
    {
108
        return array_filter($this->extractions, function ($detail) {
109
            return !$detail['status'];
110
        });
111
    }
112
113
    /**
114
     * Returns all upload names.
115
     *
116
     * @return array
117
     */
118
    public function names()
119
    {
120
        return array_keys($this->extractions);
121
    }
122
123
    /**
124
     * Returns the number of uploads
125
     *
126
     */
127
    public function count()
128
    {
129
        return count($this->extractions);
130
    }
131
}