<commit_before><?php
/**
 * Created by PhpStorm.
 * User: Alexandre
 * Date: 29/09/2017
 * Time: 13:47
 */

namespace Frogg\Model;

use Phalcon\Mvc\Model as PhalconModel;

/**
 * Class Criteria
 *
 * default call criteria on model, example:
 *
 *     public static function query(DiInterface $dependencyInjector = null)
 *     {
 *         return parent::query($dependencyInjector)->softDelete();
 *     }
 *
 * @package Frogg
 * @method static addSoftDelete(string $column = 'deleted', int $activeValue = 0) add soft delete criteria to the query
 * @method static removeSoftDelete() removes soft delete criteria from the criteriaQueue
 */
class Criteria extends PhalconModel\Criteria
{
    private   $modelCriterias = [];
    protected $alias;

    /**
     * removes soft deleted entries from the result.
     *
     * @param string $column
     * @param int    $activeValue
     *
     * @return PhalconModel\Criteria
     * @internal param $add
     *
     */
    public function softDelete($column = 'deleted', $activeValue = 0)
    {
        return $this->andWhere($column.'='.$activeValue);
    }

    /**
     * alias to make more sense when calling it on query building.
     *
     * @return $this
     */
    public function withDeleted()
    {
        return $this->removeSoftDelete();
    }

    public function createBuilder()
    {
        if (method_exists(parent::class, 'createBuilder')) {
            return parent::createBuilder();
        } else {
            $params = $this->getParams();
            /** @var PhalconModel\Manager $modelsManager */
            $modelsManager = $this->getDI()->get('modelsManager');

            return $modelsManager->createBuilder($params)->from($this->getModelName());
        }
    }

    public function execute()
    {
        foreach ($this->modelCriterias as $criteria => $value) {
            $method = lcfirst($criteria);
            $this->$method(...$value);
        }

        $builder = $this->createBuilder();
        $builder->from([$this->getAlias() => $this->getModelName()]);

        return $builder->getQuery()->execute();
    }

    public function getSql(): string
    {
        foreach ($this->modelCriterias as $criteria => $value) {
            $method = lcfirst($criteria);
            $this->$method(...$value);
        }

        $builder = $this->createBuilder();
        $builder->from([$this->getAlias() => $this->getModelName()]);

        return $builder->getQuery()->getSql();
    }

    /**
     * @return static
     */
    public function clone()
    {
        return clone $this;
    }

    public function getPhql()
    {
        return $this->createBuilder()->getPhql();
    }

    public function getQuery()
    {
        return $this->createBuilder()->getQuery();
    }

    public function getActiveCriterias()
    {
        return $this->modelCriterias;
    }

    public function findFirstById($id)
    {
        return $this->andWhere($this->getAlias().'.id = '.$id)->execute()->getFirst();
    }

    public function count($column = '*')
    {
        return $this->columns('count('.$column.') as total')->execute()->getFirst()->total;
    }

    private function parentExecute()
    {
        return parent::execute();
    }

    /**
     * Defaults merge to true on bind params
     *
     * @param array $bindParams
     * @param bool  $merge
     *
     * @return PhalconModel\Criteria|void
     */
    public function bind(array $bindParams, $merge = true)
    {
        parent::bind($bindParams, $merge);
    }

    /**
     * Add merge feature to bindTypes (lazy phalcon developers should made that) and defaults it to true
     *
     * @param array $bindTypes
     * @param bool  $merge
     *
     * @return PhalconModel\Criteria|void<commit_msg>added findFirst and findFirstBy methods<commit_after><?php
/**
 * Created by PhpStorm.
 * User: Alexandre
 * Date: 29/09/2017
 * Time: 13:47
 */

namespace Frogg\Model;

use Phalcon\Mvc\Model as PhalconModel;

/**
 * Class Criteria
 *
 * default call criteria on model, example:
 *
 *     public static function query(DiInterface $dependencyInjector = null)
 *     {
 *         return parent::query($dependencyInjector)->softDelete();
 *     }
 *
 * @package Frogg
 * @method static addSoftDelete(string $column = 'deleted', int $activeValue = 0) add soft delete criteria to the query
 * @method static removeSoftDelete() removes soft delete criteria from the criteriaQueue
 */
class Criteria extends PhalconModel\Criteria
{
    private   $modelCriterias = [];
    protected $alias;

    /**
     * removes soft deleted entries from the result.
     *
     * @param string $column
     * @param int    $activeValue
     *
     * @return PhalconModel\Criteria
     * @internal param $add
     *
     */
    public function softDelete($column = 'deleted', $activeValue = 0)
    {
        return $this->andWhere($column.'='.$activeValue);
    }

    /**
     * alias to make more sense when calling it on query building.
     *
     * @return $this
     */
    public function withDeleted()
    {
        return $this->removeSoftDelete();
    }

    public function createBuilder()
    {
        if (method_exists(parent::class, 'createBuilder')) {
            return parent::createBuilder();
        } else {
            $params = $this->getParams();
            /** @var PhalconModel\Manager $modelsManager */
            $modelsManager = $this->getDI()->get('modelsManager');

            return $modelsManager->createBuilder($params)->from($this->getModelName());
        }
    }

    public function execute()
    {
        foreach ($this->modelCriterias as $criteria => $value) {
            $method = lcfirst($criteria);
            $this->$method(...$value);
        }

        $builder = $this->createBuilder();
        $builder->from([$this->getAlias() => $this->getModelName()]);

        return $builder->getQuery()->execute();
    }

    public function getSql(): string
    {
        foreach ($this->modelCriterias as $criteria => $value) {
            $method = lcfirst($criteria);
            $this->$method(...$value);
        }

        $builder = $this->createBuilder();
        $builder->from([$this->getAlias() => $this->getModelName()]);

        return $builder->getQuery()->getSql();
    }

    /**
     * @return static
     */
    public function clone()
    {
        return clone $this;
    }

    public function getPhql()
    {
        return $this->createBuilder()->getPhql();
    }

    public function getQuery()
    {
        return $this->createBuilder()->getQuery();
    }

    public function getActiveCriterias()
    {
        return $this->modelCriterias;
    }

    public function findFirst($conditions = false, $bindParams = null, $bindTypes = null)
    {
        if($conditions){
            $this->andWhere($conditions, $bindParams, $bindTypes);
        }

        return $this->execute()->getFirst();
    }

    public function findFirstBy($column, $value)
    {
        return $this->findFirst($this->getAlias().'.'.$column.' = :value:', ['value' => $value]);
    }

    public function findFirstById($id)
    {
        return $this->findFirstBy('id', $id);
    }

    public function count($column = '*')
    {
        return $this->columns('count('.$column.') as total')->execute()->getFirst()->total;
    }

    /**
     * @deprecated
     */
    private function parentExecute()
    {
        return parent::execute();
    }

    /**
     * Defaults merge to true on bind params
     *
     * @param array $bindParams
     * @param bool  $merge
     *
     * @return PhalconModel\Criteria|void
     */
    public function bind(array $bindParams, $merge = true)
    {
        parent::bind($bindParams, $merge);
    }

    /**
     * Add merge feature to bindTypes (lazy phalcon developers should made that) and defaults it to true
     *
     * @param array $bindTypes
     * @param bool  $merge
     *
     * @return PhalconModel\Criteria|void
{'fraction_non_alphanumeric': 0.14303685998238772, 'fraction_numerical': 0.0035224556547993457, 'mean_word_length': 2.3361309274024338, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 90, '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': '3477777', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2245, 'n_words': 531}