|
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
|
|
|
|