<commit_before><?php
namespace DreamFactory\Core\SqlDb\Components;

use DreamFactory\Core\Utility\ResourcesWrapper;
use DreamFactory\Library\Utility\ArrayUtils;

trait TableDescriber
{
    /**
     * @param array $table
     * @param array $extras
     *
     * @throws \Exception
     * @return array
     */
    protected static function mergeTableExtras($table, $extras = null)
    {
        $table = ArrayUtils::clean($table);
        $extras = ArrayUtils::clean($extras);

        $labelInfo = ArrayUtils::get($extras, '', []);
        $label = ArrayUtils::get($labelInfo, 'label');
        if (!empty($label)) {
            $table['label'] = $label;
        }

        $plural = ArrayUtils::get($labelInfo, 'plural');
        if (!empty($plural)) {
            $table['plural'] = $plural;
        }

        $name_field = ArrayUtils::get($labelInfo, 'name_field');
        if (!empty($name_field)) {
            $table['name_field'] = $name_field;
        }

        $fields = ArrayUtils::get($table, 'field', []);
        foreach ($fields as &$field) {
            $name = ArrayUtils::get($field, 'name');
            $info = ArrayUtils::get($extras, $name, []);
            $field = static::mergeFieldExtras($field, $info);
        }
        $table['field'] = $fields;

        return $table;
    }

    /**
     * @param array $column
     * @param array $extras
     *
     * @throws \Exception
     * @return array
     */
    protected static function mergeFieldExtras($column, $extras = null)
    {
        $out = ArrayUtils::clean($column);
        $extras = ArrayUtils::clean($extras);

        $label = ArrayUtils::get($extras, 'label');
        if (!empty($label)) {
            $out['label'] = $label;
        }

        $validation = json_decode(ArrayUtils::get($extras, 'validation'), true);
        if (!empty($validation) && is_string($validation)) {
            // backwards compatible with old strings
            $validation = array_map('trim', explode(',', $validation));
            $validation = array_flip($validation);
        }
        $out['validation'] = $validation;
        if (is_array($validation) && isset($validation['api_read_only'])) {
            $out['required'] = false;
        }

        $picklist = ArrayUtils::get($extras, 'picklist');
        $picklist = (!empty($picklist)) ? explode("\r", $picklist) : [];
        $out['value'] = $picklist;

        switch (ArrayUtils::get($column, 'type')) {
            case 'integer':
                if (isset($extras['user_id_on_update'])) {
                    $out['type'] =
                        'user_id_on_' . (ArrayUtils::getBool($extras, 'user_id_on_update') ? 'update' : 'create');
                }

                if (null !== ArrayUtils::get($extras, 'user_id')) {
                    $out['type'] = 'user_id';
                }
                break;

            case 'timestamp':
                if (isset($extras['timestamp_on_update'])) {
                    $out['type'] =
                        'timestamp_on_' . (ArrayUtils::getBool($extras, 'timestamp_on_update') ? 'update' : 'create');
                }
                break;
        }

        return $out;
    }

    public static function getApiDocCommonModels()
    {
        $wrapper = ResourcesWrapper::getWrapper();

        return [
            'TableSchemas'  => [
                'id'         => 'TableSchemas',
                'properties' => [
                    $wrapper => [
                        'type'        => 'Array',
                        'description' => 'An array of table definitions.',
                        'items'       => [
                            '$ref' => 'TableSchema',
                        ],
                    ],
                ],
            ],<commit_msg>cleanup unused functions<commit_after><?php
namespace DreamFactory\Core\SqlDb\Components;

use DreamFactory\Core\Utility\ResourcesWrapper;
use DreamFactory\Library\Utility\ArrayUtils;

trait TableDescriber
{
    public static function getApiDocCommonModels()
    {
        $wrapper = ResourcesWrapper::getWrapper();

        return [
            'TableSchemas'  => [
                'id'         => 'TableSchemas',
                'properties' => [
                    $wrapper => [
                        'type'        => 'Array',
                        'description' => 'An array of table definitions.',
                        'items'       => [
                            '$ref' => 'TableSchema',
                        ],
                    ],
                ],
            ],
{'fraction_non_alphanumeric': 0.1493421052631579, 'fraction_numerical': 0.0, 'mean_word_length': 1.6502033701336432, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '3931930', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1206, 'n_words': 223}