Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Checklist
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
7 / 7
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getChecked
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUnchecked
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasChecked
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasUnchecked
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 check
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 uncheck
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Checklist class (used for checking off a list of values)
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Record
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\Record;
31
32use function count;
33
34/**
35 * Checklist class (used for checking off a list of values)
36 *
37 * @category VuFind
38 * @package  Record
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org Main Site
42 */
43class Checklist
44{
45    /**
46     * Unchecked values
47     *
48     * @var array
49     */
50    protected $unchecked;
51
52    /**
53     * Checked values
54     *
55     * @var array
56     */
57    protected $checked = [];
58
59    /**
60     * Constructor
61     *
62     * @param array $values Values for list (all begin unchecked)
63     */
64    public function __construct($values)
65    {
66        $this->unchecked = array_unique($values);
67    }
68
69    /**
70     * Get list of checked values.
71     *
72     * @return array
73     */
74    public function getChecked()
75    {
76        return array_values($this->checked);
77    }
78
79    /**
80     * Get list of unchecked values.
81     *
82     * @return array
83     */
84    public function getUnchecked()
85    {
86        return array_values($this->unchecked);
87    }
88
89    /**
90     * Are there checked items?
91     *
92     * @return bool
93     */
94    public function hasChecked()
95    {
96        return count($this->checked) > 0;
97    }
98
99    /**
100     * Are there unchecked items?
101     *
102     * @return bool
103     */
104    public function hasUnchecked()
105    {
106        return count($this->unchecked) > 0;
107    }
108
109    /**
110     * Check off a value, returning true if the value was found in the unchecked
111     * list and false if it was not.
112     *
113     * @param mixed $value Value to check
114     *
115     * @return bool
116     */
117    public function check($value)
118    {
119        $key = array_search($value, $this->unchecked);
120        if ($key !== false) {
121            unset($this->unchecked[$key]);
122            $this->checked[$key] = $value;
123            return true;
124        }
125        return false;
126    }
127
128    /**
129     * Uncheck a value, returning true if the value was found in the checked
130     * list and false if it was not.
131     *
132     * @param mixed $value Value to uncheck
133     *
134     * @return bool
135     */
136    public function uncheck($value)
137    {
138        $key = array_search($value, $this->checked);
139        if ($key !== false) {
140            unset($this->checked[$key]);
141            $this->unchecked[$key] = $value;
142            return true;
143        }
144        return false;
145    }
146}