Completed
Pull Request — master (#2)
by
unknown
02:01
created

AbstractCollection::contains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * This file is part of the ramsey/collection library
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright Copyright (c) Ben Ramsey <[email protected]>
9
 * @license http://opensource.org/licenses/MIT MIT
10
 * @link https://benramsey.com/projects/ramsey-collection/ Documentation
11
 * @link https://packagist.org/packages/ramsey/collection Packagist
12
 * @link https://github.com/ramsey/collection GitHub
13
 */
14
15
namespace Ramsey\Collection;
16
17
use Ramsey\Collection\Tool\TypeTrait;
18
19
/**
20
 * This class provides an implementation of the CollectionInterface, to
21
 * minimize the effort required to implement this interface
22
 */
23
abstract class AbstractCollection extends AbstractArray implements CollectionInterface
24
{
25
    use TypeTrait;
26
27
    public function add($element)
28
    {
29
        $this[] = $element;
30
31
        return true;
32
    }
33
34
    public function contains($element, $strict = true)
35
    {
36
        return in_array($element, $this->data, $strict);
37
    }
38
39
    abstract public function getType();
40
41
    public function offsetSet($offset, $value)
42
    {
43
        if ($this->checkType($this->getType(), $value) === false) {
44
            throw new \InvalidArgumentException(
45
                'Value must be of type ' . $this->getType() . '; value is '
46
                . var_export($value, true)
47
            );
48
        }
49
50
        $this->data[] = $value;
51
    }
52
53
    public function remove($element)
54
    {
55
        if (($position = array_search($element, $this->data, true)) !== false) {
56
            $this->offsetUnset($position);
57
58
            return true;
59
        }
60
61
        return false;
62
    }
63
}
64