Back to CalendarField class

Method filter

public mixed
filter
(mixed $value, mixed $group = null, \Joomla\Registry\Registry $input = null)
Method to filter a field value.
Parameters
  • mixed $value The optional value to use as the default for the field.
  • string $group The optional dot-separated form group path on which to find the field.
  • \Joomla\Registry\Registry $input An optional Registry object with the entire data set to filter against the entire form.
Returns
  • mixed The filtered value.
Since
  • 4.0.0
Class: CalendarField
Project: Joomla

Method filter - Source code

/**
 * 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   4.0.0
 */
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)));
    }
    if ((int) $value <= 0) {
        return '';
    }
    if ($this->filterFormat) {
        $value = DateTime::createFromFormat($this->filterFormat, $value)->format('Y-m-d H:i:s');
    }
    $app = Factory::getApplication();
    // 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':
            // Return an SQL formatted datetime string in UTC.
            $return = Factory::getDate($value, $app->get('offset'))->toSql();
            break;
        // Convert a date to UTC based on the user timezone offset.
        case 'USER_UTC':
            // Get the user timezone setting defaulting to the server timezone setting.
            $offset = $app->getIdentity()->getParam('timezone', $app->get('offset'));
            // Return an SQL formatted datetime string in UTC.
            $return = Factory::getDate($value, $offset)->toSql();
            break;
    }
    return $return;
}