Passed
Push — master ( 1ac661...094357 )
by Marcio
02:42
created

ValidateDot::has()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 19
rs 8.4444
cc 8
nc 5
nop 1
1
<?php
2
3
/**
4
 * Dot - PHP dot notation access to arrays
5
 *
6
 * @author  Riku Särkinen <[email protected]>
7
 * @link    https://github.com/adbario/php-dot-notation
8
 * @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License)
9
 */
10
11
namespace Ballybran\Core\Collections\Collection;
12
13
use Countable;
14
use ArrayIterator;
15
16
/**
17
 * Dot
18
 *
19
 * This class provides a dot notation access and helper functions for
20
 * working with arrays of data. Inspired by Laravel Collection.
21
 */
22
class ValidateDot implements Countable
23
{
24
   
25
    /**
26
     * Replace all items with a given array as a reference
27
     *
28
     * @param array $items
29
     */
30
    public function setReference(array &$items)
31
    {
32
        $this->elements = &$items;
0 ignored issues
show
Bug Best Practice introduced by
The property elements does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
    }
34
35
	/**
36
     * Return all the stored items
37
     *
38
     * @return array
39
     */
40
    public function all()
41
    {
42
        return $this->elements;
43
    }
44
45
46
    /**
47
     * Checks if the given key exists in the provided array.
48
     *
49
     * @param  array $array Array to validate
50
     * @param  int|string $key The key to look for
51
     *
52
     * @return bool
53
     */
54
    private function exists($array, $key)
55
    {
56
        return array_key_exists($key, $array);
57
    }
58
59
   
60
      /*
61
     * --------------------------------------------------------------
62
     * ArrayAccess interface
63
     * --------------------------------------------------------------
64
     */
65
    /**
66
     * Check if a given key exists
67
     *
68
     * @param  int|string $key
69
     * @return bool
70
     */
71
    public function offsetExists($key)
72
    {
73
        return $this->has($key);
74
    }
75
76
    /**
77
     * Replace all items with a given array
78
     *
79
     * @param mixed $items
80
     */
81
    public function setArray($items)
82
    {
83
        $this->elements = $this->getArrayItems($items);
0 ignored issues
show
Bug Best Practice introduced by
The property elements does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
introduced by
The method getArrayItems() does not exist on Ballybran\Core\Collections\Collection\ValidateDot. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

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

83
        /** @scrutinizer ignore-call */ 
84
        $this->elements = $this->getArrayItems($items);
Loading history...
84
    }
85
86
    /*
87
     * --------------------------------------------------------------
88
     * Countable interface
89
     * --------------------------------------------------------------
90
     */
91
    /**
92
     * Return the number of items in a given key
93
     *
94
     * @param  int|string|null $key
95
     * @return int
96
     */
97
    public function count($key = null)
98
    {
99
        return count($this->get($key));
0 ignored issues
show
introduced by
The method get() does not exist on Ballybran\Core\Collections\Collection\ValidateDot. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

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

99
        return count($this->/** @scrutinizer ignore-call */ get($key));
Loading history...
100
    }
101
102
    
103
    /**
104
     * Check if a given key or keys exists
105
     *
106
     * @param  array|int|string $keys
107
     * @return bool
108
     */
109
    public function has($keys)
110
    {
111
        $keys = (array)$keys;
112
        if ( empty($this->elements) || $keys === []) {
113
            return false;
114
        }
115
        foreach ($keys as $key) {
116
            $items = $this->elements;
117
            if ($this->exists($items, $key)) {
118
                continue;
119
            }
120
            foreach (explode('.', $key) as $segment) {
121
                if (!is_array($items) || !$this->exists($items, $segment)) {
122
                    return false;
123
                }
124
                $items = $items[$segment];
125
            }
126
        }
127
        return true;
128
    }
129
130
    /**
131
     * Check if a given key or keys are empty
132
     *
133
     * @param  array|int|string|null $keys
134
     * @return bool
135
     */
136
    public function isEmpty($keys = null)
137
    {
138
        if (is_null($keys)) {
139
            return empty($this->elements);
140
        }
141
        $keys = (array)$keys;
142
        foreach ($keys as $key) {
143
            if (!empty($this->get($key))) {
144
                return false;
145
            }
146
        }
147
        return true;
148
    }
149
150
    /**
151
     * Merge a given array or a Dot object with the given key
152
     * or with the whole Dot object
153
     *
154
     * @param array|string|self $key
155
     * @param array $value
156
     */
157
    public function merge($key, $value = null)
158
    {
159
        if (is_array($key)) {
160
            $this->elements = array_merge($this->elements, $key);
0 ignored issues
show
Bug Best Practice introduced by
The property elements does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
161
        } elseif (is_string($key)) {
162
            $items = (array)$this->get($key);
163
            $value = array_merge($items, $this->getArrayItems($value));
164
            $this->set($key, $value);
0 ignored issues
show
introduced by
The method set() does not exist on Ballybran\Core\Collections\Collection\ValidateDot. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

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

164
            $this->/** @scrutinizer ignore-call */ 
165
                   set($key, $value);
Loading history...
165
        } 
166
         $this->elements = array_merge($this->elements, $key->all());
167
        
168
    }
169
170
171
172
}