form::Attributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sjhc1170
5
 * Date: 09/05/2018
6
 * Time: 09:54
7
 */
8
9
namespace Iriven\Plugins\Form\Core;
10
11
12
use \Iriven\Plugins\Form\Core\Interfaces\FormInterface;
0 ignored issues
show
Bug introduced by
The type \Iriven\Plugins\Form\Core\Interfaces\FormInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use \Iriven\Plugins\Form\Core\Libs\AttributesBuilder;
0 ignored issues
show
Bug introduced by
The type \Iriven\Plugins\Form\Core\Libs\AttributesBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class form implements FormInterface
16
{
17
    private $attributes;
18
    private $Content = '';
19
    /**
20
     * form constructor.
21
     *
22
     * @param AttributesBuilder|array $attributes
23
     */
24
    public function __construct($attributes = [])
25
    {
26
        if(!$attributes instanceof AttributesBuilder)
27
            $attributes  = new AttributesBuilder($attributes);
28
        $this->attributes = $attributes;
29
        if(!$this->attributes->has('name'))
30
            $this->addName(date('Ymd H:i:s'));
31
        else
32
            $this->addName($this->attributes->get('name'));
33
34
        $this->attributes->set('type','form');
35
36
        if(!$this->attributes->has('enctype'))
37
        $this->attributes->set('enctype','application/x-www-form-urlencoded');
38
39
        if(!$this->attributes->has('method'))
40
        $this->attributes->set('method','post');
41
42
        if(!$this->attributes->has('action'))
43
            $this->attributes->set('action',$_SERVER['REQUEST_URI']);
44
45
        $this->attributes->set('autocomplete','off');
46
47
        $this->attributes->Ignore('form');
48
    }
49
50
    /**
51
     * @return AttributesBuilder
52
     */
53
    public function Attributes()
54
    {
55
        return $this->attributes;
56
    }
57
    /**
58
     * @param $token
59
     * @return $this
60
     */
61
    public function addName($token = null)
62
    {
63
        $token .= microtime(true);
64
        if(strpos($token,'form-')!==0)
65
            $token = 'form-'.$token;
66
        $this->attributes->set('name',md5($token));
67
        return  $this;
68
    }
69
    /**
70
     * @param $element
71
     * @return $this
72
     */
73
    public function append($element)
74
    {
75
        if($element instanceof FormElement)
76
            $this->Content .= $element->RenderHtml();
77
        else
78
            $this->Content .=  $element;
79
        return $this;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function RenderHtml(): string
86
    {
87
        if(strcasecmp($this->attributes->get('method'),'get') == 0)
88
            $this->attributes->remove('enctype');
89
        $html  = '<form';
90
        $html .= $this->attributes->RenderHtml();
91
        $html .= ' >';
92
        $html .= $this->Content;
93
        $html .= '</form>';
94
        return $html;
95
    }
96
97
}
98