/**
  * By default return the value.
  * This can be dangerous if used in a loop!
  * There is a PHP bug that caches it's
  * output when used in a loop.
  * Take heed.
  *
  * @return string
  */
 public function __toString()
 {
     $value = $this->object->getValue();
     if (is_array($value) || is_object($value)) {
         return json_encode($value);
     }
     return (string) $this->object->getValue();
 }
 /**
  * Drop a column.
  *
  * @param           $table
  * @param FieldType $type
  */
 public function dropColumn($table, FieldType $type)
 {
     $schema = $type->getSchema();
     if (!$this->schema->hasTable($table)) {
         return;
     }
     $this->schema->table($table, function (Blueprint $table) use($schema) {
         $schema->dropColumn($table);
     });
 }
 /**
  * Register field's custom validators.
  *
  * @param Factory     $factory
  * @param FormBuilder $builder
  * @param FieldType   $fieldType
  */
 protected function registerValidators(Factory $factory, FormBuilder $builder, FieldType $fieldType)
 {
     foreach ($fieldType->getValidators() as $rule => $validator) {
         $handler = array_get($validator, 'handler');
         if (is_string($handler) && !str_contains($handler, '@')) {
             $handler .= '@handle';
         }
         $factory->extend($rule, function ($attribute, $value, $parameters, Validator $validator) use($handler, $builder) {
             return $this->container->call($handler, compact('attribute', 'value', 'parameters', 'builder', 'validator'));
         }, array_get($validator, 'message'));
     }
 }
 /**
  * Get the config.
  *
  * @return array
  */
 public function getConfig()
 {
     $config = parent::getConfig();
     /**
      * If images only manually set
      * the allowed mimes.
      */
     if (array_get($config, 'image')) {
         array_set($config, 'mimes', ['jpg', 'jpeg', 'png', 'bmp', 'gif', 'svg']);
     }
     /**
      * If restricting mimes then prepend
      * with a period as Dropzone requires.
      */
     if (isset($config['mimes'])) {
         foreach ($config['mimes'] as &$extension) {
             $extension = '.' . $extension;
         }
     }
     /**
      * Determine the default max upload size.
      */
     if (!array_get($config, 'max')) {
         $post = str_replace('M', '', ini_get('post_max_size'));
         $file = str_replace('M', '', ini_get('upload_max_filesize'));
         array_set($config, 'max', $file > $post ? $post : $file);
     }
     return $config;
 }
 /**
  * Return the required flag.
  *
  * @return bool
  */
 public function isRequired()
 {
     if (!($required = parent::isRequired()) && array_get($this->getConfig(), 'min')) {
         return true;
     }
     return $required;
 }
 /**
  * Get the post value.
  *
  * @param null $default
  * @return null|Carbon
  */
 public function getPostValue($default = null)
 {
     if (!($value = array_filter((array) parent::getPostValue($default)))) {
         return null;
     }
     if ($this->getColumnType() === 'datetime' && count($value) !== 2) {
         return null;
     }
     return (new Carbon())->createFromFormat($this->getFormat(), implode(' ', $value));
 }
 /**
  * Get the rules.
  *
  * @return array
  */
 public function getRules()
 {
     $rules = parent::getRules();
     if ($min = array_get($this->getConfig(), 'min')) {
         $rules[] = 'min:' . $min;
     }
     if ($max = array_get($this->getConfig(), 'max')) {
         $rules[] = 'max:' . $max;
     }
     return $rules;
 }
 /**
  * Handle the command.
  *
  * @param Repository $config
  * @param Evaluator  $evaluator
  */
 public function handle(Repository $config, Evaluator $evaluator, Translator $translator)
 {
     if (!($fields = $config->get($this->fieldType->getNamespace('config/config')))) {
         $fields = $config->get($this->fieldType->getNamespace('config'), []);
     }
     $fields = $evaluator->evaluate($fields);
     foreach ($fields as $slug => $field) {
         /*
          * Determine the field label.
          */
         $label = $this->fieldType->getNamespace('config.' . $slug . '.label');
         if (!$translator->has($label)) {
             $label = $this->fieldType->getNamespace('config.' . $slug . '.name');
         }
         $field['label'] = array_get($field, 'label', $label);
         /*
          * Determine the instructions.
          */
         $instructions = $this->fieldType->getNamespace('config.' . $slug . '.instructions');
         if ($translator->has($instructions)) {
             $field['instructions'] = $instructions;
         }
         /*
          * Determine the placeholder.
          */
         $placeholder = $this->fieldType->getNamespace('config.' . $slug . '.placeholder');
         if ($translator->has($placeholder)) {
             $field['placeholder'] = $placeholder;
         }
         /*
          * Determine the warning.
          */
         $warning = $this->fieldType->getNamespace('config.' . $slug . '.warning');
         if ($translator->has($warning)) {
             $field['warning'] = $warning;
         }
         /*
          * Set the configuration value.
          */
         $field['value'] = array_get($this->fieldType->getConfig(), $slug);
         // Prefix the slugs.
         $field['field'] = 'config__' . $slug;
         $fields['config__' . $slug] = $field;
         $this->builder->addField($field);
     }
 }
 /**
  * Drop the field type column from the table.
  *
  * @param Blueprint $table
  */
 public function dropColumn(Blueprint $table)
 {
     // Skip if no column type.
     if (!$this->fieldType->getColumnType()) {
         return;
     }
     // Skip if the column doesn't exist.
     if (!$this->schema->hasColumn($table->getTable(), $this->fieldType->getColumnName())) {
         return;
     }
     // Drop dat 'ole column.
     $table->dropColumn($this->fieldType->getColumnName());
 }
 /**
  * Get the config array.
  *
  * @return array
  */
 public function getConfig()
 {
     $config = parent::getConfig();
     /**
      * Get the configuration preset.
      */
     $configuration = config($this->getNamespace('redactor.configurations.' . $this->config('configuration', 'default')));
     /**
      * Set the buttons and plugins from config.
      */
     $config['buttons'] = $this->config('buttons', $configuration['buttons']);
     $config['plugins'] = $this->config('plugins', $configuration['plugins']);
     return $config;
 }
 /**
  * Get the config array.
  *
  * @return array
  */
 public function getConfig()
 {
     $config = parent::getConfig();
     /*
      * Get the configuration values.
      */
     $configuration = config($this->getNamespace('redactor.configurations.' . $this->config('configuration', 'default')));
     $buttons = array_keys(config($this->getNamespace('redactor.buttons')));
     $plugins = array_keys(config($this->getNamespace('redactor.plugins')));
     /*
      * Set the buttons and plugins from config.
      */
     $config['buttons'] = array_intersect((array) $this->config('buttons', $configuration['buttons']), $buttons);
     $config['plugins'] = array_intersect((array) $this->config('plugins', $configuration['plugins']), $plugins);
     return $config;
 }
 /**
  * Restore the field type column to cache.
  *
  * @param Blueprint $table
  */
 public function restoreColumn(Blueprint $table)
 {
     // Skip if no column type.
     if (!$this->fieldType->getColumnType()) {
         return;
     }
     // Skip if the column doesn't exist.
     if (!$this->schema->hasColumn($table->getTable(), $this->fieldType->getColumnName())) {
         return;
     }
     // Translatable or no?
     $translatable = ends_with($table->getTable(), '_translations');
     // Restore the data.
     $results = $this->cache->get(__CLASS__ . $this->fieldType->getColumnName());
     foreach ($results as $result) {
         $result = (array) $result;
         $this->connection->table($table->getTable())->where($translatable ? 'entry_id' : 'id', array_pull($result, 'id'))->update($result);
     }
     $this->cache->forget(__CLASS__ . $this->fieldType->getColumnName());
 }
 /**
  * Get the database column name.
  *
  * @return null|string
  */
 public function getColumnName()
 {
     return parent::getColumnName() . '_id';
 }
 /**
  * Get the post value.
  *
  * @param null $default
  * @return bool
  */
 public function getPostValue($default = null)
 {
     return filter_var(parent::getPostValue($default), FILTER_VALIDATE_BOOLEAN);
 }
 /**
  * Return the required flag.
  *
  * @return bool
  */
 public function isRequired()
 {
     if ($_POST && ($value = array_get($_POST, $this->getInputName() . '_id'))) {
         return false;
     }
     return parent::isRequired();
 }
 /**
  * Rename the pivot table.
  *
  * @param Blueprint $table
  * @param FieldType $from
  */
 public function renameColumn(Blueprint $table, FieldType $from)
 {
     $this->schema->rename($table->getTable() . '_' . $from->getField(), $table->getTable() . '_' . $this->fieldType->getField());
 }
 /**
  * Order a query in the given direction
  * by a field using this field type.
  *
  * @param Builder $query
  * @param         $direction
  */
 public function orderBy(Builder $query, $direction)
 {
     $query->orderBy($this->fieldType->getColumnName(), $direction);
 }
 /**
  * Get the post value.
  *
  * @param  null $default
  * @return array
  */
 public function getPostValue($default = null)
 {
     return array_filter(explode(',', parent::getPostValue($default)));
 }
 /**
  * Get the column type.
  *
  * @return string
  */
 public function getColumnType()
 {
     return array_get($this->getConfig(), 'column_type', parent::getColumnType());
 }
 /**
  * Get the post value.
  *
  * @param null $default
  * @return array
  */
 public function getPostValue($default = null)
 {
     return explode(',', parent::getPostValue($default));
 }
 /**
  * Get the addon routes.
  *
  * @return array
  */
 public function getRoutes()
 {
     $routes = [];
     foreach (glob($this->addon->getPath('resources/routes/*')) as $include) {
         $include = (require $include);
         if (!is_array($include)) {
             continue;
         }
         $routes = array_merge($include, $routes);
     }
     return array_merge($this->routes, $routes);
 }
 /**
  * Return the validation value.
  *
  * @param null $default
  * @return mixed
  */
 public function getValidationValue($default = null)
 {
     return parent::getPostValue($default);
 }