<commit_before><?php

namespace Norm;

use Doctrine\Common\Inflector\Inflector;
use Norm\Model;

class Collection implements \JsonSerializable {
    public $clazz;
    public $name;
    public $connection;

    public $filter;

    protected $schema;

    public function __construct(array $options = array()) {
        $this->clazz = Inflector::classify($options['name']);
        $this->name = Inflector::tableize($this->clazz);
        $this->connection = $options['connection'];
    }

    public function schema($schema = NULL) {
        if (is_null($schema)) {
            return $this->schema;
        } else {
            $this->schema = $schema;
        }
    }

    public function hydrate($cursor) {
        $results = array();

        foreach ($cursor as $doc) {
            $doc = $this->connection->prepare($doc);

            $results[] = new Model($doc, array(
                'collection' => $this,
            ));
        }
        return $results;
    }

    public function filter(array $filter = null) {
        if (isset($filter)) {
            if (!isset($this->filter)) {
                $this->filter = array();
            }

            $this->filter = $this->filter + $filter;
        }
    }

    public function find(array $filter = null) {
        $this->filter($filter);

        return $this->hydrate($this->connection->query($this));
    }

    public function findOne(array $filter = null) {
        $this->filter($filter);

        $cursor = $this->connection->query($this);

        if ($cursor->hasNext()) {
            $o = $cursor->getNext();
            return $this->hydrate([$o])[0];
        }
    }

    public function newInstance() {
        return new Model(array(), array('collection' => $this));
    }

    public function save(Model $model) {
        return $this->connection->save($this, $model);
    }

    public function remove(Model $model) {
        return $this->connection->remove($this, $model);
    }

    public function jsonSerialize() {
        return $this->clazz;
    }

}<commit_msg>Collection will have newInstance method with cloned object/array as parameter<commit_after><?php

namespace Norm;

use Doctrine\Common\Inflector\Inflector;
use Norm\Model;

class Collection implements \JsonSerializable {
    public $clazz;
    public $name;
    public $connection;

    public $filter;

    protected $schema;

    public function __construct(array $options = array()) {
        $this->clazz = Inflector::classify($options['name']);
        $this->name = Inflector::tableize($this->clazz);
        $this->connection = $options['connection'];
    }

    public function schema($schema = NULL) {
        if (is_null($schema)) {
            return $this->schema;
        } else {
            $this->schema = $schema;
        }
    }

    public function hydrate($cursor) {
        $results = array();

        foreach ($cursor as $doc) {
            $doc = $this->connection->prepare($doc);

            $results[] = new Model($doc, array(
                'collection' => $this,
            ));
        }
        return $results;
    }

    public function filter(array $filter = null) {
        if (isset($filter)) {
            if (!isset($this->filter)) {
                $this->filter = array();
            }

            $this->filter = $this->filter + $filter;
        }
    }

    public function find(array $filter = null) {
        $this->filter($filter);

        return $this->hydrate($this->connection->query($this));
    }

    public function findOne(array $filter = null) {
        $this->filter($filter);

        $cursor = $this->connection->query($this);

        if ($cursor->hasNext()) {
            $o = $cursor->getNext();
            return $this->hydrate([$o])[0];
        }
    }

    public function newInstance($cloned = array()) {
        if ($cloned instanceof Model) {
            $cloned = $cloned->toArray(Model::FETCH_PUBLISHED);
        }
        return new Model($cloned, array('collection' => $this));
    }

    public function save(Model $model) {
        return $this->connection->save($this, $model);
    }

    public function remove(Model $model) {
        return $this->connection->remove($this, $model);
    }

    public function jsonSerialize() {
        return $this->clazz;
    }

}
{'fraction_non_alphanumeric': 0.15157159487776484, 'fraction_numerical': 0.00046565774155995343, 'mean_word_length': 2.1334792122538295, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 62, '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': '626559', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1191, 'n_words': 266}