Passed
Push — develop ( 77e6e2...97cb86 )
by Daniel
03:55
created

text::is_valid()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services\form\field;
11
12
class text extends base
13
{
14
	/** @var int */
15
	protected $maxlength = 0;
16
17 8
	/**
18
	 * @inheritdoc
19 8
	 */
20
	public function get_name()
21
	{
22
		return 'text';
23
	}
24
25 5
	/**
26
	 * @inheritdoc
27
	 */
28 5
	public function get_default_props()
29 5
	{
30 5
		return array(
31
			'maxlength'	=> 255,
32
			'size'		=> 40,
33
		);
34
	}
35
36
	/**
37
	 * @inheritdoc
38
	 */
39
	public function get_validation_rules(array $data)
40
	{
41
		$this->maxlength = $data['field_props']['maxlength'];
42
43
		return array(
44
			'filter'	=> FILTER_CALLBACK,
45
			'options'	=> array(
46
				'options'	=> array($this, 'is_valid'),
47
			),
48
		);
49
	}
50
51
	/**
52
	 * @param string $value
53
	 * @return false|string
54
	 */
55
	protected function is_valid($value)
56
	{
57
		if ($this->maxlength && utf8_strlen($value) > $this->maxlength)
0 ignored issues
show
Bug introduced by
The function utf8_strlen was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

57
		if ($this->maxlength && /** @scrutinizer ignore-call */ utf8_strlen($value) > $this->maxlength)
Loading history...
58
		{
59
			return false;
60
		}
61
62
		return $value;
63
	}
64
65
	/**
66
	 * @inheritdoc
67
	 */
68
	public function get_error_message(array $data)
69
	{
70
		return $this->language->lang('FIELD_TOO_LONG', $data['field_label'], $this->maxlength);
71
	}
72
}
73