Sanitize a value

@param string $value
@param string $filter
@throws \InvalidArgumentException if invalid $value type supplied
@throws \UnexpectedValueException If unknown filter type supplied
@return string
489538
public static function sanitize($value, $filter = self::FILTER_NONE)
    {
        if (!is_scalar($value) && !is_null($value)) {
            throw new InvalidArgumentException(sprintf('Sanitizer can only handle scalar values. Received "%s"', gettype($value)));
        }

        switch ($filter) {
            case self::FILTER_NONE;
                return $value;
            case self::FILTER_BOOL:
                return (bool) $value;
            case self::FILTER_FLOAT:
                return (float) $value;
            case self::FILTER_INT:
                return (int) $value;
            case self::FILTER_HTML:
                return self::escape($value);
            case self::FILTER_TAGS:
                return self::stripTags($value);
            case self::FILTER_SAFE_TAGS:
                return self::safeTags($value);
            case self::FILTER_HTML_CHARS:
                return self::specialChars($value);
            default:
                throw new UnexpectedValueException('Unknown filter type provided');
        }
    }
440729
public function sanitise_array($value)
    {
        if (\is_array($value)) {
            return $value;
        } else {
            if ($value instanceof File) {
                return $value;
            }

            return \sanitize_textarea_field($value);
        }
    }
1343080
public function filter($value)
    {
        $filtered = null;
        if (!$this->validate($value, $filtered))
        {
            throw new \InvalidArgumentException(
                "Not a valid value for " . $this->__toString() . ": " . WF::str($value)
            );
        }

        return $filtered;
    }
687808
public function getFilterValue($value)
    {
        if (($value !== 0 && $value !== '0' && empty($value)) || (is_string($value) && trim($value) === '')) {
            return false;
        } else {
            return $value;
        }
    }
1219293
protected function prepareHeaderValue($value)
    {
        if (is_string($value)) {
            $value = [$value];
        }
        if (! is_array($value)) {
            throw new InvalidArgumentException(sprintf(
                'Invalid header value "%s"; must be a string or array of strings.',
                is_object($value) ? get_class($value) : gettype($value)
            ));
        }
        foreach ($value as &$item) {
            if (! is_string($item)) {
                throw new InvalidArgumentException(sprintf(
                    'Invalid header value "%s"; must be a string.',
                    is_object($item) ? get_class($item) : gettype($item)
                ));
            }

            $item = trim($item);

            if ('' === $item) {
                throw new InvalidArgumentException(
                    'Empty header value provided.'
                );
            }

            if (preg_match('/[^\x09\x0a\x0d\x20-\x7E\x80-\xFE]/', $item)) {
                throw new InvalidArgumentException(sprintf(
                    'The header value "%s" contains illegal characters.',
                    $item
                ));
            }
            $item = preg_replace('/\s+/', ' ', $item);
        }

        return $value;
    }
1368110
private function addFilter($filter)
    {
        if (is_array($filter)) {
            if (!isset($filter['method'])) {
                throw new \InvalidArgumentException(
                    'A [method] value must be specified for each complex filter'
                );
            }
        }

        if (!$this->filters) {
            $this->filters = [$filter];
        } else {
            $this->filters[] = $filter;
        }

        return $this;
    }
823011
protected function sanitizeValue($input, Sanitize $sanitize = null)
    {
        if (!isset($sanitize)) {
            $sanitize = $this->sanitize ?: Sanitize::removeTags()->trim()->toType();
        }

        if (is_array($input)) {
            $rawRule = $sanitize->getRawRules();
            $rawRule = current($rawRule);
            if ($rawRule instanceof Attributes) {
                return $sanitize->sanitize($input);
            }
            return Sanitize::attributes($sanitize)->sanitize($input);
        }

        return $sanitize->sanitize($input);
    }
357070
public function sanitize( $value ) {
		if ( is_array( $value ) ) {
			foreach ( $value as $key => $subvalue ) {
				if ( '' !== $subvalue || isset( $this->choices[''] ) ) {
					$key           = sanitize_key( $key );
					$value[ $key ] = sanitize_text_field( $subvalue );
				}
			}
			return $value;
		}
		return sanitize_text_field( $value );
	}
463452
public static function sanitize( $value ) {
		$value = ( ! is_array( $value ) ) ? explode( ',', $value ) : $value;
		return ( ! empty( $value ) ) ? array_map( 'sanitize_text_field', $value ) : [];
	}
125588
public function sanitize($value)
    {
        if(!$value instanceof ObjectConfig)
        {
            if(is_string($value)) {
                $value = $this->getObject('object.config.factory')->fromString('ini', $value);
            } else {
                $value = $this->getObject('object.config.factory')->createFormat('ini', $value);
            }
        }

        return $value;
    }
719436
public static function sanitize($value)
    {
        if (is_array($value) || is_object($value))
        {
            foreach ($value as $key => $val)
            {
                $value[$key] = self::sanitize($val);
            }
        }
        elseif (is_string($value))
        {
            $value = stripslashes($value);
            if (false !== strpos($value, "\r"))
            {
                $value = str_replace([
                    "\r\n",
                    "\r"
                ], "\n", $value);
            }
        }

        return $value;
    }
404293
private function filterPair($value)
    {
        if (null === $value || is_scalar($value)) {
            return $value;
        }

        if ($value instanceof UriComponentInterface) {
            return $value->getContent();
        }

        if (method_exists($value, '__toString')) {
            return (string) $value;
        }

        throw new TypeError('The submitted value is invalid.');
    }
262761
public function sanitize($value)
    {
        if(!$value instanceof KObjectConfigJson)
        {
            if(is_string($value)) {
                $value = $this->getObject('object.config.factory')->fromString('json', $value);
            } else {
                $value = $this->getObject('object.config.factory')->createFormat('json', $value);
            }
        }

        return $value;
    }
182508
protected function _sanitize($state)
    {
        //Treat empty string as null
        if ($state->value === '') {
            $state->value = null;
        }

        //Only filter if we have a value
        if(!empty($state->value))
        {
            //Only accepts scalar values and array
            if(!is_scalar($state->value) && !is_array($state->value))
            {
                throw new \UnexpectedValueException(
                    'Value needs to be an array or a scalar, "'.gettype($state->value).'" given'
                );
            }

            $filter = $state->filter;

            if(!($filter instanceof FilterInterface)) {
                $filter =  $this->getObject('filter.factory')->createChain($filter);
            }

            $state->value = $filter->sanitize($state->value);
        }

        return $state;
    }
74832
protected function _sanitize($value)
    {
        if (null === $this->_charList) {
            return trim((string) $value);
        } else {
            return trim((string) $value, $this->_charList);
        }
    }
558915
public function sanitize($value)
    {
        $value = (string) $value;

        // Filter var for XSS and other 'bad' code etc.
        if (!empty ($value)) {
            $value = $this->_remove($this->_decode($value));
        }

        return $value;
    }
91166
public function sanitize()
    {
        $value = filter_var($this->value, FILTER_SANITIZE_STRING);

        if (false === $value) {
            throw FailedToSanitize::fromValueForClass($this->value, static::class);
        }

        return $value;
    }