<commit_before><?php
/**
 * Wasabi CMS
 * Copyright (c) <NAME> (http://frankfoerster.com)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) <NAME> (http://frankfoerster.com)
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace Wasabi\Core\Model\Behavior;
use Cake\Collection\Collection;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\ORM\ResultSet;
use Cake\Utility\Hash;

/**
 * Class KeyValueBehavior
 */
class KeyValueBehavior extends Behavior
{
    /**
     * Default configuration
     *
     * These are merged with user-provided configuration when the behavior is used.
     *
     * @var array
     */
    protected $_defaultConfig = [
        'scope' => 'App',
        'fields' => [
            'key' => 'field',
            'value' => 'value'
        ],
        'serializeFields' => [],
        'implementedFinder' => [
            'keyValues' => 'findKeyValues',
            'allKeyValues' => 'findAllKeyValues'
        ]
    ];

    /**
     * Generate a custom entity with all keys as properties and
     * their corresponding values
     *
     * Returns a computed entity that can be used by FormHelper.
     *<commit_msg>implement getAllKeyValues() to get all key/value pairs for all scopes<commit_after><?php
/**
 * Wasabi CMS
 * Copyright (c) <NAME> (http://frankfoerster.com)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) <NAME> (http://frankfoerster.com)
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace Wasabi\Core\Model\Behavior;
use Cake\Collection\Collection;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\ORM\ResultSet;
use Cake\Utility\Hash;

/**
 * Class KeyValueBehavior
 */
class KeyValueBehavior extends Behavior
{
    /**
     * Default configuration
     *
     * These are merged with user-provided configuration when the behavior is used.
     *
     * @var array
     */
    protected $_defaultConfig = [
        'scope' => 'App',
        'fields' => [
            'key' => 'field',
            'value' => 'value'
        ],
        'serializeFields' => [],
        'implementedFinder' => [
            'keyValues' => 'findKeyValues',
            'allKeyValues' => 'findAllKeyValues'
        ]
    ];

    /**
     * Returns all keys and their values for all scopes.
     *
     * @return array
     */
    public function getAllKeyValues()
    {
        $query = $this->_table->query();

        $query->formatResults(function(ResultSet $results) {
                return $results->map(function ($row) {
                    if ($row['serialized'] === true) {
                        $row['value'] = unserialize($row['value']);
                    }
                    return $row;
                });
            });

        $settings = [];

        foreach ($query->all() as $row) {
            $key = $row['scope'] . '__' . $row[$this->config('fields.key')];
            $value = $row[$this->config('fields.value')];
            $settings[$key] = $value;
        }

        return Hash::expand($settings, '__');
    }

    /**
     * Generate a custom entity with all keys as properties and
     * their corresponding values
     *
     * Returns a computed entity that can be used by FormHelper.
     *
{'fraction_non_alphanumeric': 0.1297013274336283, 'fraction_numerical': 0.0, 'mean_word_length': 2.2208370436331255, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6187990', 'n_tokens_mistral': 1025, 'n_tokens_neox': 1000, 'n_words': 277}