<commit_before><?php

namespace Palmtree\Collection;

class Collection implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
{
    protected $items;
    protected $type;

    public function __construct($items = [], $type = null)
    {
        $this
            ->set($items)
            ->setType($type);
    }

    public function add($key, $item)
    {
        if ($this->type && class_exists($this->type) && !$item instanceof $this->type) {
            throw new \InvalidArgumentException(sprintf('Item must be of type %s', $this->type));
        }

        $this->items[$key] = $item;

        return $this;
    }

    public function addMany(array $items)
    {
        foreach ($items as $key => $item) {
            $this->add($key, $item);
        }

        return $this;
    }

    public function remove($key)
    {
        unset($this->items[$key]);

        return $this;
    }

    public function set($items)
    {
        $this->items = [];

        return $this->addMany($items);
    }

    public function all()
    {
        return $this->items;
    }

    public function get($key)
    {
        if (!isset($this->items[$key])) {
            throw new \InvalidArgumentException(sprintf("Item '%s' does not exist.", $key));
        }

        return $this->items[$key];
    }

    public function first()
    {
        $items = $this->all();

        return reset($items);
    }

    public function last()
    {
        $items = $this->all();

        return end($items);
    }

    /**
     * @inheritDoc
     */
    public function offsetExists($offset)
    {
        return isset($this->items[$offset]);
    }

    /**
     * @inheritDoc
     */
    public function offsetGet($offset)
    {
        return $this->get($offset);
    }

    /**
     * @inheritDoc
     */
    public function offsetSet($offset, $value)
    {
        $this->add($offset, $value);
    }

    /**
     * @inheritDoc
     */
    public function offsetUnset($offset)
    {
        $this->remove($offset);
    }

    /**
     * @inheritDoc
     */
    public function getIterator()
    {
        return new \ArrayIterator($this->items);
    }

    /**
     * @param null $type
     *
     * @return Collection
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * @return null
     */
    public function getType()
    {
        return $this->type;
    }

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

    /**
     * @inheritDoc
     */
    public function serialize()
    {
        return serialize($this->all());
    }

    /**
     * @inheritDoc
     */
    public function unserialize($serialized)
    {
        $this->set(unserialize($serialized));
    }
}<commit_msg>Rename add to set, addMany to add<commit_after><?php

namespace Palmtree\Collection;

class Collection implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
{
    protected $items;
    protected $type;

    public static $typeMap = [
        'boolean'  => ['boolean', 'bool'],
        'integer'  => ['integer', 'int'],
        'double'   => ['double', 'float'],
        'string'   => ['string'],
        'array'    => ['array'],
        'object'   => ['object'],
        'resource' => ['resource'],
    ];

    public function __construct($items = [], $type = null)
    {
        $this
            ->add($items)
            ->setType($type);
    }

    public function set($key, $item)
    {
        $valid = true;
        if ($this->getType()) {
            if (class_exists($this->getType())) {
                if (!is_a($item, $this->getType())) {
                    $valid = false;
                }
            } else {
                if (is_string($this->getType())) {
                    $type = gettype($item);

                    if (!(isset(static::$typeMap[$type]) && in_array($this->getType(), static::$typeMap[$type]))) {
                        $valid = false;
                    }
                }
            }
        }

        if (!$valid) {
            throw new \InvalidArgumentException(sprintf('Item must be of type %s', $this->getType()));
        }

        $this->items[$key] = $item;

        return $this;
    }

    public function get($key)
    {
        if (!isset($this->items[$key])) {
            throw new \InvalidArgumentException(sprintf("Item '%s' does not exist.", $key));
        }

        return $this->items[$key];
    }

    public function add(array $items)
    {
        foreach ($items as $key => $item) {
            $this->set($key, $item);
        }

        return $this;
    }

    public function remove($key)
    {
        unset($this->items[$key]);

        return $this;
    }

    public function all()
    {
        return $this->items;
    }

    public function first()
    {
        foreach ($this->all() as $item) {
            return $item;
        }

        return null;
    }

    public function last()
    {
        $items = $this->all();

        return ($items) ? array_slice($items, -1)[0] : null;
    }

    /**
     * @inheritDoc
     */
    public function offsetExists($offset)
    {
        return isset($this->items[$offset]);
    }

    /**
     * @inheritDoc
     */
    public function offsetGet($offset)
    {
        return $this->get($offset);
    }

    /**
     * @inheritDoc
     */
    public function offsetSet($offset, $value)
    {
        $this->set($offset, $value);
    }

    /**
     * @inheritDoc
     */
    public function offsetUnset($offset)
    {
        $this->remove($offset);
    }

    /**
     * @inheritDoc
     */
    public function getIterator()
    {
        return new \ArrayIterator($this->items);
    }

    /**
     * @param null $type
     *
     * @return Collection
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * @return null
     */
    public function getType()
    {
        return $this->type;
    }

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

    /**
     * @inheritDoc
     */
    public function serialize()
    {
        return serialize($this->all());
    }

    /**
     * @inheritDoc
     */
    public function unserialize($serialized)
    {
        $this->add(unserialize($serialized));
    }
}
{'fraction_non_alphanumeric': 0.1579111457521434, 'fraction_numerical': 0.0003117692907248636, 'mean_word_length': 1.6241308793456033, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 64, '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': '2926561', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1871, 'n_words': 387}