<commit_before><?php
/**
 * @link https://github.com/bitrix-expert/tools
 * @copyright Copyright © 2015 <NAME>
 * @license MIT
 */

namespace Bex\Tools\Group;

use Bex\Tools\Finder;
use Bitrix\Main;
use Bitrix\Main\Application;

/**
 * @author <NAME> <<EMAIL>>
 */
class GroupFinder extends Finder
{
    protected static $cacheTime = 8600000;
    protected static $cacheDir = 'bex_tools/groups';
    protected static $cacheTag = 'bex_user_groups';
    protected $id;
    protected $code;

    public function __construct(array $filter)
    {
        $filter = $this->prepareFilter($filter);

        if (isset($filter['id']))
        {
            $this->id = $filter['id'];
        }

        if (isset($filter['code']))
        {
            $this->code = $filter['code'];
        }

        if (!isset($this->id))
        {
            if (!isset($this->code))
            {
                throw new Main\ArgumentNullException('code');
            }

            $this->id = $this->getFromCache([
                'type' => 'groupId'
            ]);
        }
    }

    public function id()
    {
        return $this->getFromCache([
            'object' => 'groupId'
        ]);
    }

    public function code()
    {
        return $this->getFromCache([
            'object' => 'groupCode'
        ]);
    }

    protected function prepareFilter(array $filter)
    {
        foreach ($filter as $code => &$value)
        {
            if ($code === 'id')
            {
                intval($value);

                if ($value <= 0)
                {
                    throw new Main\ArgumentNullException($code);
                }
            }
            else
            {
                trim(htmlspecialchars($value));

                if (strlen($value) <= 0)
                {
                    throw new Main\ArgumentNullException($code);
                }
            }
        }

        return $filter;
    }

    protected function getValue(array $cache, array $filter)
    {
        switch ($filter['object'])
        {
            case 'groupId':
                $value = (int) $cache['GROUPS'][$filter['groupCode']];

                if ($value <= 0)
                {
                    throw new \Exception();
                }
                break;

            case 'groupCode':
                $value = $cache['CODES'][$this->id];

                if (strlen($value) <= 0)
                {
                    throw new \Exception();
                }
                break;

            default:
                throw new Main\ArgumentException('', 'type');
                break;
        }

        return $value;
    }

    protected function getItems()
    {
        $items = [];

        $rsGroups = Main\GroupTable::getList();

        while ($group = $rsGroups->Fetch())
        {
            if ($group['STRING_ID'])
            {
                $items['GROUPS'][$group['STRING_ID']] = $group['ID'];
                $items['CODES'][$group['ID']] = $group['STRING_ID'];
            }
        }

        if (!empty($items))
        {
            Application::getInstance()->getTaggedCache()->registerTag($this->getCacheTag());<commit_msg>Groups finder<commit_after><?php
/**
 * @link https://github.com/bitrix-expert/tools
 * @copyright Copyright © 2015 <NAME>
 * @license MIT
 */

namespace Bex\Tools\Group;

use Bex\Tools\Finder;
use Bitrix\Main\Application;
use Bitrix\Main\ArgumentException;
use Bitrix\Main\ArgumentNullException;
use Bitrix\Main\GroupTable;

/**
 * @author <NAME> <<EMAIL>>
 */
class GroupFinder extends Finder
{
    protected static $cacheTime = 8600000;
    protected static $cacheDir = 'bex_tools/groups';
    protected static $cacheTag = 'bex_user_groups';
    protected $id;
    protected $code;

    public function __construct(array $filter)
    {
        $filter = $this->prepareFilter($filter);

        if (isset($filter['id']))
        {
            $this->id = $filter['id'];
        }

        if (isset($filter['code']))
        {
            $this->code = $filter['code'];
        }

        if (!isset($this->id))
        {
            if (!isset($this->code))
            {
                throw new ArgumentNullException('code');
            }

            $this->id = $this->getFromCache([
                'type' => 'id'
            ]);
        }
    }

    public function id()
    {
        return $this->getFromCache([
            'type' => 'id'
        ]);
    }

    public function code()
    {
        return $this->getFromCache([
            'type' => 'code'
        ]);
    }

    protected function prepareFilter(array $filter)
    {
        foreach ($filter as $code => &$value)
        {
            if ($code === 'id')
            {
                intval($value);

                if ($value <= 0)
                {
                    throw new ArgumentNullException($code);
                }
            }
            else
            {
                trim(htmlspecialchars($value));

                if (strlen($value) <= 0)
                {
                    throw new ArgumentNullException($code);
                }
            }
        }

        return $filter;
    }

    protected function getValue(array $cache, array $filter)
    {
        switch ($filter['type'])
        {
            case 'id':
                $value = (int) $cache['GROUPS'][$this->code];

                if ($value <= 0)
                {
                    throw new ArgumentException('Group ID by group code "' . $this->code . '" not found');
                }
                break;

            case 'code':
                $value = $cache['CODES'][$this->id];

                if (strlen($value) <= 0)
                {
                    throw new ArgumentException('Group code by ID "' . $this->id . '" not found');
                }
                break;

            default:
                throw new \InvalidArgumentException('Invalid type of filter');
                break;
        }

        return $value;
    }

    protected function getItems()
    {
        $items = [];

        $rsGroups = GroupTable::getList();

        while ($group = $rsGroups->Fetch())
        {
            if ($group['STRING_ID'])
            {
                $items['GROUPS'][$group['STRING_ID']] = $group['ID'];
                $items['CODES'][$group['ID']] = $group['STRING_ID'];
            }
        }

        if (!empty($items))
        {
            Application::getInstance()->getTaggedCache()->registerTag($this->getCacheTag());
{'fraction_non_alphanumeric': 0.14377682403433475, 'fraction_numerical': 0.004598405885959534, 'mean_word_length': 1.3718647764449292, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 50, 'https://': 2, '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': '7351617', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1713, 'n_words': 351}