<commit_before><?php

namespace Neutrino\Repositories;

use Neutrino\Interfaces\Repositories\RepositoryInterface;
use Neutrino\Repositories\Exceptions\TransactionException;
use Phalcon\Di\Injectable;

abstract class Repository extends Injectable implements RepositoryInterface
{
    /** @var \Neutrino\Model */
    protected $modelClass;

    /** @var string */
    protected $alias;

    /** @var \Phalcon\Mvc\Model\MessageInterface[] */
    protected $messages = [];

    /** @var \Phalcon\Mvc\Model\Query[] */
    protected $queries = [];

    /**
     * Repository constructor.
     *
     * @param null $modelClass
     *
     * @throws \RuntimeException
     */
    public function __construct($modelClass = null)
    {
        $this->modelClass = is_null($modelClass) ? $this->modelClass : $modelClass;

        if (empty($this->modelClass)) {
            throw new \RuntimeException(static::class . ' must have a $modelClass.');
        }

        $this->alias = \Phalcon\Text::random(\Phalcon\Text::RANDOM_ALPHA, 3);
    }

    /**
     * @return \Phalcon\Mvc\Model\ResultsetInterface|\Neutrino\Model[]
     */
    public function all()
    {
        return $this->getQuery($this->createPhql())->execute();
    }

    /**
     * @param null|array $criteria
     *
     * @return \Phalcon\Mvc\Model\ResultsetInterface|int
     */
    public function count(array $criteria = null)
    {
        $class = $this->modelClass;

        return $class::count($criteria);
    }

    /**
     * @param array      $params
     * @param array|null $orders
     * @param int|null   $limit
     * @param int|null   $offset
     *
     * @return \Neutrino\Model[]|\Phalcon\Mvc\Model\ResultsetInterface
     */
    public function find(array $params = [], array $orders = null, $limit = null, $offset = null)
    {
        return $this
            ->getQuery($this->createPhql(null, $params, $orders, $limit, $offset))
            ->execute($this->createBindParams($params, $limit, $offset));
    }

    /**
     * @param array      $params
     * @param array|null $orders
     * @param int|null   $offset
     *
     * @return \Neutrino\Model|\Phalcon\Mvc\Model
     */
    public function first(array $params = [], array $orders = null, $offset = null)
    {
        $query = $this->getQuery($this->createPhql(null, $params, $orders, null, $offset));

        $query->setUniqueRow(true);

        return $query->execute($this->createBindParams($params, null, $offset));
    }

    /**
     * @param array $params
     * @param bool  $create
     *
     * @param bool  $create
     *
     * @return \Neutrino\Model|\Phalcon\Mvc\Model
     * @throws \Neutrino\Repositories\Exceptions\TransactionException
     */
    public function firstOrNew(array $params = [], $create = false)
    {
        $model = $this->first($params);

        if ($model === false) {
            $class = $this->modelClass;

            $model = new $class;

            foreach ($params as $key => $param) {
                $model->$key = $param;
            }

            if ($create && $this->create($model) === false) {
                throw new TransactionException(__METHOD__ . ': can\'t create model : ' . get_class($model));
            };
        }

        return $model;
    }

    /**
     * @param array $params
     *
     * @return \Neutrino\Model|\Phalcon\Mvc\Model
     * @throws \Neutrino\Repositories\Exceptions\TransactionException
     */
    public function firstOrCreate(array $params = [])
    {
        return $this->firstOrNew($params, true);
    }

    /**
     * @param \Neutrino\Model|\Neutrino\Model[] $value
     *
     * @return bool
     */
    public function create($value)
    {
        return $this->transactionCall(is_array($value) ? $value : [$value], __FUNCTION__);
    }

    /**
     * @param \Neutrino\Model|\Neutrino\Model[] $value
     *
     * @return bool
     */
    public function save($value)
    {
        return $this->transactionCall(is_array($value) ? $value : [$value], __FUNCTION__);
    }

    /**
     * @param \Neutrino\Model|\Neutrino\Model[] $value
     *
     * @return bool
     */
    public function update($value)
    {
        return $this->transactionCall(is_array($value) ? $value : [$value], __FUNCTION__);
    }

    /**
     * @param \Neutrino\Model|\Neutrino\Model[] $value
     *
     * @return bool
     */
    public function delete($value)
    {
        return $this->transactionCall(is_array($value) ? $value : [$value], __FUNCTION__);
    }

    /**
     * Use as :
     * foreach($repository->each() as $model){
     *     // ... do some stuff
     * }
     *
     *
     * @param array    $params
     * @param null|int $start
     * @param null|int $end
     * @param int      $pad
     *
     * @return \Generator|\Neutrino\Model[]
     */<commit_msg>fix(weak warning): Fix some weak warning & phpdoc<commit_after><?php

namespace Neutrino\Repositories;

use Neutrino\Interfaces\Repositories\RepositoryInterface;
use Neutrino\Repositories\Exceptions\TransactionException;
use Phalcon\Di\Injectable;
use Phalcon\Text;

/**
 * Class Repository
 *
 * @package Neutrino\Repositories
 */
abstract class Repository extends Injectable implements RepositoryInterface
{
    /** @var \Neutrino\Model */
    protected $modelClass;

    /** @var string */
    protected $alias;

    /** @var \Phalcon\Mvc\Model\MessageInterface[] */
    protected $messages = [];

    /** @var \Phalcon\Mvc\Model\Query[] */
    protected $queries = [];

    /**
     * Repository constructor.
     *
     * @param null $modelClass
     *
     * @throws \RuntimeException
     */
    public function __construct($modelClass = null)
    {
        $this->modelClass = is_null($modelClass) ? $this->modelClass : $modelClass;

        if (empty($this->modelClass)) {
            throw new \RuntimeException(static::class . ' must have a $modelClass.');
        }

        $this->alias = Text::random(Text::RANDOM_ALPHA, 3);
    }

    /**
     * @return \Phalcon\Mvc\Model\ResultsetInterface|\Neutrino\Model[]
     */
    public function all()
    {
        return $this->getQuery($this->createPhql())->execute();
    }

    /**
     * @param null|array $criteria
     *
     * @return \Phalcon\Mvc\Model\ResultsetInterface|int
     */
    public function count(array $criteria = null)
    {
        $class = $this->modelClass;

        return $class::count($criteria);
    }

    /**
     * @param array      $params
     * @param array|null $orders
     * @param int|null   $limit
     * @param int|null   $offset
     *
     * @return \Neutrino\Model[]|\Phalcon\Mvc\Model\ResultsetInterface
     */
    public function find(array $params = [], array $orders = null, $limit = null, $offset = null)
    {
        return $this
            ->getQuery($this->createPhql(null, $params, $orders, $limit, $offset))
            ->execute($this->createBindParams($params, $limit, $offset));
    }

    /**
     * @param array      $params
     * @param array|null $orders
     * @param int|null   $offset
     *
     * @return \Neutrino\Model|\Phalcon\Mvc\Model
     */
    public function first(array $params = [], array $orders = null, $offset = null)
    {
        $query = $this->getQuery($this->createPhql(null, $params, $orders, null, $offset));

        $query->setUniqueRow(true);

        return $query->execute($this->createBindParams($params, null, $offset));
    }

    /**
     * @param array $params
     * @param bool  $create
     *
     * @return \Neutrino\Model|\Phalcon\Mvc\Model
     * @throws \Neutrino\Repositories\Exceptions\TransactionException
     */
    public function firstOrNew(array $params = [], $create = false)
    {
        $model = $this->first($params);

        if ($model === false) {
            $class = $this->modelClass;

            $model = new $class;

            foreach ($params as $key => $param) {
                $model->$key = $param;
            }

            if ($create && $this->create($model) === false) {
                throw new TransactionException(__METHOD__ . ': can\'t create model : ' . get_class($model));
            };
        }

        return $model;
    }

    /**
     * @param array $params
     *
     * @return \Neutrino\Model|\Phalcon\Mvc\Model
     * @throws \Neutrino\Repositories\Exceptions\TransactionException
     */
    public function firstOrCreate(array $params = [])
    {
        return $this->firstOrNew($params, true);
    }

    /**
     * @param \Neutrino\Model|\Neutrino\Model[] $value
     *
     * @return bool
     */
    public function create($value)
    {
        return $this->transactionCall(is_array($value) ? $value : [$value], __FUNCTION__);
    }

    /**
     * @param \Neutrino\Model|\Neutrino\Model[] $value
     *
     * @return bool
     */
    public function save($value)
    {
        return $this->transactionCall(is_array($value) ? $value : [$value], __FUNCTION__);
    }

    /**
     * @param \Neutrino\Model|\Neutrino\Model[] $value
     *
     * @return bool
     */
    public function update($value)
    {
        return $this->transactionCall(is_array($value) ? $value : [$value], __FUNCTION__);
    }

    /**
     * @param \Neutrino\Model|\Neutrino\Model[] $value
     *
     * @return bool
     */
    public function delete($value)
    {
        return $this->transactionCall(is_array($value) ? $value : [$value], __FUNCTION__);
    }

    /**
     * Use as :
     * foreach($repository->each() as $model){
     *     // ... do some stuff
     * }
     *
     * @param array    $params
     * @param null|int $start
     * @param null|int $end
     * @param int      $pad
     *
     * @return \Generator|\Neutrino\Model[]
     */
{'fraction_non_alphanumeric': 0.1531382483714197, 'fraction_numerical': 0.00020680384655154586, 'mean_word_length': 2.1994707244459146, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, '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': '4613535', 'n_tokens_mistral': 3044, 'n_tokens_neox': 2999, 'n_words': 575}