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