<commit_before><?php

namespace Wave\Framework\Http;

use Wave\Framework\Interfaces\Http\QueryInterface;
use Wave\Framework\Interfaces\Http\UrlInterface;

class Url implements UrlInterface, \Serializable
{
    const SCHEME_NORM   = 'http';
    const SCHEME_SECURE = 'https';

    private $standardPorts = [
        'https' => 443,
        'http'  => 80
    ];

    protected $scheme    = '';
    protected $host      = '';
    protected $port      = 80;
    protected $path      = '/';
    protected $query     = '';
    protected $fragment  = '';

    public function serialize()
    {
        return serialize([
            'scheme' => $this->scheme,
            'host' => $this->host,
            'port' => $this->port,
            'path' => $this->path,
            'query' => $this->query,
            'fragment' => $this->fragment
        ]);
    }

    public function unserialize($data)
    {
        $data = unserialize($data);
        foreach ($data as $key => $value) {
            if (property_exists($this, '_' . strtolower($key))) {
                $this->{'_' . $key} = $value;
            }
        }
    }

    public function __construct(
        $path = '/',
        QueryInterface $query = null,
        $host = null,
        $port = null,
        $scheme = '',
        $fragment = ''
    ) {
        $this->path = $path;
        $this->query = $query;

        $this->host = (string) $host;
        if (1 <= (int) $port && (int) $port <= 65535) {
            $this->port = (int) $port;
        } else {
            if ($port !== null) {
                throw new \OutOfRangeException(
                    'The provided port is not in the valid port range 1 - 65535'
                );
            }
        }

        if ($this->isStandardPort($this->port)) {
            $this->scheme = array_search($this->port, $this->standardPorts, true);
        } else {
            $scheme = strtolower($scheme);
            if ($scheme === self::SCHEME_NORM || $scheme === self::SCHEME_SECURE || $scheme === '') {
                $this->scheme = $scheme;
            } else {
                throw new \InvalidArgumentException(
                    sprintf('Invalid scheme "%s" provided, valid schemes are "http" or "https"', $scheme)
                );
            }
        }

        $this->fragment = $fragment;
    }

    public function __toString()
    {
        $format = '';
        if ($this->host !== '' && $this->host !== null) {
            $format .= '{scheme}://{host}';
            if (!$this->isStandardPort($this->port)) {
                $format .= ':{port}';
            }
        }
        $format .= '/{path}';

        if ($this->query !== null && count($this->query) > 0) {
            $format .= '?{query}';
        }

        if ($this->fragment !== '') {
            $format .= '#{fragment}';
        }

        return strtr($format, [
            '{scheme}' => $this->scheme,
            '{host}' => $this->host,
            '{port}' => $this->port,
            '{path}' => ltrim($this->path, '/'),
            '{query}' => $this->query,
            '{fragment}' => $this->fragment
        ]);
    }

    public function getScheme()
    {
        return $this->scheme;
    }

    public function getHost()
    {
        return $this->host;
    }

    public function getPort()
    {
        return $this->port;
    }

    public function getPath()
    {
        return $this->path;
    }

    public function getQuery()
    {
        return $this->query;
    }

    public function getFragment()
    {
        return $this->fragment;
    }

    public function setScheme($scheme)
    {
        if (!in_array(strtolower($scheme), [self::SCHEME_SECURE, self::SCHEME_NORM, ''], true)) {
            throw new \InvalidArgumentException(sprintf(
                'Invalid scheme provided. Expected "https", "http" or ""(empty string) "%s" received',
                strtolower($scheme)
            ));
        }

        $self = clone $this;
        $self->scheme = strtolower($scheme);

        return $self;
    }

    public function setHost($host)
    {
        $self = clone $this;
        $self->host = filter_var($host, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_LOW);

        return $self;
    }

    public function setPort($port)
    {
        if (!is_int($port)) {<commit_msg>Changes to reflect intended behaviour in case of invalid parameters and etc.<commit_after><?php

namespace Wave\Framework\Http;

use Wave\Framework\Interfaces\Http\QueryInterface;
use Wave\Framework\Interfaces\Http\UrlInterface;

class Url implements UrlInterface, \Serializable
{
    const SCHEME_NORM   = 'http';
    const SCHEME_SECURE = 'https';

    private $standardPorts = [
        'https' => 443,
        'http'  => 80
    ];

    private $scheme    = '';
    private $host      = '';
    private $port      = 80;
    private $path      = '/';
    private $query     = '';
    private $fragment  = '';

    public function serialize()
    {
        return serialize([
            'scheme' => $this->scheme,
            'host' => $this->host,
            'port' => $this->port,
            'path' => $this->path,
            'query' => $this->query,
            'fragment' => $this->fragment
        ]);
    }

    public function unserialize($data)
    {
        $data = unserialize($data);
        foreach ($data as $key => $value) {
            if (property_exists($this, $key)) {
                $this->$key = $value;
            }
        }
    }

    public function __construct(
        $path = '/',
        QueryInterface $query = null,
        $host = null,
        $port = null,
        $scheme = '',
        $fragment = null
    ) {
        $this->path = $path;
        $this->query = $query;

        $this->host = (string) $host;

        if (!is_int($port) && null !== $port) {
            throw new \InvalidArgumentException(
                'Supplied port argument must be an integer'
            );
        }
        if (1 <= $port && $port <= 65535) {
            $this->port = (int) $port;
        } else {
            if ($port !== null) {
                throw new \OutOfRangeException(
                    'The provided port is not in the valid port range 1 - 65535'
                );
            }
        }

        if ($this->isStandardPort($this->port)) {
            $this->scheme = array_search($this->port, $this->standardPorts, true);
        } else {
            $scheme = strtolower($scheme);
            if ($scheme === self::SCHEME_NORM || $scheme === self::SCHEME_SECURE || $scheme === '') {
                $this->scheme = $scheme;
            } else {
                throw new \InvalidArgumentException(
                    sprintf('Invalid scheme "%s" provided, valid schemes are "http" or "https"', $scheme)
                );
            }
        }

        $this->fragment = $fragment;
    }

    public function __toString()
    {
        $format = '';
        if ($this->host !== '' && $this->host !== null) {
            $format .= '{scheme}://{host}';
            if (!$this->isStandardPort($this->port)) {
                $format .= ':{port}';
            }
        }
        $format .= '/{path}';

        if ($this->query !== null && count($this->query) > 0) {
            $format .= '?{query}';
        }

        if ($this->fragment !== '') {
            $format .= '#{fragment}';
        }

        return strtr($format, [
            '{scheme}' => $this->scheme,
            '{host}' => $this->host,
            '{port}' => $this->port,
            '{path}' => ltrim($this->path, '/'),
            '{query}' => $this->query,
            '{fragment}' => $this->fragment
        ]);
    }

    public function getScheme()
    {
        return $this->scheme;
    }

    public function getHost()
    {
        return $this->host;
    }

    public function getPort()
    {
        return $this->port;
    }

    public function getPath()
    {
        return $this->path;
    }

    public function getQuery()
    {
        return $this->query;
    }

    public function getFragment()
    {
        return $this->fragment;
    }

    public function setScheme($scheme)
    {
        if (!in_array(strtolower($scheme), [self::SCHEME_SECURE, self::SCHEME_NORM, ''], true)) {
            throw new \InvalidArgumentException(sprintf(
                'Invalid scheme provided. Expected "https", "http" or ""(empty string) "%s" received',
                strtolower($scheme)
            ));
        }

        $self = clone $this;
        $self->scheme = strtolower($scheme);

        return $self;
    }

    public function setHost($host)
    {
        $self = clone $this;
        $self->host = filter_var($host, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_LOW);

        return $self;
    }

    public function setPort($port)
    {
        if (!is_int($port) && null !== $port) {
{'fraction_non_alphanumeric': 0.15749887741356083, 'fraction_numerical': 0.00449034575662326, 'mean_word_length': 1.6326832151300237, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5301730', 'n_tokens_mistral': 2659, 'n_tokens_neox': 2551, 'n_words': 569}