Skip to content

Commit

Permalink
[4.0] Adding filter() and postProcessing() to FormField (#12414)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackwar authored and wilsonge committed Jan 18, 2019
1 parent 28d2fac commit f02b11c
Show file tree
Hide file tree
Showing 12 changed files with 960 additions and 543 deletions.
64 changes: 64 additions & 0 deletions libraries/src/Form/Field/CalendarField.php
Expand Up @@ -13,6 +13,7 @@
use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormField;
use Joomla\CMS\Language\Text;
use Joomla\Registry\Registry;

/**
* Form Field class for the Joomla Platform.
Expand Down Expand Up @@ -317,4 +318,67 @@ protected function getLayoutData()

return array_merge($data, $extraData);
}

/**
* Method to filter a field value.
*
* @param mixed $value The optional value to use as the default for the field.
* @param string $group The optional dot-separated form group path on which to find the field.
* @param Registry $input An optional Registry object with the entire data set to filter
* against the entire form.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter($value, $group = null, Registry $input = null)
{
// Make sure there is a valid SimpleXMLElement.
if (!($this->element instanceof \SimpleXMLElement))
{
throw new \UnexpectedValueException(sprintf('%s::filter `element` is not an instance of SimpleXMLElement', get_class($this)));
}

// Get the field filter type.
$filter = (string) $this->element['filter'];

$return = $value;

switch (strtoupper($filter))
{
// Convert a date to UTC based on the server timezone offset.
case 'SERVER_UTC':
if ((int) $value > 0)
{
// Get the server timezone setting.
$offset = Factory::getConfig()->get('offset');

// Return an SQL formatted datetime string in UTC.
$return = Factory::getDate($value, $offset)->toSql();
}
else
{
$return = '';
}
break;

// Convert a date to UTC based on the user timezone offset.
case 'USER_UTC':
if ((int) $value > 0)
{
// Get the user timezone setting defaulting to the server timezone setting.
$offset = Factory::getUser()->getParam('timezone', Factory::getConfig()->get('offset'));

// Return an SQL formatted datetime string in UTC.
$return = Factory::getDate($value, $offset)->toSql();
}
else
{
$return = '';
}
break;
}

return $return;
}
}
53 changes: 53 additions & 0 deletions libraries/src/Form/Filter/IntarrayFilter.php
@@ -0,0 +1,53 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Form\Filter;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormFilterInterface;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;

/**
* Form Filter class for integer arrays
*
* @since __DEPLOY_VERSION__
*/
class IntarrayFilter implements FormFilterInterface
{
/**
* Method to filter a field value.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param Form $form The form object for which the field is being tested.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
{
if (is_object($value))
{
$value = get_object_vars($value);
}

$value = is_array($value) ? $value : array($value);

$value = ArrayHelper::toInteger($value);

return $value;
}
}
43 changes: 43 additions & 0 deletions libraries/src/Form/Filter/RawFilter.php
@@ -0,0 +1,43 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Form\Filter;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormFilterInterface;
use Joomla\Registry\Registry;

/**
* Form Filter class for raw values
*
* @since __DEPLOY_VERSION__
*/
class RawFilter implements FormFilterInterface
{
/**
* Method to filter a field value.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param Form $form The form object for which the field is being tested.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
{
return $value;
}
}
59 changes: 59 additions & 0 deletions libraries/src/Form/Filter/RulesFilter.php
@@ -0,0 +1,59 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Form\Filter;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormFilterInterface;
use Joomla\Registry\Registry;

/**
* Form Filter class for rules
*
* @since __DEPLOY_VERSION__
*/
class RulesFilter implements FormFilterInterface
{
/**
* Method to filter a field value.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param Form $form The form object for which the field is being tested.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
{
$return = array();

foreach ((array) $value as $action => $ids)
{
// Build the rules array.
$return[$action] = array();

foreach ($ids as $id => $p)
{
if ($p !== '')
{
$return[$action][$id] = ($p == '1' || $p == 'true') ? true : false;
}
}
}

return $return;
}
}
44 changes: 44 additions & 0 deletions libraries/src/Form/Filter/SafehtmlFilter.php
@@ -0,0 +1,44 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Form\Filter;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormFilterInterface;
use Joomla\Registry\Registry;

/**
* Form Filter class for safe HTML
*
* @since __DEPLOY_VERSION__
*/
class SafehtmlFilter implements FormFilterInterface
{
/**
* Method to filter a field value.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param Form $form The form object for which the field is being tested.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
{
return InputFilter::getInstance(null, null, 1, 1)->clean($value, 'html');
}
}
120 changes: 120 additions & 0 deletions libraries/src/Form/Filter/TelFilter.php
@@ -0,0 +1,120 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Form\Filter;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormFilterInterface;
use Joomla\Registry\Registry;

/**
* Form Filter class for phone numbers
*
* @since __DEPLOY_VERSION__
*/
class TelFilter implements FormFilterInterface
{
/**
* Method to filter a field value.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param Form $form The form object for which the field is being tested.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
{
$value = trim($value);

// Does it match the NANP pattern?
if (preg_match('/^(?:\+?1[-. ]?)?\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/', $value) == 1)
{
$number = (string) preg_replace('/[^\d]/', '', $value);

if (substr($number, 0, 1) == 1)
{
$number = substr($number, 1);
}

if (substr($number, 0, 2) == '+1')
{
$number = substr($number, 2);
}

$result = '1.' . $number;
}

// If not, does it match ITU-T?
elseif (preg_match('/^\+(?:[0-9] ?){6,14}[0-9]$/', $value) == 1)
{
$countrycode = substr($value, 0, strpos($value, ' '));
$countrycode = (string) preg_replace('/[^\d]/', '', $countrycode);
$number = strstr($value, ' ');
$number = (string) preg_replace('/[^\d]/', '', $number);
$result = $countrycode . '.' . $number;
}

// If not, does it match EPP?
elseif (preg_match('/^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/', $value) == 1)
{
if (strstr($value, 'x'))
{
$xpos = strpos($value, 'x');
$value = substr($value, 0, $xpos);
}

$result = str_replace('+', '', $value);
}

// Maybe it is already ccc.nnnnnnn?
elseif (preg_match('/[0-9]{1,3}\.[0-9]{4,14}$/', $value) == 1)
{
$result = $value;
}

// If not, can we make it a string of digits?
else
{
$value = (string) preg_replace('/[^\d]/', '', $value);

if ($value != null && strlen($value) <= 15)
{
$length = strlen($value);

// If it is fewer than 13 digits assume it is a local number
if ($length <= 12)
{
$result = '.' . $value;
}
else
{
// If it has 13 or more digits let's make a country code.
$cclen = $length - 12;
$result = substr($value, 0, $cclen) . '.' . substr($value, $cclen);
}
}

// If not let's not save anything.
else
{
$result = '';
}
}

return $result;
}
}

0 comments on commit f02b11c

Please sign in to comment.