1<?php
2/**
3 * Array-based data source for HTML_QuickForm2 objects
4 *
5 * PHP version 5
6 *
7 * LICENSE
8 *
9 * This source file is subject to BSD 3-Clause License that is bundled
10 * with this package in the file LICENSE and available at the URL
11 * https://raw.githubusercontent.com/pear/HTML_QuickForm2/trunk/docs/LICENSE
12 *
13 * @category  HTML
14 * @package   HTML_QuickForm2
15 * @author    Alexey Borzov <avb@php.net>
16 * @author    Bertrand Mansion <golgote@mamasam.com>
17 * @copyright 2006-2021 Alexey Borzov <avb@php.net>, Bertrand Mansion <golgote@mamasam.com>
18 * @license   https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
19 * @link      https://pear.php.net/package/HTML_QuickForm2
20 */
21
22/**
23 * Interface for data sources used by HTML_QuickForm2 objects
24 */
25require_once 'HTML/QuickForm2/DataSource/NullAware.php';
26
27/**
28 * Array-based data source for HTML_QuickForm2 objects
29 *
30 * @category HTML
31 * @package  HTML_QuickForm2
32 * @author   Alexey Borzov <avb@php.net>
33 * @author   Bertrand Mansion <golgote@mamasam.com>
34 * @license  https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
35 * @version  Release: 2.2.2
36 * @link     https://pear.php.net/package/HTML_QuickForm2
37 */
38class HTML_QuickForm2_DataSource_Array implements HTML_QuickForm2_DataSource_NullAware
39{
40   /**
41    * Array containing elements' values
42    * @var array
43    */
44    protected $values;
45
46   /**
47    * Class constructor, initializes the values array
48    *
49    * @param array $values Array containing the elements' values
50    */
51    public function __construct(array $values = [])
52    {
53        $this->values = $values;
54    }
55
56    public function getValue($name)
57    {
58        if (empty($this->values)) {
59            return null;
60        }
61        if (strpos($name, '[')) {
62            $tokens = explode('[', str_replace(']', '', $name));
63            $value = $this->values;
64            do {
65                $token = array_shift($tokens);
66                if (!is_array($value) || !isset($value[$token])) {
67                    return null;
68                }
69                $value = $value[$token];
70            } while (!empty($tokens));
71            return $value;
72        } elseif (isset($this->values[$name])) {
73            return $this->values[$name];
74        } else {
75            return null;
76        }
77    }
78
79    public function hasValue($name)
80    {
81        if (empty($this->values)) {
82            return false;
83
84        } elseif (!strpos($name, '[')) {
85            return array_key_exists($name, $this->values);
86
87        } else {
88            $tokens = explode('[', str_replace(']', '', $name));
89            $value  = $this->values;
90            do {
91                $token = array_shift($tokens);
92                if (!is_array($value) || !array_key_exists($token, $value)) {
93                    return false;
94                }
95                $value = $value[$token];
96            } while (!empty($tokens));
97            return true;
98        }
99    }
100}
101?>
102