Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Datalist
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 render
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Form\Element\Input;
15
16use Pop\Dom\Child;
17
18/**
19 * Form text element class
20 *
21 * @category   Pop
22 * @package    Pop\Form
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    4.0.0
27 */
28
29class Datalist extends Text
30{
31
32    /**
33     * Datalist object.
34     * @var ?Child
35     */
36    protected ?Child $datalist = null;
37
38    /**
39     * Constructor
40     *
41     * Instantiate the datalist text input form element
42     *
43     * @param  string  $name
44     * @param  array  $values
45     * @param  ?string $value
46     * @param  ?string $indent
47     */
48    public function __construct(string $name, array $values, ?string $value = null, ?string $indent = null)
49    {
50        parent::__construct($name, $value);
51        if ($indent !== null) {
52            $this->setIndent($indent);
53        }
54        $this->setAttribute('list', $name . '_datalist');
55
56        if ($values !== null) {
57            $this->datalist = new Child('datalist');
58            if ($indent !== null) {
59                $this->datalist->setIndent($indent);
60            }
61            $this->datalist->setAttribute('id', $name . '_datalist');
62            foreach ($values as $key => $val) {
63                $this->datalist->addChild((new Child('option', $val))->setAttribute('value', $key));
64            }
65        }
66    }
67
68    /**
69     * Render the datalist element
70     *
71     * @param  int     $depth
72     * @param  ?string $indent
73     * @param  bool    $inner
74     * @return string
75     */
76    public function render(int $depth = 0, ?string $indent = null, bool $inner = false): string
77    {
78        return parent::render($depth, $indent, $inner) . $this->datalist->render($depth, $indent, $inner);
79    }
80
81    /**
82     * Print the datalist element
83     *
84     * @return string
85     */
86    public function __toString(): string
87    {
88        return $this->render();
89    }
90
91}