Completed
Push — master ( 52038d...574828 )
by Emily
02:11
created

AbstractCollection::contains()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Model\Collection;
16
17
/**
18
 * Represents an abstract collection of items
19
 *
20
 * @generic ValueType
21
 */
22
abstract class AbstractCollection
23
    implements \ArrayAccess, \IteratorAggregate, \Countable
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
24
{
25
    /**
26
     * Returns the size of the collection
27
     *
28
     * @return int The size of the collection
29
     */
30
    abstract public function size() : int;
31
32
    /**
33
     * Returns how many elements are in the Collection
34
     *
35
     * @return int The number of elements in the Collection
36
     */
37 4
    public function count()
38
    {
39 4
        return $this->size();
40
    }
41
42
    /**
43
     * Checks if the Collection is empty
44
     *
45
     * @return boolean True if the element is empty
46
     */
47 2
    public function empty()
48
    {
49 2
        return $this->size() === 0;
50
    }
51
52
    /**
53
     * Basic implementation of contains
54
     *
55
     * Should be overridden by datatype-specific implementations for
56
     * speed improvements
57
     *
58
     * @param ValueType $searchFor The key to search for
59
     * @return boolean
60
     */
61 1
    public function contains($searchFor) : bool
62
    {
63 1
        foreach ($this as $item)
64
        {
65 1
            if ($item === $searchFor)
66
            {
67 1
                return true;
68
            }
69
        }
70
71 1
        return false;
72
    }
73
74
    public function map(callable $cb)
75
    {
76
        foreach ($this as $key => $value)
77
        {
78
            $cb($key, $value);
79
        }
80
    }
81
}
82