UtilSet   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 120
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A includes() 0 4 1
A add() 0 4 1
A discard() 0 4 1
A keys() 0 4 1
A diffKeys() 0 4 1
A toArray() 0 4 2
A getIterator() 0 4 1
A count() 0 4 1
A isEmpty() 0 4 1
A __construct() 0 8 2
1
<?php namespace Arcanedev\Stripe\Utilities;
2
3
use Arcanedev\Stripe\Contracts\Utilities\UtilSet as UtilSetContract;
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 UtilSetContract, IteratorAggregate, Countable
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Properties
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /** @var array */
21
    private $items;
22
23
    /* ------------------------------------------------------------------------------------------------
24
     |  Constructor
25
     | ------------------------------------------------------------------------------------------------
26
     */
27 508
    public function __construct($attributes = [])
28
    {
29 508
        $this->items = [];
30
31 508
        foreach ($attributes as $attribute) {
32 504
            $this->add($attribute);
33
        }
34 508
    }
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 456
    public function includes($attribute)
48
    {
49 456
        return isset($this->items[$attribute]);
50
    }
51
52
    /**
53
     * Add attribute
54
     *
55
     * @param $attribute
56
     */
57 506
    public function add($attribute)
58
    {
59 506
        $this->items[$attribute] = true;
60 506
    }
61
62
    /**
63
     * @param $attribute
64
     */
65 404
    public function discard($attribute)
66
    {
67 404
        unset($this->items[$attribute]);
68 404
    }
69
70
    /**
71
     * Get all attributes key
72
     *
73
     * @return array
74
     */
75 94
    public function keys()
76
    {
77 94
        return array_keys($this->items);
78
    }
79
80
    /**
81
     * Diff Keys
82
     *
83
     * @param array $keys
84
     *
85
     * @return array
86
     */
87 16
    public function diffKeys(array $keys = [])
88
    {
89 16
        return array_diff($this->keys(), $keys);
90
    }
91
92
    /**
93
     * Get all Attributes
94
     *
95
     * @return array
96
     */
97 102
    public function toArray()
98
    {
99 102
        return $this->count() ? $this->keys() : [];
100
    }
101
102
    /**
103
     * @return ArrayIterator
104
     */
105 12
    public function getIterator()
106
    {
107 12
        return new ArrayIterator($this->toArray());
108
    }
109
110
    /**
111
     * Count items
112
     *
113
     * @return int The custom count as an integer.
114
     */
115 104
    public function count()
116
    {
117 104
        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 6
    public function isEmpty()
130
    {
131 6
        return empty($this->items);
132
    }
133
}
134