/**
  * Guess the assignment names.
  *
  * @param Migration $migration
  */
 public function guess(Migration $migration)
 {
     /**
      * If we don't have any addon then
      * we can't automate anything.
      *
      * @var Addon $addon
      */
     if (!($addon = $migration->getAddon())) {
         return;
     }
     $stream = $migration->getStream();
     $stream = $this->streams->findBySlugAndNamespace(array_get($stream, 'slug'), array_get($stream, 'namespace'));
     if (!$stream) {
         return;
     }
     $locale = $this->config->get('app.fallback_locale');
     $assignments = $migration->getAssignments();
     foreach ($assignments as &$assignment) {
         foreach (['label', 'warning', 'instructions', 'placeholder'] as $key) {
             if (is_null(array_get($assignment, $locale . '.' . $key))) {
                 $assignment = array_add($assignment, $locale . '.' . $key, $addon->getNamespace('field.' . array_get($assignment, 'field') . '.' . $key . '.' . $stream->getSlug()));
             }
         }
     }
     $migration->setAssignments($assignments);
 }
 /**
  * Normalize the assignments input.
  *
  * @param Migration $migration
  */
 public function normalize(Migration $migration)
 {
     $locale = $this->config->get('app.fallback_locale');
     $stream = $migration->getStream();
     $assignments = $migration->getAssignments();
     foreach ($assignments as $field => &$assignment) {
         /*
          * If the assignment is a simple string
          * then the assignment is the field slug.
          */
         if (is_string($assignment)) {
             $assignment = ['field' => $assignment];
         }
         /*
          * Generally the field will be the
          * array key. Make sure we have one.
          */
         if (!isset($assignment['field'])) {
             $assignment['field'] = $field;
         }
         /*
          * If any of the translatable items exist
          * in the base array then move them up into
          * the translation array.
          */
         foreach (['label', 'warning', 'instructions', 'placeholder'] as $key) {
             if ($value = array_pull($assignment, $key)) {
                 $assignment = array_add($assignment, $locale . '.' . $key, $value);
             }
         }
     }
     $migration->setAssignments($assignments);
 }
 /**
  * Handle the command.
  *
  * @param FieldRepositoryInterface      $fields
  * @param StreamRepositoryInterface     $streams
  * @param AssignmentRepositoryInterface $assignments
  */
 public function handle(FieldRepositoryInterface $fields, StreamRepositoryInterface $streams, AssignmentRepositoryInterface $assignments)
 {
     $addon = $this->migration->getAddon();
     $stream = $this->migration->getStream();
     $namespace = array_get($stream, 'namespace', $this->migration->getNamespace());
     $slug = array_get($stream, 'slug', $addon ? $addon->getSlug() : null);
     $stream = $streams->findBySlugAndNamespace($slug, $namespace);
     foreach ($this->migration->getAssignments() as $field => $assignment) {
         if (is_numeric($field)) {
             $field = $assignment;
         }
         if ($stream && ($field = $fields->findBySlugAndNamespace($field, $namespace))) {
             if ($assignment = $assignments->findByStreamAndField($stream, $field)) {
                 $assignments->delete($assignment);
             }
         }
     }
     $assignments->cleanup();
 }