<commit_before><?php

namespace Sokil\Mongo\Yii;

class DataProvider extends \CDataProvider
{
    public $attributes;
    
    public $filter;

    /**
     *
     * @var \Sokil\Mongo\QueryBuilder
     */
    private $_queryBuilder;

    public function __construct($dataSource, $config = array())
    {
        // name of collection
        if (is_string($dataSource)) {
            $collection = \Yii::app()->mongo->getCollection($dataSource);
            $this->_queryBuilder = $collection->find();
        }
        // query builder
        else if ($dataSource instanceof \Sokil\Mongo\QueryBuilder) {
            $this->_queryBuilder = $dataSource;
        }

        foreach ($config as $key => $value) {
            $this->$key = $value;
        }

        if (isset($this->filter)) {
            foreach ($this->filter as $a => $v) {
                if (!empty($v)) {
                    $this->_queryBuilder->where($a, array('$regex' => $v, '$options' => 'i'));
                }
            }
        }

        if (!isset($this->attributes) || !is_array($this->attributes)) {
            throw new \Exception('Attributes are required.');
        }
    }

    public function fetchData()
    {
        if (($pagination = $this->getPagination()) !== false) {
            $pagination->setItemCount($this->getTotalItemCount());
            $this->_queryBuilder
                ->skip($pagination->getOffset())
                ->limit($pagination->getLimit());
        }

        $sort = $this->getSort();
        if ($sort) {
            foreach ($sort->getDirections() as $a => $d) {
                if ($d == 0) {
                    $d = -1; // mongo desc
                }
                $this->_queryBuilder->sort(array($a => $d));
            }
        }

        return array_values($this->_queryBuilder->findAll());
    }

    public function calculateTotalItemCount()
    {
        return $this->_queryBuilder->count();
    }

    public function fetchKeys()
    {
        return array_keys($this->getData());
    }

    public function getSort($className = 'CSort')
    {
        if (($sort = parent::getSort($className)) !== false) {
            $sort->attributes = $this->attributes;
        }

        return $sort;
    }

}
<commit_msg>Update DataProvider.php<commit_after><?php

namespace Sokil\Mongo\Yii;

class DataProvider extends \CDataProvider
{
    public $attributes;
    
    public $filter;

    /**
     *
     * @var \Sokil\Mongo\QueryBuilder
     */
    private $_queryBuilder;

    public function __construct($dataSource, $config = array())
    {
        // name of collection
        if (is_string($dataSource)) {
            $collection = \Yii::app()->mongo->getCollection($dataSource);
            $this->_queryBuilder = $collection->find();
        }
        // query builder
        else if ($dataSource instanceof \Sokil\Mongo\Cursor) {
            $this->_queryBuilder = $dataSource;
        }

        foreach ($config as $key => $value) {
            $this->$key = $value;
        }

        if (isset($this->filter)) {
            foreach ($this->filter as $a => $v) {
                if (!empty($v)) {
                    $this->_queryBuilder->where($a, array('$regex' => $v, '$options' => 'i'));
                }
            }
        }

        if (!isset($this->attributes) || !is_array($this->attributes)) {
            throw new \Exception('Attributes are required.');
        }
    }

    public function fetchData()
    {
        if (($pagination = $this->getPagination()) !== false) {
            $pagination->setItemCount($this->getTotalItemCount());
            $this->_queryBuilder
                ->skip($pagination->getOffset())
                ->limit($pagination->getLimit());
        }

        $sort = $this->getSort();
        if ($sort) {
            foreach ($sort->getDirections() as $a => $d) {
                if ($d == 0) {
                    $d = -1; // mongo desc
                }
                $this->_queryBuilder->sort(array($a => $d));
            }
        }

        return array_values($this->_queryBuilder->findAll());
    }

    public function calculateTotalItemCount()
    {
        return $this->_queryBuilder->count();
    }

    public function fetchKeys()
    {
        return array_keys($this->getData());
    }

    public function getSort($className = 'CSort')
    {
        if (($sort = parent::getSort($className)) !== false) {
            $sort->attributes = $this->attributes;
        }

        return $sort;
    }

}

{'fraction_non_alphanumeric': 0.1581858407079646, 'fraction_numerical': 0.0008849557522123894, 'mean_word_length': 1.728424864212432, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, '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': '5612227', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1249, 'n_words': 241}