Completed
Push — master ( 64d077...8cc10c )
by Zoltán
03:57
created

Set::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace BuildR\Collection;
2
3
use BuildR\Collection\Collection\AbstractCollection;
4
use BuildR\Collection\Exception\CollectionException;
5
6
/**
7
 * Set type (Collection implementation)
8
 * Sets only store scalar types
9
 *
10
 * BuildR PHP Framework
11
 *
12
 * @author Zoltán Borsos <[email protected]>
13
 * @package Collection
14
 *
15
 * @copyright    Copyright 2015, Zoltán Borsos.
16
 * @license      https://github.com/Zolli/BuildR/blob/master/LICENSE.md
17
 * @link         https://github.com/Zolli/BuildR
18
 */
19
class Set extends AbstractCollection {
20
21
    /**
22
     * Set constructor.
23
     *
24
     * @param null|array $values
25
     */
26 27
    public function __construct($values = NULL) {
27 27
        if(is_array($values)) {
28 7
            $this->addAll($values);
29 7
        }
30 27
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 27
    public function add($element) {
36 27
        if(!is_scalar($element)) {
37 4
            throw CollectionException::nonScalarTypeGiven(gettype($element));
38
        }
39
40 23
        if(!$this->contains($element)) {
41 23
            $this->data[] = $element;
42 23
        }
43 23
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 21
    public function addAll($elements) {
49 21
        $elements = $this->collectionToArray($elements);
50
51 21
        foreach($elements as $item) {
52 21
            $this->add($item);
53 21
        }
54 21
    }
55
56
57
58
59
60
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function remove($element) {
66 2
        if(($index = array_search($element, $this->data)) !== FALSE) {
67 2
            unset($this->data[$index]);
68
69 2
            return TRUE;
70
        }
71
72 2
        return FALSE;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 1 View Code Duplication
    public function removeAll($elements) {
79 1
        $elements = $this->collectionToArray($elements);
80
81 1
        $result = FALSE;
82
83 1
        foreach($elements as $item) {
84 1
            if($this->remove($item) === TRUE) {
85 1
                $result = TRUE;
86 1
            }
87 1
        }
88
89 1
        return $result;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 1
    public function retainAll($elements) {
96 1
        $elements = $this->collectionToArray($elements);
97
98 1
        $result = FALSE;
99
100 1
        foreach($this->data as $index => $data) {
101 1
            if(array_search($data, $elements) === FALSE) {
102 1
                unset($this->data[$index]);
103
104 1
                $result = TRUE;
105 1
            }
106 1
        }
107
108 1
        return $result;
109
    }
110
111
112
113
114
115
116
117
}
118