Commit e919879f authored by claudiu.cristea's avatar claudiu.cristea Committed by claudiu.cristea

Issue #3175430 by claudiu.cristea: ConfigurablePluginInterface was removed

parent cffc6aa7
<?php
declare(strict_types = 1);
namespace Drupal\migrate_spreadsheet\Plugin\migrate\source;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate_spreadsheet\SpreadsheetIterator;
use Drupal\migrate_spreadsheet\SpreadsheetIteratorInterface;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -21,10 +25,10 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* spreadsheet files.
*
* @MigrateSource(
* id = "spreadsheet"
* id = "spreadsheet",
* )
*/
class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterface, ContainerFactoryPluginInterface {
class Spreadsheet extends SourcePluginBase implements ConfigurableInterface, DependentPluginInterface, ContainerFactoryPluginInterface {
/**
* The file system service.
......@@ -73,7 +77,7 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL): self {
return new static(
$configuration,
$plugin_id,
......@@ -87,7 +91,7 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
public function defaultConfiguration(): array {
return [
'file' => NULL,
'worksheet' => NULL,
......@@ -102,7 +106,7 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
public function setConfiguration(array $configuration): void {
$this->configuration = NestedArray::mergeDeep(
$this->defaultConfiguration(),
$configuration
......@@ -112,21 +116,21 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
/**
* {@inheritdoc}
*/
public function getConfiguration() {
public function getConfiguration(): array {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function __toString() {
public function __toString(): string {
return $this->configuration['file'] . ':' . $this->configuration['worksheet'];
}
/**
* {@inheritdoc}
*/
public function getIds() {
public function getIds(): array {
$config = $this->getConfiguration();
if (empty($config['keys'])) {
......@@ -144,7 +148,7 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
/**
* {@inheritdoc}
*/
public function fields() {
public function fields(): array {
// No column headers provided in config, read worksheet for header row.
if (!$columns = $this->getConfiguration()['columns']) {
$this->initializeIterator();
......@@ -160,7 +164,7 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
/**
* {@inheritdoc}
*/
public function initializeIterator() {
public function initializeIterator(): SpreadsheetIteratorInterface {
if (!$this->iteratorIsInitialized) {
$configuration = $this->getConfiguration();
$configuration['worksheet'] = $this->loadWorksheet();
......@@ -186,7 +190,7 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
* @throws \Drupal\migrate\MigrateException
* When it's impossible to load the file or the worksheet does not exist.
*/
protected function loadWorksheet() {
protected function loadWorksheet(): Worksheet {
$config = $this->getConfiguration();
// Check that the file exists.
......@@ -216,7 +220,6 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
// Advise the Reader of which worksheet we want to load.
$reader->setLoadSheetsOnly($config['worksheet']);
/** @var \PhpOffice\PhpSpreadsheet\Spreadsheet $workbook */
$workbook = $reader->load($file_path);
return $workbook->getSheet(0);
......@@ -230,7 +233,7 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
public function calculateDependencies(): array {
return [];
}
......
<?php
declare(strict_types = 1);
namespace Drupal\migrate_spreadsheet;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
......@@ -41,7 +43,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function key() {
public function key(): array {
if (empty($keys = $this->getKeys())) {
// If no keys were passed, use the spreadsheet current row position.
if (!$this->getRowIndexColumn()) {
......@@ -51,7 +53,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
}
return array_values(array_map(
function ($col_letter) {
function (string $col_letter) {
$cell_reference = "$col_letter{$this->getAbsoluteRowIndex()}";
if ($cell = $this->getWorksheet()->getCell($cell_reference, FALSE)) {
return $cell->getValue();
......@@ -66,14 +68,14 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function valid() {
public function valid(): bool {
return ($this->relativeRow >= 0) && ($this->getAbsoluteRowIndex() <= $this->getRowsCount());
}
/**
* {@inheritdoc}
*/
public function rewind() {
public function rewind(): void {
unset($this->absoluteRow);
$this->relativeRow = 0;
}
......@@ -81,7 +83,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function current() {
public function current(): array {
$keys = $this->getKeys();
$all_columns = $keys + $this->getColumns();
......@@ -98,7 +100,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
asort($all_columns);
return array_map(
function ($col_letter) {
function (string $col_letter) {
if ($col_letter === '@') {
return $this->getAbsoluteRowIndex();
}
......@@ -115,7 +117,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function next() {
public function next(): void {
unset($this->absoluteRow);
$this->relativeRow++;
}
......@@ -123,7 +125,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
public function setConfiguration(array $configuration): SpreadsheetIteratorInterface {
// Unset cached values.
$this->clearCache();
$this->configuration = $configuration;
......@@ -133,14 +135,14 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function getConfiguration() {
public function getConfiguration(): array {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function getWorksheet() {
public function getWorksheet(): Worksheet {
if (!isset($this->cache['worksheet'])) {
if (empty($this->configuration['worksheet']) || !$this->configuration['worksheet'] instanceof Worksheet) {
throw new \InvalidArgumentException("No valid 'worksheet' configuration.");
......@@ -153,7 +155,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function getOrigin() {
public function getOrigin(): string {
if (!isset($this->cache['origin'])) {
$config = $this->getConfiguration();
if (empty($config['origin'])) {
......@@ -177,7 +179,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function getColumns() {
public function getColumns(): array {
if (!isset($this->cache['columns'])) {
$headers = $this->getHeaders();
if (empty($this->configuration['columns'])) {
......@@ -201,7 +203,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function getKeys() {
public function getKeys(): array {
if (!isset($this->cache['keys'])) {
$this->cache['keys'] = [];
if (!empty($this->configuration['keys'])) {
......@@ -220,9 +222,9 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function getHeaderRow() {
public function getHeaderRow(): ?int {
if (!isset($this->cache['header_row'])) {
$header_row = isset($this->configuration['header_row']) ? $this->configuration['header_row'] : NULL;
$header_row = $this->configuration['header_row'] ?? NULL;
if ($header_row !== NULL && (!is_numeric($this->configuration['header_row']) || ($this->configuration['header_row'] < 1))) {
throw new \InvalidArgumentException("Wrong header_row value '{$this->configuration['header_row']}'.");
}
......@@ -234,8 +236,8 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function getRowIndexColumn() {
$column = empty($this->configuration['row_index_column']) ? NULL : $this->configuration['row_index_column'];
public function getRowIndexColumn(): ?string {
$column = $this->configuration['row_index_column'] ?? NULL;
if ($column) {
$headers = $this->getHeaders();
if (isset($headers[$column])) {
......@@ -248,7 +250,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function getHeaders() {
public function getHeaders(): array {
if (!isset($this->cache['headers'])) {
// Get the first column index (one based, A is 1).
$first_col_index = Coordinate::columnIndexFromString(Coordinate::coordinateFromString($this->getOrigin())[0]);
......@@ -278,7 +280,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function getRowsCount() {
public function getRowsCount(): int {
if (!isset($this->cache['rows_count'])) {
$this->cache['rows_count'] = $this->getWorksheet()->getHighestDataRow();
}
......@@ -288,7 +290,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function getColumnsCount() {
public function getColumnsCount(): int {
if (!isset($this->cache['columns_count'])) {
$this->cache['columns_count'] = Coordinate::columnIndexFromString($this->getWorksheet()->getHighestDataColumn());
}
......@@ -298,7 +300,7 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
/**
* {@inheritdoc}
*/
public function clearCache() {
public function clearCache(): void {
$this->cache = [];
}
......@@ -307,10 +309,13 @@ class SpreadsheetIterator implements SpreadsheetIteratorInterface {
*
* @return int
* The absolute row index.
*
* @throws \PhpOffice\PhpSpreadsheet\Exception
* On wrong coordinate string.
*/
protected function getAbsoluteRowIndex() {
protected function getAbsoluteRowIndex(): int {
if (!isset($this->absoluteRow)) {
$row = Coordinate::coordinateFromString($this->getOrigin())[1];
$row = (int) Coordinate::coordinateFromString($this->getOrigin())[1];
$this->absoluteRow = $row + $this->relativeRow;
}
return $this->absoluteRow;
......
<?php
declare(strict_types=1);
namespace Drupal\migrate_spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
/**
* Provides an interface for spreadsheet iterators.
*/
......@@ -32,7 +36,7 @@ interface SpreadsheetIteratorInterface extends \Iterator {
*
* @return $this
*/
public function setConfiguration(array $configuration);
public function setConfiguration(array $configuration): self;
/**
* Gets the iterator configuration.
......@@ -40,7 +44,7 @@ interface SpreadsheetIteratorInterface extends \Iterator {
* @return array
* The iterator configuration as an associative array.
*/
public function getConfiguration();
public function getConfiguration(): array;
/**
* Gets the worksheet.
......@@ -51,7 +55,7 @@ interface SpreadsheetIteratorInterface extends \Iterator {
* @throws \InvalidArgumentException
* If an empty or invalid 'worksheet' has been passed.
*/
public function getWorksheet();
public function getWorksheet(): Worksheet;
/**
* Retrieves the top-left origin of data area.
......@@ -59,7 +63,7 @@ interface SpreadsheetIteratorInterface extends \Iterator {
* @return string
* The top-left cell of data area (such as A2 or B5).
*/
public function getOrigin();
public function getOrigin(): string;
/**
* Gets the list of columns.
......@@ -72,7 +76,7 @@ interface SpreadsheetIteratorInterface extends \Iterator {
*
* @see \Drupal\migrate_spreadsheet\SpreadsheetIteratorInterface::setColumns()
*/
public function getColumns();
public function getColumns(): array;
/**
* Gets the list of columns that are composing the primary key.
......@@ -83,23 +87,23 @@ interface SpreadsheetIteratorInterface extends \Iterator {
* @throws \InvalidArgumentException
* If a key passed in 'keys' does not exist in the header.
*/
public function getKeys();
public function getKeys(): array;
/**
* Gets the header row index.
*
* @return int
* @return int|null
* The header row index.
*/
public function getHeaderRow();
public function getHeaderRow(): ?int;
/**
* Gets the name of the row index column.
*
* @return string
* @return string|null
* A name to be given to the column returning the numeric index of the row.
*/
public function getRowIndexColumn();
public function getRowIndexColumn(): ?string;
/**
* Retrieves a full list of headers.
......@@ -112,7 +116,7 @@ interface SpreadsheetIteratorInterface extends \Iterator {
* @throws \RuntimeException
* If a header cell is duplicated.
*/
public function getHeaders();
public function getHeaders(): array;
/**
* Gets the total number of rows in the worksheet.
......@@ -120,7 +124,7 @@ interface SpreadsheetIteratorInterface extends \Iterator {
* @return int
* The total number of rows in the worksheet.
*/
public function getRowsCount();
public function getRowsCount(): int;
/**
* Gets the total number of columns in the worksheet.
......@@ -128,11 +132,11 @@ interface SpreadsheetIteratorInterface extends \Iterator {
* @return int
* The total number of columns in the worksheet.
*/
public function getColumnsCount();
public function getColumnsCount(): int;
/**
* Clears the iterator internal, static cache.
*/
public function clearCache();
public function clearCache(): void;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment