Completed
Branch master (555f37)
by Alfred
01:38
created

AttributesBuilder::RenderHtml()   B

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;
12
use \Iriven\Plugins\Form\Core\Libs\Traits\KeyNormalizer;
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($attributes)
0 ignored issues
show
Bug Best Practice introduced by
The expression $attributes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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($ignore)
0 ignored issues
show
Bug Best Practice introduced by
The expression $ignore of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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 $key
49
     * @return $this
50
     */
51
    public function createElementID($key)
52
    {
53
        $key = ucfirst($this->normalize($key));
0 ignored issues
show
Bug introduced by
It seems like $this->normalize($key) can also be of type false; however, parameter $str of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $key = ucfirst(/** @scrutinizer ignore-type */ $this->normalize($key));
Loading history...
54
        if(strpos($key,'input')!==0)
55
            $key = 'input'.$key;
56
        $this->set('id',$key);
57
        return  $this;
58
    }
59
60
    /**
61
     * @param $token
62
     * @return $this
63
     */
64
    public function createFormID($token = null)
65
    {
66
        $token or $token = microtime(true);
67
        $token = $this->normalize($token);
68
        if(strpos($token,'form-')!==0)
0 ignored issues
show
Bug introduced by
It seems like $token can also be of type false; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        if(strpos(/** @scrutinizer ignore-type */ $token,'form-')!==0)
Loading history...
69
            $token = 'form-'.$token;
0 ignored issues
show
Bug introduced by
Are you sure $token of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            $token = 'form-'./** @scrutinizer ignore-type */ $token;
Loading history...
70
        $this->set('name',md5($token));
0 ignored issues
show
Bug introduced by
It seems like $token can also be of type false; however, parameter $str of md5() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $this->set('name',md5(/** @scrutinizer ignore-type */ $token));
Loading history...
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 $key
88
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
89
     * @return mixed
90
     */
91
    public function get($key,$default=null)
92
    {
93
        $key = $this->normalize($key);
94
        return $this->attributes->get($key,$default);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type false; however, parameter $key of Iriven\Plugins\Form\Core\Libs\Collection::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
        return $this->attributes->get(/** @scrutinizer ignore-type */ $key,$default);
Loading history...
95
    }
96
97
    /**
98
     * @param $key
99
     * @return bool
100
     */
101
    public function has($key)
102
    {
103
        $key = $this->normalize($key);
104
        return $this->attributes->has($key);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type false; however, parameter $key of Iriven\Plugins\Form\Core\Libs\Collection::has() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
        return $this->attributes->has(/** @scrutinizer ignore-type */ $key);
Loading history...
105
    }
106
    /**
107
     * @param $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 $key
120
     * @return $this
121
     */
122
    public function remove($key)
123
    {
124
        $key = $this->normalize($key);
125
        $this->attributes->remove($key);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type false; however, parameter $key of Iriven\Plugins\Form\Core\Libs\Collection::remove() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
        $this->attributes->remove(/** @scrutinizer ignore-type */ $key);
Loading history...
126
        return $this;
127
    }
128
129
    /**
130
     * @param $key
131
     * @param $value
132
     * @return $this
133
     */
134
    public function set($key,$value)
135
    {
136
        $key = $this->normalize($key);
137
        $this->attributes->set($key,$value);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type false; however, parameter $key of Iriven\Plugins\Form\Core\Libs\Collection::set() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
        $this->attributes->set(/** @scrutinizer ignore-type */ $key,$value);
Loading history...
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
}