<commit_before><?php
/**
 * Created by PhpStorm.
 * User: dimaninc
 * Date: 19.01.2018
 * Time: 18:03
 */

namespace diCore\Database\Entity\Mongo;

use diCore\Database\Legacy\Mongo;
use diCore\Helper\ArrayHelper;
use MongoDB\Driver\Cursor;

/**
 * Class Collection
 * @package diCore\Database\Entity\Mongo
 *
 * @method Mongo getDb
 */
class Collection extends \diCollection
{
	/** @var  Cursor */
	protected $cursor;

	protected static $operators = [
		'=' => '$eq',
		'!=' => '$ne',
		'in' => '$in',
		'not in' => '$nin',
		'>' => '$gt',
		'>=' => '$gte',
		'<' => '$lt',
		'<=' => '$lte',
	];

	public function addAliasToTable($table, $alias = null)
	{
		return $table;
	}

	public function addAliasToField($field, $alias = null)
	{
		if ($field == 'id')
		{
			/** @var Model $m */
			$m = $this->getNewEmptyItem();

			return $m->getIdFieldName();
		}

		return $field;
	}

	public function getFullQuery()
	{
		$ar = [
			'filter' => $this->getQueryWhere(),
			//todo: 'group' => $this->getQueryGroupBy(),
			'sort' => $this->getQueryOrderBy(),
			'skip' => $this->getStartFrom(),
			'limit' => $this->getPageSize(),
		];

		return $ar;
	}

	protected function getQueryWhere()
	{
		$filter = [];

		/** @var Model $modelClass */
		$modelClass = static::getModelClass();

		if ($this->sqlParts['where'])
		{
			foreach ($this->sqlParts['where'] as $val)
			{
				$val['value'] = $modelClass::tuneFieldValueByTypeBeforeDb($val['field'], $val['value']);

				$existingFilter = isset($filter[$val['field']])
					? $filter[$val['field']]
					: [];

				$val['operator'] = mb_strtolower($val['operator']);

				if (isset(self::$operators[$val['operator']]))
				{
					$operator = self::$operators[$val['operator']];

					$newFilter = [
						$operator => $val['value'],
					];
				}
				elseif ($val['operator'] == 'between')
				{
					if (is_array($val['value']) && count($val['value']) == 2)
					{
						$newFilter = [
							'$gte' => ArrayHelper::getValue($val['value'], 0),
							'$lte' => ArrayHelper::getValue($val['value'], 1),
						];
					}<commit_msg>Mongo/Collection.php filter by null fix<commit_after><?php
/**
 * Created by PhpStorm.
 * User: dimaninc
 * Date: 19.01.2018
 * Time: 18:03
 */

namespace diCore\Database\Entity\Mongo;

use diCore\Database\Legacy\Mongo;
use diCore\Helper\ArrayHelper;
use MongoDB\Driver\Cursor;

/**
 * Class Collection
 * @package diCore\Database\Entity\Mongo
 *
 * @method Mongo getDb
 */
class Collection extends \diCollection
{
	/** @var  Cursor */
	protected $cursor;

	protected static $operators = [
		'=' => '$eq',
		'!=' => '$ne',
		'in' => '$in',
		'not in' => '$nin',
		'>' => '$gt',
		'>=' => '$gte',
		'<' => '$lt',
		'<=' => '$lte',
	];

	public function addAliasToTable($table, $alias = null)
	{
		return $table;
	}

	public function addAliasToField($field, $alias = null)
	{
		if ($field == 'id')
		{
			/** @var Model $m */
			$m = $this->getNewEmptyItem();

			return $m->getIdFieldName();
		}

		return $field;
	}

	public function getFullQuery()
	{
		$ar = [
			'filter' => $this->getQueryWhere(),
			//todo: 'group' => $this->getQueryGroupBy(),
			'sort' => $this->getQueryOrderBy(),
			'skip' => $this->getStartFrom(),
			'limit' => $this->getPageSize(),
		];

		return $ar;
	}

	protected function getQueryWhere()
	{
		$filter = [];

		/** @var Model $modelClass */
		$modelClass = static::getModelClass();

		if ($this->sqlParts['where'])
		{
			foreach ($this->sqlParts['where'] as $val)
			{
				if ($val['value'] !== null)
				{
					$val['value'] = $modelClass::tuneFieldValueByTypeBeforeDb($val['field'], $val['value']);
				}

				$existingFilter = isset($filter[$val['field']])
					? $filter[$val['field']]
					: [];

				$val['operator'] = mb_strtolower($val['operator']);

				if (isset(self::$operators[$val['operator']]))
				{
					$operator = self::$operators[$val['operator']];

					$newFilter = [
						$operator => $val['value'],
					];
				}
				elseif ($val['operator'] == 'between')
				{
					if (is_array($val['value']) && count($val['value']) == 2)
					{
						$newFilter = [
							'$gte' => ArrayHelper::getValue($val['value'], 0),
							'$lte' => ArrayHelper::getValue($val['value'], 1),
						];
					}
{'fraction_non_alphanumeric': 0.22654951317976726, 'fraction_numerical': 0.007124198527665638, 'mean_word_length': 3.324435318275154, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 57, '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': '5995076', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1493, 'n_words': 271}