Filter a fragment value to ensure it is properly encoded.

@param null|string $fragment
@return string
439093
private function _filterFragment($fragment)
    {
        if ($fragment && $fragment[0] === '#') {
            $fragment = substr($fragment, 1);
        }

        return $this->_filterQueryOrFragment($fragment);
    }
764824
private function _autoFilter($item)
	{
		# NULL
		if(null === $item) { return null; }

		# WHAT ELSE
		if(is_object($item) || is_resource($item)) {
			return $item;
		}

		# BOOL
		if(is_bool($item) || $item === 'false' || $item === 'true') {
			if($item === 'false') { $item = false; }
			elseif($item === 'true') { $item = true; }
			return $item;
		}

		# ARRAY
		if(is_array($item)) {
			foreach ($item as $k => $v) {
				$item[$k] = $this->_autoFilter($v);
			}
			return $item;
		}

		# INT
		$int = filter_var($item, FILTER_VALIDATE_INT);
		if($int !== false) { return $int; }

		# FLOAT
		if(preg_match('#^([\+\-])?(?!0[0-9]+)[0-9]+\.[0-9]+$#', $item)) {
			return floatval($item);
		}

		# STRING
		return filter_var($item, FILTER_SANITIZE_SPECIAL_CHARS);
	}
1092754
public static function getSegmentRegExp($segment)
        {
            if (($pos = mb_strpos($segment, '|')) !== false) {
                $regexp = mb_substr($segment, $pos + 1);

                if (!mb_strlen($regexp)) return false;

                return '/'.$regexp.'/';
            }

            return false;
        }
674597
protected static function normalizeFragment(?string $fragment): ?string
    {
        if ($fragment === null) {
            return null;
        }

        if (!static::isValidFragment($fragment)) {
            $message = sprintf('Invalid URI fragment: %s', $fragment);
            throw new DomainException($message);
        }

        return static::encodeFragment(static::decode($fragment, static::UNRESERVED_SET));
    }
339160
public function setFragment($fragment = null)
    {
        if (is_string($fragment)) {
            if ($fragment{0} === '#') {
                $fragment = substr($fragment, 1);
            }
        }
        else if (!is_null($fragment)) {
            throw new \InvalidArgumentException(
                "invalid value provided for 'fragment'; ".
                "expecting a string or null"
            );
        }

        $this->fragment = $fragment;
        return $this;
    }
1062313
private static function myValidateFragment(?string $fragment, ?string &$error): bool
    {
        if ($fragment === null) {
            return true;
        }

        if (preg_match('/[^0-9a-zA-Z._~!\$&\'()*\+,;=:@\[\]\/\?%-]/', $fragment, $matches)) {
            $error = 'Fragment "' . $fragment . '" contains invalid character "' . $matches[0] . '".';

            return false;
        }

        return true;
    }
258190
public function withFragment($fragment){
        if(!is_string($fragment)){
            throw new \InvalidArgumentException();
        }

        if("" !== $fragment && 1 !== preg_match(static::REGEX_FRAGMENT, $fragment)){
            throw new \InvalidArgumentException();
        }

        if($this->fragment === $fragment){
            return $this;
        }

        $clone              = clone $this;
        $clone->fragment    = $fragment;

        return $clone;
    }
280551
protected function replace(string $fragment, string $code, string $tpl = null): string
    {
        if (!$tpl)
        {
            $tpl = $this->tpl;
        }

        return \preg_replace(
            '~' . \preg_quote($fragment, '~') . '~u',
            $code,
            $tpl,
            1
        );
    }
163363
protected static function filterFragment($fragment)
    {
        if (! is_string($fragment)) {
            throw new InvalidArgumentException(sprintf(
                'Invalid fragment provided; must be a string argument; received %s',
                (is_object($fragment) ? get_class($fragment) : gettype($fragment))
            ));
        }

        return static::filterQueryOrFragment(ltrim($fragment, '#'));
    }
288494
private function convertToFragments(TableNode $fragments)
    {
        return array_map(
            function ($cell) {
                if (strpos($cell, '@') === 0) {
                    return $this->fragments[substr($cell, 1)];
                }

                return substr($cell, 1, -1); // Removes the quotes.
            },
            $fragments->getRow(0)
        );
    }
329035
public function withFragment(string $fragment): UriInterface
    {
        $instance = clone $this;
        $instance->fragment = $this->marshalQueryAndFragment($fragment);

        return $instance;
    }
910173
public static function segmentIsOptional($segment)
        {
            $name = mb_substr($segment, 1);

            $optMarkerPos = mb_strpos($name, '?');

            if ($optMarkerPos === false) return false;

            $regexMarkerPos = mb_strpos($name, '|');

            if ($regexMarkerPos === false) return true;

            if ($optMarkerPos !== false && $regexMarkerPos !== false) return $optMarkerPos < $regexMarkerPos;

            return false;
        }
659606
public function setFragment($fragment)
    {
        if (preg_match(self::PATTERN_MATCH_FRAGMENT, $fragment) !== 1) {
            throw new \InvalidArgumentException('"' . $fragment . '" is not valid fragment as part of a URI.', 1184071252);
        }

        $this->fragment = $fragment;
    }
91657
public static function getUriSegment(int $segment)
    {
        $segments = self::getUriSegments();
        return isset($segments[$segment]) ? $segments[$segment] : null;
    }
112504
public function formatHomeUrl(string $url, string $fragment = 'cms')
    {
        $length = (int) strlen($fragment) * -1;

        if (substr($url, $length) === $fragment) {
            $url = substr($url, 0, $length);
            $url = rtrim($url, '/');
        }

        return $url;
    }
1264884
public function getUrlForFragment($fragment)
    {
        $newUrl = clone $this;
        $newUrl->_fragments['fragment'] = $fragment;
        $newUrl->_inputString = $newUrl->__toString();
        return $newUrl;
    }
93310
private function filterFragment(string $fragment): string
    {
        if (trim($fragment) == '') {
            return '';
        }

        if ($fragment[0] == '#') {
            $fragment = substr($fragment, 1);
        }

        return preg_replace_callback(
            '/(?:[^'.self::UNRESERVED.self::SUB_DELIMS.':@%~\/\[\]\?]+|%(?![A-Fa-f0-9]{2}))/',
            function (array $match) {
                return rawurlencode($match[0]);
            },
            $fragment
        );
    }