Rule::test()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 7
nop 5
dl 0
loc 22
rs 9.2568
c 0
b 0
f 0
1
<?php
2
/**
3
 * Part of the Joomla Framework Form Package
4
 *
5
 * @copyright  Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
6
 * @license    GNU General Public License version 2 or later; see LICENSE
7
 */
8
9
namespace Joomla\Form;
10
11
12
use Joomla\Registry\Registry;
13
use SimpleXMLElement;
14
use UnexpectedValueException;
15
16
// Detect if we have full UTF-8 and unicode PCRE support.
17
if (!defined('JCOMPAT_UNICODE_PROPERTIES'))
18
{
19
	define('JCOMPAT_UNICODE_PROPERTIES', (bool) @preg_match('/\pL/u', 'a'));
20
}
21
22
/**
23
 * Form Rule class for the Joomla Framework.
24
 *
25
 * @since       1.0
26
 * @deprecated  The joomla/form package is deprecated
27
 */
28
class Rule
29
{
30
	/**
31
	 * The regular expression to use in testing a form field value.
32
	 *
33
	 * @var    string
34
	 * @since  1.0
35
	 */
36
	protected $regex;
37
38
	/**
39
	 * The regular expression modifiers to use when testing a form field value.
40
	 *
41
	 * @var    string
42
	 * @since  1.0
43
	 */
44
	protected $modifiers;
45
46
	/**
47
	 * Method to test the value.
48
	 *
49
	 * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
50
	 * @param   mixed             $value    The form field value to validate.
51
	 * @param   string            $group    The field name group control value. This acts as as an array container for the field.
52
	 *                                      For example if the field has name="foo" and the group value is set to "bar" then the
53
	 *                                      full field name would end up being "bar[foo]".
54
	 * @param   Registry          $input    An optional Registry object with the entire data set to validate against the entire form.
55
	 * @param   Form              $form     The form object for which the field is being tested.
56
	 *
57
	 * @return  boolean  True if the value is valid, false otherwise.
58
	 *
59
	 * @since   1.0
60
	 * @throws  UnexpectedValueException if rule is invalid.
61
	 */
62
	public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
63
	{
64
		// Check for a valid regex.
65
		if (empty($this->regex))
66
		{
67
			throw new UnexpectedValueException(sprintf('%s has invalid regex.', get_class($this)));
68
		}
69
70
		// Add unicode property support if available.
71
		if (JCOMPAT_UNICODE_PROPERTIES)
72
		{
73
			$this->modifiers = (strpos($this->modifiers, 'u') !== false) ? $this->modifiers : $this->modifiers . 'u';
74
		}
75
76
		// Test the value against the regular expression.
77
		if (preg_match(chr(1) . $this->regex . chr(1) . $this->modifiers, $value))
78
		{
79
			return true;
80
		}
81
82
		return false;
83
	}
84
}
85