Text::getBlock()   D
last analyzed

Complexity

Conditions 9
Paths 65

Size

Total Lines 30
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 4.909
c 0
b 0
f 0
cc 9
eloc 11
nc 65
nop 0
1
<?php
2
3
/**
4
 * @package Cadmium\Framework\Form
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Form\Field {
11
12
	use Form, Str, Tag, Template;
13
14
	class Text extends Form\Field {
15
16
		# Field default value
17
18
		protected $value = '';
19
20
		# Field data
21
22
		private $type = FORM_FIELD_TEXT, $maxlength = 0;
23
24
		# Field configuration
25
26
		protected $config = [
27
28
			'multiline'         => false,
29
			'codestyle'         => false,
30
			'spaces'            => '',
31
			'convert'           => '',
32
			'transform'         => '',
33
34
			'placeholder'       => '',
35
			'readonly'          => false,
36
			'autofocus'         => false,
37
			'rows'              => 0,
38
			'cols'              => 0
39
		];
40
41
		/**
42
		 * Get a hidden input tag
43
		 */
44
45
		private function getHidden() : Tag {
46
47
			return $this->getTag('input', ['type' => 'hidden', 'value' => $this->value]);
48
		}
49
50
		/**
51
		 * Get a password input tag
52
		 */
53
54
		private function getPassword() : Tag {
55
56
			return $this->getTag('input', ['type' => 'password', 'value' => '']);
57
		}
58
59
		/**
60
		 * Get a captcha input tag
61
		 */
62
63
		private function getCaptcha() : Tag {
64
65
			return $this->getTag('input', ['type' => 'text', 'value' => '']);
66
		}
67
68
		/**
69
		 * Get a text input tag
70
		 */
71
72
		private function getText() : Tag {
73
74
			return $this->getTag('input', ['type' => 'text', 'value' => $this->value]);
75
		}
76
77
		/**
78
		 * Get a textarea input tag
79
		 */
80
81
		private function getTextarea() : Tag {
82
83
			$tag = $this->getTag('textarea', [], $this->value);
84
85
			if ($this->rows > 0) $tag->setAttribute('rows', $this->rows);
0 ignored issues
show
Documentation introduced by
The property rows does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
86
87
			if ($this->cols > 0) $tag->setAttribute('cols', $this->cols);
0 ignored issues
show
Documentation introduced by
The property cols does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
88
89
			# ------------------------
90
91
			return $tag;
92
		}
93
94
		/**
95
		 * Process spaces
96
		 */
97
98
		private function processSpaces() {
99
100
			if ($this->spaces === 'strip') $this->value = Str::stripSpaces($this->value);
0 ignored issues
show
Documentation introduced by
The property spaces does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
102
			else if ($this->spaces === 'single') $this->value = Str::singleSpaces($this->value);
0 ignored issues
show
Documentation introduced by
The property spaces does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
		}
104
105
		/**
106
		 * Process convert
107
		 */
108
109
		private function processConvert() {
110
111
			if ($this->convert === 'url') $this->value = Str::toUrl($this->value, $this->maxlength);
0 ignored issues
show
Documentation introduced by
The property convert does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
112
113
			else if ($this->convert === 'var') $this->value = Str::toVar($this->value, $this->maxlength);
0 ignored issues
show
Documentation introduced by
The property convert does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
114
		}
115
116
		/**
117
		 * Process case transform
118
		 */
119
120
		private function processTransform() {
121
122
			if ($this->transform === 'lower') $this->value = Str::toLower($this->value);
0 ignored issues
show
Documentation introduced by
The property transform does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
123
124
			else if ($this->transform === 'upper') $this->value = Str::toUpper($this->value);
0 ignored issues
show
Documentation introduced by
The property transform does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
125
		}
126
127
		/**
128
		 * Constructor
129
		 */
130
131
		public function __construct(Form $form, string $key, string $value = '',
132
133
		    string $type = FORM_FIELD_TEXT, int $maxlength = 0, array $config = []) {
134
135
			# Init field
136
137
			self::init($form, $key, $config);
138
139
			# Set data
140
141
			$this->type = $type; $this->maxlength = $maxlength;
142
143
			# Set value
144
145
			$this->setValue($value);
146
		}
147
148
		/**
149
		 * Set a value
150
		 *
151
		 * @return bool : true if the result value is not empty, otherwise false
152
		 */
153
154
		public function setValue(string $value) : bool {
155
156
			if (($this->type === FORM_FIELD_PASSWORD) || ($this->type === FORM_FIELD_CAPTCHA)) {
157
158
				$this->value = Str::substr($value, 0, $this->maxlength);
159
160
			} else {
161
162
				$multiline = (($this->type === FORM_FIELD_TEXTAREA) && $this->multiline);
0 ignored issues
show
Documentation introduced by
The property multiline does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
163
164
				$codestyle = (($this->type === FORM_FIELD_TEXTAREA) && $this->codestyle);
0 ignored issues
show
Documentation introduced by
The property codestyle does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
165
166
				$this->value = Str::formatInput($value, $this->maxlength, $multiline, $codestyle);
167
168
				# Process operations
169
170
				$this->processSpaces(); $this->processConvert(); $this->processTransform();
171
			}
172
173
			# ------------------------
174
175
			return ('' !== $this->value);
176
		}
177
178
		/**
179
		 * Get a block
180
		 */
181
182
		public function getBlock() : Template\Block {
183
184
			# Process hidden field
185
186
			if ($this->type === FORM_FIELD_HIDDEN) return $this->getHidden()->getBlock();
187
188
			# Process visible field
189
190
			else if ($this->type === FORM_FIELD_PASSWORD) $tag = $this->getPassword();
191
192
			else if ($this->type === FORM_FIELD_CAPTCHA) $tag = $this->getCaptcha();
193
194
			else if ($this->type === FORM_FIELD_TEXTAREA) $tag = $this->getTextarea();
195
196
			else $tag = $this->getText();
197
198
			# Set appearance
199
200
			if ($this->maxlength > 0) $tag->setAttribute('maxlength', $this->maxlength);
201
202
			if ('' !== $this->placeholder) $tag->setAttribute('placeholder', $this->placeholder);
0 ignored issues
show
Documentation introduced by
The property placeholder does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
203
204
			if ($this->readonly) $tag->setAttribute('readonly', 'readonly');
0 ignored issues
show
Documentation introduced by
The property readonly does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
205
206
			if ($this->autofocus) $tag->setAttribute('autofocus', 'autofocus');
0 ignored issues
show
Documentation introduced by
The property autofocus does not exist on object<Form\Field\Text>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
207
208
			# ------------------------
209
210
			return $tag->getBlock();
211
		}
212
	}
213
}
214