<commit_before><?php

namespace Fracture\Http\Parsers;

class DataForm
{

    private $input;
    private $boundry;

    private $factory;

    private $data = [];
    private $stage = null;


    public function __construct(\Fracture\Http\HeaderFactory $factory, $input, $boundry = null)
    {
        $this->factory = $factory;
        $this->input = $input;
        $this->boundry = $boundry;
    }


    public function prepare()
    {
        try {
            $file = new \SPLFileObject($this->input);

            $this->boundry = $this->guessBoundry($file, $this->boundry);

            foreach ($this->collectParameters($file, $this->boundry) as $key => $value) {
                $this->data[$key] = $value;
            }

        } catch (\RuntimeException $e) {
            return false;
        }

    }


    private function guessBoundry($file, $boundry)
    {
        if (false !== strpos($file->current(), $boundry)) {
            return trim($file->current(), "\r\n");
        }
        return null;
    }

    private function collectParameters(\SPLFileObject $file, $boundry)
    {
        $name = null;
        $value = null;

        foreach ($file as $line) {
            if ($line === $boundry .  "--\r\n") {
                // .. then this is the last line
                break;
            }

            if ($line === $boundry . "\r\n") {
                $this->stage = 'headers';
                continue;
            }

            if ($line === "\r\n") {
                $this->stage = 'body';
                continue;
            }

            switch ($this->stage) {
                case 'headers':
                    $entry = $this->readHeader($line);
                    if ($entry && $entry->getFieldName() === 'Content-Disposition') {
                        $name = $entry->getAttribute('name');
                    }
                    break;

                case 'body':
                    yield $name => trim($line, "\r\n");
                    break;
            }
        }
    }


    private function readHeader($line)
    {
        $line = trim($line, "\r\n");
        return $this->factory->create($line);
    }


    private function readBody($line)
    {

    }

    public function getParameter($name)
    {
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }

        return null;
    }
}
<commit_msg>chaning the yield order a bit<commit_after><?php

namespace Fracture\Http\Parsers;

class DataForm
{

    private $input;
    private $boundry;

    private $factory;

    private $data = [];
    private $stage = null;


    public function __construct(\Fracture\Http\HeaderFactory $factory, $input, $boundry = null)
    {
        $this->factory = $factory;
        $this->input = $input;
        $this->boundry = $boundry;
    }


    public function prepare()
    {
        try {
            $file = new \SPLFileObject($this->input);

            $this->boundry = $this->guessBoundry($file, $this->boundry);

            foreach ($this->collectParameters($file, $this->boundry) as $key => $value) {
                $this->data[$key] = $value;
            }

        } catch (\RuntimeException $e) {
            return false;
        }

    }


    private function guessBoundry($file, $boundry)
    {
        if (false !== strpos($file->current(), $boundry)) {
            return trim($file->current(), "\r\n");
        }
        return null;
    }

    private function collectParameters(\SPLFileObject $file, $boundry)
    {
        $name = null;
        $value = null;

        foreach ($file as $line) {
            if ($line === $boundry .  "--\r\n") {
                if (null !== $name) {
                    yield $name => trim($value, "\r\n");
                    $value = null;
                }
                // .. then this is the last line
                break;
            }

            if ($line === $boundry . "\r\n") {
                if (null !== $name) {
                    yield $name => trim($value, "\r\n");
                    $value = null;
                }

                $this->stage = 'headers';
                continue;
            }

            if ($line === "\r\n") {
                $this->stage = 'body';
                continue;
            }

            switch ($this->stage) {
                case 'headers':
                    $entry = $this->readHeader($line);
                    if ($entry && $entry->getFieldName() === 'Content-Disposition') {
                        $name = $entry->getAttribute('name');
                    }
                    break;

                case 'body':
                    $value .= $line;
                    break;
            }
        }
    }


    private function readHeader($line)
    {
        $line = trim($line, "\r\n");
        return $this->factory->create($line);
    }


    public function getParameter($name)
    {
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }

        return null;
    }
}

{'fraction_non_alphanumeric': 0.14797230464886252, 'fraction_numerical': 0.0, 'mean_word_length': 1.3267372296364472, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, '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': '3615394', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1372, 'n_words': 304}