Passed
Push — master ( 2afdeb...b846a2 )
by Nikolaos
06:23 queued 03:51
created

AbstractInput::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 19
ccs 9
cts 9
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * For the full copyright and license information, please view the LICENSE.md
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Phalcon\Html\Helper\Input;
13
14
use Phalcon\Html\Helper\AbstractHelper;
15
16
/**
17
 * Class AbstractInput
18
 *
19
 * @property array  $attributes
20
 * @property string $type
21
 * @property string $value
22
 */
23
abstract class AbstractInput extends AbstractHelper
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $type = "text";
29
30
    /**
31
     * @var array
32
     */
33
    protected $attributes = [];
34
35
    /**
36
     * @param string      $name
37
     * @param string|null $value
38
     * @param array       $attributes
39
     *
40
     * @return AbstractInput
41
     */
42 5
    public function __invoke(
43
        string $name,
44
        string $value = null,
45
        array $attributes = []
46
    ): AbstractInput {
47 5
        $this->attributes = [
48 5
            "type" => $this->type,
49 5
            "name" => $name,
50
        ];
51
52 5
        if (!isset($attributes["id"])) {
53 5
            $this->attributes["id"] = $name;
54
        }
55
56 5
        $this->setValue($value);
57
58 5
        $this->attributes = array_merge($this->attributes, $attributes);
59
60 5
        return $this;
61
    }
62
63
    /**
64
     * Returns the HTML for the input.
65
     *
66
     * @return string
67
     */
68 4
    public function __toString()
69
    {
70 4
        $attributes       = $this->attributes;
71 4
        $this->attributes = [];
72
73 4
        return $this->renderTag(
74 4
            "input",
75
            $attributes,
76 4
            "/"
77
        );
78
    }
79
80
    /**
81
     * Sets the value of the element
82
     *
83
     * @param string|null $value
84
     *
85
     * @return AbstractInput
86
     */
87 5
    public function setValue(string $value = null): AbstractInput
88
    {
89 5
        if (null !== $value) {
90 4
            $this->attributes["value"] = $value;
91
        }
92
93 5
        return $this;
94
    }
95
}
96