Completed
Pull Request — master (#16)
by ARCANEDEV
02:38
created

UtilSet::keys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\Stripe\Utilities;
2
3
use Arcanedev\Stripe\Contracts\Utilities\UtilSetInterface;
4
use ArrayIterator;
5
use Countable;
6
use IteratorAggregate;
7
8
/**
9
 * Class     UtilSet
10
 *
11
 * @package  Arcanedev\Stripe\Utilities
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class UtilSet implements UtilSetInterface, IteratorAggregate, Countable
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Properties
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /** @var array */
21
    private $items;
22
23
    /* ------------------------------------------------------------------------------------------------
24
     |  Constructor
25
     | ------------------------------------------------------------------------------------------------
26
     */
27 950
    public function __construct($attributes = [])
28
    {
29 950
        $this->items = [];
30
31 950
        foreach ($attributes as $attribute) {
32 940
            $this->add($attribute);
33 760
        }
34 950
    }
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Main Functions
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Check if attribute is included.
42
     *
43
     * @param string $attribute
44
     *
45
     * @return bool
46
     */
47 804
    public function includes($attribute)
48
    {
49 804
        return isset($this->items[$attribute]);
50
    }
51
52
    /**
53
     * Add attribute
54
     *
55
     * @param $attribute
56
     */
57 945
    public function add($attribute)
58
    {
59 945
        $this->items[$attribute] = true;
60 945
    }
61
62
    /**
63
     * @param $attribute
64
     */
65 679
    public function discard($attribute)
66
    {
67 679
        unset($this->items[$attribute]);
68 679
    }
69
70
    /**
71
     * Get all attributes key
72
     *
73
     * @return array
74
     */
75 175
    public function keys()
76
    {
77 175
        return array_keys($this->items);
78
    }
79
80
    /**
81
     * Diff Keys
82
     *
83
     * @param array $keys
84
     *
85
     * @return array
86
     */
87 40
    public function diffKeys(array $keys = [])
88
    {
89 40
        return array_diff($this->keys(), $keys);
90
    }
91
92
    /**
93
     * Get all Attributes
94
     *
95
     * @return array
96
     */
97 195
    public function toArray()
98
    {
99 195
        return $this->count() ? $this->keys() : [];
100
    }
101
102
    /**
103
     * @return ArrayIterator
104
     */
105 30
    public function getIterator()
106
    {
107 30
        return new ArrayIterator($this->toArray());
108
    }
109
110
    /**
111
     * Count items
112
     *
113
     * @return int The custom count as an integer.
114
     */
115 200
    public function count()
116
    {
117 200
        return count($this->items);
118
    }
119
120
    /* ------------------------------------------------------------------------------------------------
121
     |  Check Functions
122
     | ------------------------------------------------------------------------------------------------
123
     */
124
    /**
125
     * Check if the items are empty.
126
     *
127
     * @return bool
128
     */
129 15
    public function isEmpty()
130
    {
131 15
        return empty($this->items);
132
    }
133
}
134