<commit_before><?php

namespace SimpleCrud;

use SimpleCrud\Scheme\Scheme;
use ArrayAccess;
use Iterator;
use Countable;

/**
 * Stores a collection of rows.
 */
class RowCollection extends AbstractRow implements ArrayAccess, Iterator, Countable
{
    private $rows = [];
    private $loadedRelations = [];
    private $idAsKey = true;

    /**
     * Magic method to get properties from all rows.
     *
     * @see self::get()
     */
    public function __get($name)
    {
        $table = $this->getTable();
        $scheme = $table->getScheme();

        //It's a field
        if (isset($scheme['fields'][$name])) {
            $result = [];

            foreach ($this->rows as $id => $row) {
                $result[$id] = $row->$name;
            }

            return $result;
        }

        if (!isset($scheme['relations'][$name])) {
            throw new SimpleCrudException(sprintf('Undefined property "%s"', $name));
        }

        $relation = $scheme['relations'][$name];

        //It's already loaded relation
        if (in_array($name, $this->loadedRelations, true)) {
            $result = $table->createCollection();

            if ($relation[0] === Scheme::HAS_ONE) {
                foreach ($this->rows as $row) {
                    $result[] = $row->$name;
                }

                return $result;
            }

            foreach ($this->rows as $row) {
                foreach ($row->$name as $r) {
                    $result[] = $r;
                }
            }

            return $result;
        }

        //Load the relation
        $result = $this->getDatabase()->{$name}->select()
            ->relatedWith($this)
            ->all()
            ->run();

        $this->__set($name, $result);

        return $result;
    }

    /**
     * Magic method to set properties to all rows.
     *
     * @param string $name
     * @param mixed  $value
     */
    public function __set($name, $value)
    {
        $table = $this->getTable();
        $scheme = $table->getScheme();

        //It's a field
        if (isset($scheme['fields'][$name])) {
            foreach ($this->rows as $row) {
                $row->$name = $value;
            }

            return;
        }

        //It's a relation
        if (!isset($scheme['relations'][$name])) {
            throw new SimpleCrudException(sprintf('Undefined property "%s"'), $name);
        }

        $relation = $scheme['relations'][$name];

        //Check types
        if ($value === null) {
            $value = $table->createCollection();
        } elseif (!($value instanceof self)) {
            throw new SimpleCrudException(sprintf('Invalid value: %s must be a RowCollection instance or null', $name));
        }

        //Join the relations and rows
        switch ($relation[0]) {
            case Scheme::HAS_ONE:
                self::join($row->getTable(), $row, $table, $this->rows, $relation[1]);
                break;

            case Scheme::HAS_MANY:
            case Scheme::HAS_MANY_TO_MANY:
                self::join($table, $this->rows, $row->getTable(), $row, $relation[1]);
                break;
        }

        $this->loadedRelations[] = $name;
    }

    /**
     * Magic method to check if a property is defined or not.
     *
     * @param string $name Property name
     *
     * @return bool
     */
    public function __isset($name)
    {
        return isset($this->getTable()->getScheme()['fields'][$name]) || isset($this->loadedRelations[$name]);
    }

    /**
     * Set whether or not use the id as key.
     *
     * @param bool $idAsKey
     *
     * @return self
     */
    public function idAsKey($idAsKey)
    {
        $this->idAsKey = (boolean) $idAsKey;

        return $this;
    }

    /**
     * @see ArrayAccess
     */
    public function offsetSet($offset, $value)
    {
        if (!($value instanceof Row)) {
            throw new SimpleCrudException('Only instances of SimpleCrud\\Row must be added to collections');
        }

        if ($this->idAsKey === false) {
            $this->rows[] = $value;
<commit_msg>php5 support<commit_after><?php

namespace SimpleCrud;

use SimpleCrud\Scheme\Scheme;
use ArrayAccess;
use Iterator;
use Countable;

/**
 * Stores a collection of rows.
 */
class RowCollection extends AbstractRow implements ArrayAccess, Iterator, Countable
{
    private $rows = [];
    private $loadedRelations = [];
    private $idAsKey = true;

    /**
     * Magic method to get properties from all rows.
     *
     * @see self::get()
     */
    public function __get($name)
    {
        $table = $this->getTable();
        $scheme = $table->getScheme();

        //It's a field
        if (isset($scheme['fields'][$name])) {
            $result = [];

            foreach ($this->rows as $id => $row) {
                $result[$id] = $row->$name;
            }

            return $result;
        }

        if (!isset($scheme['relations'][$name])) {
            throw new SimpleCrudException(sprintf('Undefined property "%s"', $name));
        }

        $relation = $scheme['relations'][$name];

        //It's already loaded relation
        if (in_array($name, $this->loadedRelations, true)) {
            $result = $table->createCollection();

            if ($relation[0] === Scheme::HAS_ONE) {
                foreach ($this->rows as $row) {
                    $result[] = $row->$name;
                }

                return $result;
            }

            foreach ($this->rows as $row) {
                foreach ($row->$name as $r) {
                    $result[] = $r;
                }
            }

            return $result;
        }

        //Load the relation
        $result = $this->getDatabase()->{$name}->select()
            ->relatedWith($this)
            ->all()
            ->run();

        $this->__set($name, $result);

        return $result;
    }

    /**
     * Magic method to set properties to all rows.
     *
     * @param string $name
     * @param mixed  $value
     */
    public function __set($name, $value)
    {
        $table = $this->getTable();
        $scheme = $table->getScheme();

        //It's a field
        if (isset($scheme['fields'][$name])) {
            foreach ($this->rows as $row) {
                $row->$name = $value;
            }

            return;
        }

        //It's a relation
        if (!isset($scheme['relations'][$name])) {
            throw new SimpleCrudException(sprintf('Undefined property "%s"'), $name);
        }

        $relation = $scheme['relations'][$name];

        //Check types
        if ($value === null) {
            $value = $table->createCollection();
        } elseif (!($value instanceof self)) {
            throw new SimpleCrudException(sprintf('Invalid value: %s must be a RowCollection instance or null', $name));
        }

        //Join the relations and rows
        switch ($relation[0]) {
            case Scheme::HAS_ONE:
                self::join($row->getTable(), $row, $table, $this->rows, $relation[1]);
                break;

            case Scheme::HAS_MANY:
            case Scheme::HAS_MANY_TO_MANY:
                self::join($table, $this->rows, $row->getTable(), $row, $relation[1]);
                break;
        }

        $this->loadedRelations[] = $name;
    }

    /**
     * Magic method to check if a property is defined or not.
     *
     * @param string $name Property name
     *
     * @return bool
     */
    public function __isset($name)
    {
        $scheme = $this->getTable()->getScheme();

        return isset($scheme['fields'][$name]) || isset($this->loadedRelations[$name]);
    }

    /**
     * Set whether or not use the id as key.
     *
     * @param bool $idAsKey
     *
     * @return self
     */
    public function idAsKey($idAsKey)
    {
        $this->idAsKey = (boolean) $idAsKey;

        return $this;
    }

    /**
     * @see ArrayAccess
     */
    public function offsetSet($offset, $value)
    {
        if (!($value instanceof Row)) {
            throw new SimpleCrudException('Only instances of SimpleCrud\\Row must be added to collections');
        }

        if ($this->idAsKey === false) {
            $this->rows[] = $value;

{'fraction_non_alphanumeric': 0.15048012641303027, 'fraction_numerical': 0.0010939589157651635, 'mean_word_length': 1.706578947368421, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, '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': '7352791', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2275, 'n_words': 579}