Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Container
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
6 / 6
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getAllValues
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 __get
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 __set
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __isset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __unset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Class for treating a set of cookies as an object (inspired by
5 * \Laminas\Session\Container).
6 *
7 * PHP version 8
8 *
9 * Copyright (C) Villanova University 2012.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  Cookie
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development Wiki
29 */
30
31namespace VuFind\Cookie;
32
33use function strlen;
34
35/**
36 * Class for treating a set of cookies as an object (inspired by
37 * \Laminas\Session\Container).
38 *
39 * @category VuFind
40 * @package  Cookie
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development Wiki
44 */
45class Container
46{
47    /**
48     * Prefix to use for cookie values.
49     *
50     * @var string
51     */
52    protected $groupName;
53
54    /**
55     * Cookie manager.
56     *
57     * @var CookieManager
58     */
59    protected $manager;
60
61    /**
62     * Constructor
63     *
64     * @param string        $groupName Prefix to use for cookie values.
65     * @param CookieManager $manager   Cookie manager.
66     */
67    public function __construct($groupName, CookieManager $manager = null)
68    {
69        $this->groupName = $groupName;
70        $this->manager = $manager ?? new CookieManager($_COOKIE);
71    }
72
73    /**
74     * Get all values in the container as an associative array.
75     *
76     * @return array
77     */
78    public function getAllValues()
79    {
80        $retVal = [];
81        foreach ($this->manager->getCookies() as $key => $value) {
82            if (str_starts_with($key, $this->groupName)) {
83                $retVal[substr($key, strlen($this->groupName))] = $value;
84            }
85        }
86        return $retVal;
87    }
88
89    /**
90     * Get the value of a variable in this object.
91     *
92     * @param string $var programmatic name of a key, in a <key,value> pair in the
93     * current container
94     *
95     * @return void
96     */
97    public function & __get($var)
98    {
99        $val = $this->manager->get($this->groupName . $var);
100        return $val;
101    }
102
103    /**
104     * Set a variable in this object.
105     *
106     * @param string $var   programmatic name of a key, in a <key,value> pair in the
107     * current container
108     * @param string $value new value for the key
109     *
110     * @return void
111     */
112    public function __set($var, $value)
113    {
114        $this->manager->set($this->groupName . $var, $value);
115    }
116
117    /**
118     * Test the existence of a variable in this object.
119     *
120     * @param string $var programmatic name of a key, in a <key,value> pair in the
121     * current container
122     *
123     * @return bool
124     */
125    public function __isset($var)
126    {
127        return null !== $this->manager->get($this->groupName . $var);
128    }
129
130    /**
131     * Unset a variable in this object.
132     *
133     * @param string $var programmatic name of a key, in a <key,value> pair in the
134     * current groupName
135     *
136     * @return void
137     */
138    public function __unset($var)
139    {
140        $this->manager->clear($this->groupName . $var);
141    }
142}