Completed
Push — master ( 8d3dbf...7592af )
by Paweł
62:36
created

Criteria   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 68.18%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 105
ccs 15
cts 22
cp 0.6818
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A all() 0 4 1
A add() 0 4 1
A get() 0 4 2
A set() 0 4 1
A has() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1
A remove() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Common Component.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú.
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Component\Common\Criteria;
18
19
class Criteria implements \IteratorAggregate, \Countable
20
{
21
    /**
22
     * Criteria storage.
23
     *
24
     * @var array
25
     */
26
    protected $criteria;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param array $criteria An array of criteria
32
     */
33 90
    public function __construct(array $criteria = [])
34
    {
35 90
        $this->criteria = $criteria;
36 90
    }
37
38
    /**
39
     * Returns the criteria.
40
     *
41
     * @return array An array of criteria
42
     */
43 87
    public function all()
44
    {
45 87
        return $this->criteria;
46
    }
47
48
    /**
49
     * Adds criteria.
50
     *
51
     * @param array $criteria An array of criteria
52
     */
53
    public function add(array $criteria = array())
54
    {
55
        $this->criteria = array_replace($this->criteria, $criteria);
56
    }
57
58
    /**
59
     * Returns a parameter by name.
60
     *
61
     * @param string $key     The key
62
     * @param mixed  $default The default value if the parameter key does not exist
63
     *
64
     * @return mixed
65
     */
66 87
    public function get($key, $default = null)
67
    {
68 87
        return array_key_exists($key, $this->criteria) ? $this->criteria[$key] : $default;
69
    }
70
71
    /**
72
     * Sets a parameter by name.
73
     *
74
     * @param string $key   The key
75
     * @param mixed  $value The value
76
     */
77 15
    public function set($key, $value)
78
    {
79 15
        $this->criteria[$key] = $value;
80 15
    }
81
82
    /**
83
     * Returns true if the parameter is defined.
84
     *
85
     * @param string $key The key
86
     *
87
     * @return bool true if the parameter exists, false otherwise
88
     */
89 87
    public function has($key)
90
    {
91 87
        return array_key_exists($key, $this->criteria);
92
    }
93
94
    /**
95
     * Removes a parameter.
96
     *
97
     * @param string $key The key
98
     */
99 3
    public function remove($key)
100
    {
101 3
        unset($this->criteria[$key]);
102 3
    }
103
104
    /**
105
     * Returns an iterator for criteria.
106
     *
107
     * @return \ArrayIterator An \ArrayIterator instance
108
     */
109
    public function getIterator()
110
    {
111
        return new \ArrayIterator($this->criteria);
112
    }
113
114
    /**
115
     * Returns the number of criteria.
116
     *
117
     * @return int The number of criteria
118
     */
119
    public function count()
120
    {
121
        return count($this->criteria);
122
    }
123
}
124