<commit_before><?php
declare(strict_types=1);

namespace Tale;

use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use RuntimeException;

/**
 * Class Stream
 *
 * @package Tale
 */
class Stream implements StreamInterface
{

    /**
     * The default stream mode
     */
    public const DEFAULT_MODE = 'rb+';

    /**
     * The current stream context (file resource)
     *
     * @var resource
     */
    private $context;

    /**
     * An array of meta data information
     *
     * @var array
     */
    private $metadata;


    /**
     * Stream constructor.
     *
     * @param UriInterface|string|resource $context
     * @param string|null $mode
     * @throws \InvalidArgumentException
     */
    public function __construct($context, ?string $mode = null)
    {
        $this->context = $context;
        $mode = $mode ?: self::DEFAULT_MODE;

        if (\is_object($context) && method_exists($context, '__toString')) {
            $this->context = (string)$this->context;
        }

        if (\is_string($this->context)) {
            $this->context = fopen($this->context, $mode);
        }

        if (!\is_resource($this->context)) {
            throw new InvalidArgumentException('Argument 1 needs to be resource or path/URI');
        }

        $this->metadata = stream_get_meta_data($this->context);
    }

    /**<commit_msg>- Opened up context and metadata to be able to use fgetcsv and similar functions directly<commit_after><?php
declare(strict_types=1);

namespace Tale;

use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use RuntimeException;

/**
 * Class Stream
 *
 * @package Tale
 */
class Stream implements StreamInterface
{

    /**
     * The default stream mode
     */
    public const DEFAULT_MODE = 'rb+';

    /**
     * The current stream context (file resource)
     *
     * @var resource
     */
    protected $context;

    /**
     * An array of meta data information
     *
     * @var array
     */
    protected $metadata;


    /**
     * Stream constructor.
     *
     * @param UriInterface|string|resource $context
     * @param string|null $mode
     * @throws \InvalidArgumentException
     */
    public function __construct($context, ?string $mode = null)
    {
        $this->context = $context;
        $mode = $mode ?: self::DEFAULT_MODE;

        if (\is_object($context) && method_exists($context, '__toString')) {
            $this->context = (string)$this->context;
        }

        if (\is_string($this->context)) {
            $this->context = fopen($this->context, $mode);
        }

        if (!\is_resource($this->context)) {
            throw new InvalidArgumentException('Argument 1 needs to be resource or path/URI');
        }

        $this->metadata = stream_get_meta_data($this->context);
    }

    /**
{'fraction_non_alphanumeric': 0.12879834254143646, 'fraction_numerical': 0.0013812154696132596, 'mean_word_length': 2.3962485345838216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '1301731', 'n_tokens_mistral': 837, 'n_tokens_neox': 801, 'n_words': 203}