<?php

/**
 * @file
 * Contains \Drupal\migrate_pets\Plugin\migrate\source\PetSource
 */

namespace Drupal\migrate_pets\Plugin\migrate\source;

use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Row;

/**
* Pet source from database.
*
* @MigrateSource(
*   id = "pet_source"
* )
*/
class PetSource extends SQLBase {

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return [
      'pid' => $this->t('Pet id'),
      'oid' => $this->t('Owner id'),
      'type' => $this->t('Pet type'),
      'name' => $this->t('Pet name'),
      'picture' => $this->t('Pet photo'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    return ['pid' => ['type' => 'integer']];
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = $this->select('pet', 'p')
      ->fields('p', ['pid', 'type', 'name', 'picture'])
      ->condition('picture', 'IS NOT NULL');
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    if ($picture = $row->getSourceProperty('picture')) {
      $row->setSourceProperty('picture', $this->animate($picture));
    }
    return parent::prepareRow($row);
  }

}
