<commit_before><?php
namespace Enum\Model\Behavior\Strategy;

use Cake\Core\InstanceConfigTrait;
use Cake\Datasource\ModelAwareTrait;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;

class LookupStrategy extends AbstractStrategy
{

    use ModelAwareTrait;

    /**
     * @inheritdoc
     */
    public function __construct($alias, Table $table)
    {
        parent::__construct($alias, $table);
        $this->modelClass = 'Enum.Lookups';
        $this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']);
    }

    /**
     * @inheritdoc
     */
    public function hasPrefix($prefix)
    {
        return in_array(strtoupper($prefix), $this->listPrefixes());
    }

    /**
     * @inheritdoc
     */
    public function listPrefixes()
    {
        if (empty($this->_prefixes)) {
            $this->_prefixes = array_keys($this->loadModel()->find('list', [
                'keyField' => 'prefix',
            ])->toArray());
        }

        return $this->_prefixes;
    }

    /**
     * @inheritdoc
     */
    public function enum(array $config)
    {
        $query = $this->loadModel()
            ->find('list', [
                'keyField' => 'group',
                'valueField' => 'label',
            ])
            ->where([
                'prefix' => $config['prefix'],
            ]);

        foreach ($config as $method => $args) {
            if (method_exists($query, $method)) {
                $query = call_user_func_array([$query, $method], $args);
            }
        }

        return $query->toArray();
    }

    /**
     * @inheritdoc
     */
    public function get($key)
    {
        return $this->loadModel()->find()
            ->where(['name' => $key, 'prefix' => $this->config('prefix')]);
    }
}
<commit_msg>Auto configure enum associations<commit_after><?php
namespace Enum\Model\Behavior\Strategy;

use Cake\Core\InstanceConfigTrait;
use Cake\Datasource\ModelAwareTrait;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;

class LookupStrategy extends AbstractStrategy
{

    use ModelAwareTrait;

    /**
     * @inheritdoc
     */
    public function __construct($alias, Table $table)
    {
        parent::__construct($alias, $table);
        $this->modelClass = 'Enum.Lookups';
        $this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']);
    }

    /**
     * @inheritdoc
     */
    public function hasPrefix($prefix)
    {
        return in_array(strtoupper($prefix), $this->listPrefixes());
    }

    /**
     * @inheritdoc
     */
    public function listPrefixes()
    {
        if (empty($this->_prefixes)) {
            $this->_prefixes = array_keys($this->loadModel()->find('list', [
                'keyField' => 'prefix',
            ])->toArray());
        }

        return $this->_prefixes;
    }

    /**
     * @inheritdoc
     */
    public function enum(array $config)
    {
        $query = $this->loadModel()
            ->find('list', [
                'keyField' => 'group',
                'valueField' => 'label',
            ])
            ->where([
                'prefix' => $config['prefix'],
            ]);

        foreach ($config as $method => $args) {
            if (method_exists($query, $method)) {
                $query = call_user_func_array([$query, $method], $args);
            }
        }

        return $query->toArray();
    }

    /**
     * @inheritdoc
     */
    public function get($key)
    {
        return $this->loadModel()->find()
            ->where(['name' => $key, 'prefix' => $this->config('prefix')]);
    }

    /**
     * @inheritdoc
     */
    public function initialize($config)
    {
        $config = parent::initialize($config);
        $assocName = Inflector::pluralize(Inflector::classify($this->_alias));

        $this->_table->belongsTo($assocName, [
            'className' => $this->modelClass,
            'foreignKey' => $config['field'],
            'bindingKey' => 'name',
            'conditions' => [$assocName . '.prefix' => strtoupper($this->_alias)],
        ]);

        return $config;
    }
}

{'fraction_non_alphanumeric': 0.16597712338768555, 'fraction_numerical': 0.0, 'mean_word_length': 1.9610951008645534, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, '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': '2004134', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1201, 'n_words': 206}