Equals::test()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 5
dl 0
loc 30
rs 8.8177
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\Rule;
10
11
use Joomla\Form\Rule;
12
use Joomla\Form\Form;
13
use Joomla\Registry\Registry;
14
use SimpleXMLElement;
15
use UnexpectedValueException;
16
use InvalidArgumentException;
17
18
/**
19
 * Form Rule class for the Joomla Framework.
20
 *
21
 * @since       1.0
22
 * @deprecated  The joomla/form package is deprecated
23
 */
24
class Equals extends Rule
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Form\Rule has been deprecated with message: The joomla/form package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
25
{
26
	/**
27
	 * Method to test if two values are equal. To use this rule, the form
28
	 * XML needs a validate attribute of equals and a field attribute
29
	 * that is equal to the field to test against.
30
	 *
31
	 * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
32
	 * @param   mixed             $value    The form field value to validate.
33
	 * @param   string            $group    The field name group control value. This acts as as an array container for the field.
34
	 *                                      For example if the field has name="foo" and the group value is set to "bar" then the
35
	 *                                      full field name would end up being "bar[foo]".
36
	 * @param   Registry          $input    An optional Registry object with the entire data set to validate against the entire form.
37
	 * @param   Form              $form     The form object for which the field is being tested.
38
	 *
39
	 * @return  boolean  True if the value is valid, false otherwise.
40
	 *
41
	 * @since   1.0
42
	 * @throws  InvalidArgumentException
43
	 * @throws  UnexpectedValueException
44
	 */
45
	public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
46
	{
47
		$field = (string) $element['field'];
48
49
		// Check that a validation field is set.
50
		if (!$field)
51
		{
52
			throw new UnexpectedValueException(sprintf('$field empty in %s::test', get_class($this)));
53
		}
54
55
		if (is_null($form))
56
		{
57
			throw new InvalidArgumentException(sprintf('The value for $form must not be null in %s', get_class($this)));
58
		}
59
60
		if (is_null($input))
61
		{
62
			throw new InvalidArgumentException(sprintf('The value for $input must not be null in %s', get_class($this)));
63
		}
64
65
		$test = $input->get($field);
66
67
		if (isset($group) && $group !== '')
68
		{
69
			$test = $input->get("$group.$field");
70
		}
71
72
		// Test the two values against each other.
73
		return $value == $test;
74
	}
75
}
76