<commit_before><?php

namespace CrazyGoat\Octophpus;

use CrazyGoat\Octophpus\Exception\EsiTagParseException;
use CrazyGoat\Octophpus\Validator\EsiAttributeValidator;

class EsiRequest
{
    /**
     * @var string
     */
    private $src;

    /**
     * @var string
     */
    private $esiTag;

    /**
     * @var float|null
     */
    private $timeout = null;

    /**
     * @var bool
     */
    private $noCache = false;

    public function __construct(string $esiTag)
    {
        $this->esiTag = $esiTag;
        $this->parse($esiTag);
    }

    private function parse(string $esiTag) : void
    {
        $p = xml_parser_create();
        $parseStatus = xml_parse_into_struct($p, $esiTag, $values);
        xml_parser_free($p);

        if ($parseStatus == 0) {
            throw new EsiTagParseException('Unable to parse xml: '.$esiTag);
        }

        $parsedTag = $this->getTag($values);

        $validator = new EsiAttributeValidator($parsedTag['attributes']);
        $validator->validate();

        $this->src = $parsedTag['attributes']['SRC'];
        $this->timeout = isset($parsedTag['attributes']['TIMEOUT']) ? (float)$parsedTag['attributes']['TIMEOUT'] : null;
        $this->noCache = isset($parsedTag['attributes']['NOCACHE']);

    }

    private function getTag(array $tags) : array
    {
        if (!is_array($tags) || !isset($tags[0])) {
            throw new EsiTagParseException('No valid html tags found');
        }

        $tag = $tags[0];

        if ($tag['tag'] !== 'ESI:INCLUDE') {
            throw new EsiTagParseException('No valid html tags found');
        }

        if (!isset($tag['attributes']['SRC'])) {
            throw new EsiTagParseException('Esi tag does not have required parameter src');
        }

        return $tag;
    }

    /**
     * @return string
     */
    public function getSrc(): string
    {
        return $this->src;
    }

    /**
     * @return string<commit_msg>add more expects<commit_after><?php

namespace CrazyGoat\Octophpus;

use CrazyGoat\Octophpus\Exception\EsiTagParseException;
use CrazyGoat\Octophpus\Validator\EsiAttributeValidator;
use Nunzion\Expect;

class EsiRequest
{
    /**
     * @var string
     */
    private $src;

    /**
     * @var string
     */
    private $esiTag;

    /**
     * @var float|null
     */
    private $timeout = null;

    /**
     * @var bool
     */
    private $noCache = false;

    public function __construct(string $esiTag)
    {
        $this->esiTag = $esiTag;
        $this->parse($esiTag);
    }

    private function parse(string $esiTag) : void
    {
        $p = xml_parser_create();
        $parseStatus = xml_parse_into_struct($p, $esiTag, $values);
        xml_parser_free($p);

        if ($parseStatus == 0) {
            throw new EsiTagParseException('Unable to parse xml: '.$esiTag);
        }

        $parsedTag = $this->getTag($values);

        $validator = new EsiAttributeValidator($parsedTag['attributes']);
        $validator->validate();

        $this->src = $parsedTag['attributes']['SRC'];
        $this->timeout = isset($parsedTag['attributes']['TIMEOUT']) ? (float)$parsedTag['attributes']['TIMEOUT'] : null;
        $this->noCache = isset($parsedTag['attributes']['NOCACHE']);

    }

    private function getTag(array $tags) : array
    {
        Expect::that($tags)->itsArrayElement(0);
        $tag = $tags[0];

        Expect::that($tag)->isArray();
        Expect::that($tag)->itsArrayElement('tag')->isString()->equals('ESI:INCLUDE');
        Expect::that($tag)->itsArrayElement('attributes')->isArray();
        Expect::that($tag['attributes'])->itsArrayElement('SRC')->isString();

        return $tag;
    }

    /**
     * @return string
     */
    public function getSrc(): string
    {
        return $this->src;
    }

    /**
     * @return string
{'fraction_non_alphanumeric': 0.15840292275574114, 'fraction_numerical': 0.0015657620041753654, 'mean_word_length': 2.465641952983725, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '1157477', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1164, 'n_words': 218}