<commit_before><?php

namespace Innmind\UrlResolver;

use Innmind\UrlResolver\Exception\ResolutionException;
use Innmind\UrlResolver\Exception\UrlException;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\Validator\Constraints\Url;
use Pdp\Parser;
use Pdp\PublicSuffixListManager;
use Pdp\Uri\Url as ParsedUrl;

class UrlResolver implements ResolverInterface
{
    protected $parser;
    protected $validator;
    protected $constraint;

    public function __construct(
        array $protocols,
        Parser $parser = null,
        ValidatorInterface $validator = null
    ) {
        if (empty($protocols)) {
            $protocols = ['http', 'https'];
        }

        if ($parser === null) {
            $parser = new Parser(
                (new PublicSuffixListManager)->getList()
            );
        }

        if ($validator === null) {
            $validator = Validation::createValidator();
        }

        $this->constraint = new Url(['protocols' => $protocols]);
        $this->parser = $parser;
        $this->validator = $validator;
    }

    /**
     * {@inheritdoc}
     */
    public function resolve($origin, $destination)
    {
        $origin = (string) $origin;
        $destination = $this->appendProtocol((string) $destination);
        $violations = $this
            ->validator
            ->validate(
                $destination,
                [$this->constraint]
            );

        if ($violations->count() === 0) {
            return $destination;
        }

        $origin = $this->appendProtocol($origin);

        $violations = $this
            ->validator
            ->validate(
                $origin,
                [$this->constraint]
            );

        if ($violations->count() > 0) {
            throw new UrlException(sprintf(
                'The origin variable is not a url (given: %s)',
                $origin
            ));
        }

        $origin = $this->parser->parseUrl($origin);

        switch (true) {
            case substr($destination, 0, 1) === '?':
                return $this->buildQueryString($origin, $destination);
            case substr($destination, 0, 1) === '#':
                return $this->buildFragment($origin, $destination);
            case substr($destination, 0, 1) === '/':
                return $this->buildAbsoluteUrl($origin, $destination);
            case substr($destination, 0, 2) === './':
                $destination = substr($destination, 2);<commit_msg>remove useless parsing<commit_after><?php

namespace Innmind\UrlResolver;

use Innmind\UrlResolver\Exception\ResolutionException;
use Innmind\UrlResolver\Exception\UrlException;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\Validator\Constraints\Url;
use Pdp\Parser;
use Pdp\PublicSuffixListManager;
use Pdp\Uri\Url as ParsedUrl;

class UrlResolver implements ResolverInterface
{
    protected $parser;
    protected $validator;
    protected $constraint;

    public function __construct(
        array $protocols,
        Parser $parser = null,
        ValidatorInterface $validator = null
    ) {
        if (empty($protocols)) {
            $protocols = ['http', 'https'];
        }

        if ($parser === null) {
            $parser = new Parser(
                (new PublicSuffixListManager)->getList()
            );
        }

        if ($validator === null) {
            $validator = Validation::createValidator();
        }

        $this->constraint = new Url(['protocols' => $protocols]);
        $this->parser = $parser;
        $this->validator = $validator;
    }

    /**
     * {@inheritdoc}
     */
    public function resolve($origin, $destination)
    {
        $origin = (string) $origin;
        $destination = $this->appendProtocol((string) $destination);
        $violations = $this
            ->validator
            ->validate(
                $destination,
                [$this->constraint]
            );

        if ($violations->count() === 0) {
            return $destination;
        }

        $origin = $this->appendProtocol($origin);

        $violations = $this
            ->validator
            ->validate(
                $origin,
                [$this->constraint]
            );

        if ($violations->count() > 0) {
            throw new UrlException(sprintf(
                'The origin variable is not a url (given: %s)',
                $origin
            ));
        }

        switch (true) {
            case substr($destination, 0, 1) === '?':
                return $this->buildQueryString($origin, $destination);
            case substr($destination, 0, 1) === '#':
                return $this->buildFragment($origin, $destination);
            case substr($destination, 0, 1) === '/':
                return $this->buildAbsoluteUrl($origin, $destination);
            case substr($destination, 0, 2) === './':
                $destination = substr($destination, 2);
{'fraction_non_alphanumeric': 0.13202692003167063, 'fraction_numerical': 0.00435471100554236, 'mean_word_length': 1.939499709133217, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, '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': '5954706', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1291, 'n_words': 291}