1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 /**
15 * Form Rule class for the Joomla Platform.
16 *
17 * @since 11.1
18 */
19 class JFormRuleEquals extends JFormRule
20 {
21 /**
22 * Method to test if two values are equal. To use this rule, the form
23 * XML needs a validate attribute of equals and a field attribute
24 * that is equal to the field to test against.
25 *
26 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
27 * @param mixed $value The form field value to validate.
28 * @param string $group The field name group control value. This acts as an array container for the field.
29 * For example if the field has name="foo" and the group value is set to "bar" then the
30 * full field name would end up being "bar[foo]".
31 * @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
32 * @param JForm $form The form object for which the field is being tested.
33 *
34 * @return boolean True if the value is valid, false otherwise.
35 *
36 * @since 11.1
37 * @throws InvalidArgumentException
38 * @throws UnexpectedValueException
39 */
40 public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null)
41 {
42 $field = (string) $element['field'];
43
44 // Check that a validation field is set.
45 if (!$field)
46 {
47 throw new UnexpectedValueException(sprintf('$field empty in %s::test', get_class($this)));
48 }
49
50 if (is_null($form))
51 {
52 throw new InvalidArgumentException(sprintf('The value for $form must not be null in %s', get_class($this)));
53 }
54
55 if (is_null($input))
56 {
57 throw new InvalidArgumentException(sprintf('The value for $input must not be null in %s', get_class($this)));
58 }
59
60 $test = $input->get($field);
61
62 if (isset($group) && $group !== '')
63 {
64 $test = $input->get($group . '.' . $field);
65 }
66
67 // Test the two values against each other.
68 return $value == $test;
69 }
70 }
71