<commit_before><?php

namespace Harp\Core\Model;

use Harp\Util\Objects;
use SplObjectStorage;
use Closure;
use Countable;
use Iterator;
use LogicException;

/*
 * @author     <NAME>
 * @copyright  (c) 2014 Clippings Ltd.
 * @license    http://www.opensource.org/licenses/isc-license.txt
 */
class Models implements Countable, Iterator
{
    /**
     * @var SplObjectStorage
     */
    private $models;

    public function __construct(array $models = null)
    {
        $this->models = new SplObjectStorage();

        if ($models) {
            $this->addArray($models);
        }
    }

    /**
     * Clone internal SplObjectStorage
     */
    public function __clone()
    {
        $this->models = clone $this->models;
    }

    /**
     * @param  SplObjectStorage $models
     * @return Models           $this
     */
    public function addObjects(SplObjectStorage $models)
    {
        foreach ($models as $model) {
            $this->add($model);
        }

        return $this;
    }

    public function addAll(Models $other)
    {
        if ($other->count() > 0) {
            $this->models->addAll($other->models);
        }

        return $this;
    }

    /**
     * @param  array  $models
     * @return Models $this
     */
    public function addArray(array $models)
    {
        foreach ($models as $model) {
            $this->add($model);
        }

        return $this;
    }

    /**
     * @param  AbstractModel $model
     * @return Models        $this
     */
    public function add(AbstractModel $model)
    {
        $this->models->attach($model);

        return $this;
    }

    /**
     * @return Models $this
     */
    public function clear()
    {
        $this->models = new SplObjectStorage();

        return $this;
    }

    /**
     * @param  AbstractModel $model
     * @return boolean
     */
    public function has(AbstractModel $model)
    {
        return $this->models->contains($model);
    }

    /**
     * @param  string|integer $id
     * @return boolean
     */
    public function hasId($id)
    {
        return array_search($id, $this->getIds()) !== false;
    }

    /**
     * @return AbstractModel|null
     */
    public function getFirst()
    {
        $this->models->rewind();

        return $this->models->current();
    }

    /**
     * @return AbstractModel|null
     */
    public function getNext()
    {
        $this->models->next();

        return $this->models->current();
    }

    /**
     * @return int
     */
    public function count()
    {
        return $this->models->count();
    }

    /**
     * Return containing models
     *
     * @return SplObjectStorage
     */
    public function all()
    {
        return $this->models;
    }

    /**
     * @throws LogicException If a model is invalid
     * @return Models $this
     */
    public function assertValid()
    {
        foreach ($this->models as $model) {
            if (! $model->validate()) {
                throw new LogicException(
                    sprintf('Model contains errors %s', $model->getErrors()->humanize())
                );
            }
        }

        return $this;
    }

    /**
     * @return array
     */
    public function toArray()
    {
        return Objects::toArray($this->models);
    }

    /**
     * @param  AbstractModel $model<commit_msg>More readable exception<commit_after><?php

namespace Harp\Core\Model;

use Harp\Util\Objects;
use SplObjectStorage;
use Closure;
use Countable;
use Iterator;
use LogicException;

/*
 * @author     <NAME>
 * @copyright  (c) 2014 Clippings Ltd.
 * @license    http://www.opensource.org/licenses/isc-license.txt
 */
class Models implements Countable, Iterator
{
    /**
     * @var SplObjectStorage
     */
    private $models;

    public function __construct(array $models = null)
    {
        $this->models = new SplObjectStorage();

        if ($models) {
            $this->addArray($models);
        }
    }

    /**
     * Clone internal SplObjectStorage
     */
    public function __clone()
    {
        $this->models = clone $this->models;
    }

    /**
     * @param  SplObjectStorage $models
     * @return Models           $this
     */
    public function addObjects(SplObjectStorage $models)
    {
        foreach ($models as $model) {
            $this->add($model);
        }

        return $this;
    }

    public function addAll(Models $other)
    {
        if ($other->count() > 0) {
            $this->models->addAll($other->models);
        }

        return $this;
    }

    /**
     * @param  array  $models
     * @return Models $this
     */
    public function addArray(array $models)
    {
        foreach ($models as $model) {
            $this->add($model);
        }

        return $this;
    }

    /**
     * @param  AbstractModel $model
     * @return Models        $this
     */
    public function add(AbstractModel $model)
    {
        $this->models->attach($model);

        return $this;
    }

    /**
     * @return Models $this
     */
    public function clear()
    {
        $this->models = new SplObjectStorage();

        return $this;
    }

    /**
     * @param  AbstractModel $model
     * @return boolean
     */
    public function has(AbstractModel $model)
    {
        return $this->models->contains($model);
    }

    /**
     * @param  string|integer $id
     * @return boolean
     */
    public function hasId($id)
    {
        return array_search($id, $this->getIds()) !== false;
    }

    /**
     * @return AbstractModel|null
     */
    public function getFirst()
    {
        $this->models->rewind();

        return $this->models->current();
    }

    /**
     * @return AbstractModel|null
     */
    public function getNext()
    {
        $this->models->next();

        return $this->models->current();
    }

    /**
     * @return int
     */
    public function count()
    {
        return $this->models->count();
    }

    /**
     * Return containing models
     *
     * @return SplObjectStorage
     */
    public function all()
    {
        return $this->models;
    }

    /**
     * @throws LogicException If a model is invalid
     * @return Models $this
     */
    public function assertValid()
    {
        foreach ($this->models as $model) {
            if (! $model->validate()) {
                throw new LogicException(
                    sprintf('%s contains errors: %s', $model->getRepo()->getName(), $model->getErrors()->humanize())
                );
            }
        }

        return $this;
    }

    /**
     * @return array
     */
    public function toArray()
    {
        return Objects::toArray($this->models);
    }

    /**
     * @param  AbstractModel $model
{'fraction_non_alphanumeric': 0.13666072489601902, 'fraction_numerical': 0.0014854426619132501, 'mean_word_length': 1.691047162270184, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2808305', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1923, 'n_words': 434}