<commit_before><?php

namespace DreamFactory\Core\Scripting\Models;

use DreamFactory\Core\Contracts\ScriptEngineTypeInterface;
use DreamFactory\Core\Exceptions\ServiceUnavailableException;
use DreamFactory\Core\Models\BaseServiceConfigModel;
use ScriptEngineManager;

/**
 * ScriptConfig
 *
 * @property integer $service_id
 * @property string  $content
 * @property string  $config
 * @method static \Illuminate\Database\Query\Builder|ScriptConfig whereServiceId($value)
 * @method static \Illuminate\Database\Query\Builder|ScriptConfig whereType($value)
 */
class ScriptConfig extends BaseServiceConfigModel
{
    protected $table = 'script_config';

    protected $fillable = ['service_id', 'content', 'config'];

    protected $appends = ['engine'];

    protected $casts = ['service_id' => 'integer', 'config' => 'array'];

    /**
     * @var array Extra config to pass to any config handler
     */
    protected $engine = [];

    public static function getType()
    {
        return '';
    }
    
    /**
     * {@inheritdoc}
     */
    public static function validateConfig($config, $create = true)
    {
        if (!empty($disable = config('df.scripting.disable')))
        {
            switch (strtolower($disable)){
                case 'all':
                    throw new ServiceUnavailableException("All scripting is disabled for this instance.");
                    break;
                default:
                    $type = static::getType();
                    if (!empty($type) && (false !== stripos($disable, $type))){
                        throw new ServiceUnavailableException("Scripting with $type is disabled for this instance.");
                    }
                    break;
            }
        }

        return true;
    }
    
    /**
     * @return mixed
     */
    public function getEngineAttribute()
    {
        /** @type ScriptEngineTypeInterface $typeInfo */
        if (null !== $typeInfo = ScriptEngineManager::getScriptEngineType($this->getType())) {
            $this->engine = $typeInfo->toArray();
        }

        return $this->engine;
    }

    /**
     * @param array $schema
     */
    protected static function prepareConfigSchemaField(array &$schema)
    {
        parent::prepareConfigSchemaField($schema);

        switch ($schema['name']) {
            case 'content':
                $schema['label'] = 'Content';
                $schema['type'] = 'text';
                $schema['description'] =
                    'The content of the script written in the appropriate language.';
                break;
            case 'config':
                $schema['label'] = 'Additional Configuration';
                $schema['type'] = 'object';
                $schema['object'] =
                    [
                        'key'   => ['label' => 'Name', 'type' => 'string'],
                        'value' => ['label' => 'Value', 'type' => 'string']
                    ];<commit_msg>cleanup from deprecated db fields<commit_after><?php

namespace DreamFactory\Core\Scripting\Models;

use DreamFactory\Core\Contracts\ScriptEngineTypeInterface;
use DreamFactory\Core\Exceptions\ServiceUnavailableException;
use DreamFactory\Core\Models\BaseServiceConfigModel;
use ScriptEngineManager;

/**
 * ScriptConfig
 *
 * @property integer $service_id
 * @property string  $content
 * @property string  $config
 * @method static \Illuminate\Database\Query\Builder|ScriptConfig whereServiceId($value)
 * @method static \Illuminate\Database\Query\Builder|ScriptConfig whereType($value)
 */
class ScriptConfig extends BaseServiceConfigModel
{
    protected $table = 'script_config';

    protected $fillable = ['service_id', 'content', 'config'];

    // deprecated, service has type designation now
    protected $hidden = ['type'];
    
    protected $casts = ['service_id' => 'integer', 'config' => 'array'];

    /**
     * @var array Extra config to pass to any config handler
     */
    protected $engine = [];

    public static function getType()
    {
        return '';
    }
    
    /**
     * {@inheritdoc}
     */
    public static function validateConfig($config, $create = true)
    {
        if (!empty($disable = config('df.scripting.disable')))
        {
            switch (strtolower($disable)){
                case 'all':
                    throw new ServiceUnavailableException("All scripting is disabled for this instance.");
                    break;
                default:
                    $type = static::getType();
                    if (!empty($type) && (false !== stripos($disable, $type))){
                        throw new ServiceUnavailableException("Scripting with $type is disabled for this instance.");
                    }
                    break;
            }
        }

        return true;
    }
    
    /**
     * @param array $schema
     */
    protected static function prepareConfigSchemaField(array &$schema)
    {
        parent::prepareConfigSchemaField($schema);

        switch ($schema['name']) {
            case 'content':
                $schema['label'] = 'Content';
                $schema['type'] = 'text';
                $schema['description'] =
                    'The content of the script written in the appropriate language.';
                break;
            case 'config':
                $schema['label'] = 'Additional Configuration';
                $schema['type'] = 'object';
                $schema['object'] =
                    [
                        'key'   => ['label' => 'Name', 'type' => 'string'],
                        'value' => ['label' => 'Value', 'type' => 'string']
                    ];
{'fraction_non_alphanumeric': 0.12780625773378115, 'fraction_numerical': 0.0, 'mean_word_length': 2.012779552715655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '4653603', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1372, 'n_words': 344}