AttributesBuilder::RenderHtml()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 21
rs 8.8333
cc 7
nc 6
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sjhc1170
5
 * Date: 07/05/2018
6
 * Time: 09:34
7
 */
8
9
namespace Iriven\Plugins\Form\Core\Libs;
10
11
use \Iriven\Plugins\Form\Core\Interfaces\AttributesBuilderInterface;
0 ignored issues
show
Bug introduced by
The type \Iriven\Plugins\Form\Cor...ributesBuilderInterface 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...
12
use \Iriven\Plugins\Form\Core\Libs\Traits\KeyNormalizer;
0 ignored issues
show
Bug introduced by
The type \Iriven\Plugins\Form\Cor...bs\Traits\KeyNormalizer 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
14
class AttributesBuilder implements AttributesBuilderInterface
15
{
16
    use KeyNormalizer;
17
18
    private $attributes;
19
    private $ignore ;
20
21
    /**
22
     * AttributesBuilder constructor.
23
     *
24
     * @param array $attributes
25
     * @param array $ignore
26
     */
27
    public function __construct(array $attributes, $ignore = [])
28
    {
29
        $this->attributes    = new Collection();
30
        $this->ignore        = new Collection();
31
        if(!empty($attributes))
32
        {
33
            is_array($attributes) or $attributes = array($attributes);
34
            $attributes = array_change_key_case($attributes,CASE_LOWER);
35
            $attributes = $this->array_map_keys([$this,'normalize'],$attributes);
36
            $this->attributes->add($attributes);
37
        }
38
        if(!empty($ignore))
39
        {
40
            is_array($ignore) or $ignore = array($ignore);
41
            $ignore = array_map([$this,'normalize'],$ignore);
42
            $this->ignore->add($ignore);
43
        }
44
        return $this;
45
    }
46
47
    /**
48
     * @param string $key
49
     * @return $this
50
     */
51
    public function createElementID($key)
52
    {
53
        $key = ucfirst($this->normalize($key));
54
        if(strpos($key,'input')!==0)
55
            $key = 'input'.$key;
56
        $this->set('id',$key);
57
        return  $this;
58
    }
59
60
    /**
61
     * @param string $token
62
     * @return $this
63
     */
64
    public function createFormID($token = null)
65
    {
66
        !empty($token) or $token = microtime(true);
67
        $token = $this->normalize($token);
68
        if(strpos($token,'form-')!==0)
69
            $token = 'form-'.$token;
70
        $this->set('name',md5($token));
71
        return  $this;
72
    }
73
74
    /**
75
     * @param array $attributes
76
     * @return $this
77
     */
78
    public function add(array $attributes)
79
    {
80
        is_array($attributes) or $attributes = array($attributes);
81
        $attributes = $this->array_map_keys([$this,'normalize'],$attributes);
82
        $this->attributes->add($attributes);
83
        return $this;
84
    }
85
86
    /**
87
     * @param string $key
88
     * @param $default
89
     * @return mixed
90
     */
91
    public function get($key,$default=null)
92
    {
93
        $key = $this->normalize($key);
94
        return $this->attributes->get($key,$default);
95
    }
96
97
    /**
98
     * @param string $key
99
     * @return bool
100
     */
101
    public function has($key)
102
    {
103
        $key = $this->normalize($key);
104
        return $this->attributes->has($key);
105
    }
106
    /**
107
     * @param mixed $ignore
108
     * @return $this
109
     */
110
    public function Ignore($ignore)
111
    {
112
        is_array($ignore) or $ignore = array($ignore);
113
        $ignore = array_merge($this->ignore->all(), array_map([$this,'normalize'],$ignore));
114
        $this->ignore->replace($ignore);
115
        return $this;
116
    }
117
118
    /**
119
     * @param string $key
120
     * @return $this
121
     */
122
    public function remove($key)
123
    {
124
        $key = $this->normalize($key);
125
        $this->attributes->remove($key);
126
        return $this;
127
    }
128
129
    /**
130
     * @param string $key
131
     * @param mixed $value
132
     * @return $this
133
     */
134
    public function set($key,$value)
135
    {
136
        $key = $this->normalize($key);
137
        $this->attributes->set($key,$value);
138
        return $this;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function All()
145
    {
146
        return $this->attributes->all();
147
    }
148
149
    /**
150
     * @return bool|null|string
151
     */
152
    public function RenderHtml()
153
    {
154
        $output = [];
155
        if($attributes = $this->attributes->all())
156
        {
157
            AttributesMapper::filter($this);
158
            $filtered = array_keys($attributes);
159
            if($ignore = $this->ignore->all())
160
                $filtered = array_diff($filtered, $ignore);
161
            $attributes = array_intersect_key($attributes, array_flip($filtered));
162
            foreach($attributes as $key=>$value)
163
            {
164
                if(is_array($value)):
165
                    $output[]= $key.'="'.implode(' ',array_values($value)).'"';
166
                else:
167
                    if(is_numeric($key)) $output[]= $value;
168
                    else $output[]= $key.'="'.$value.'"';
169
                endif;
170
            }
171
        }
172
        return count($output) ? ' ' . implode(' ', $output) : '';
173
    }
174
}
175