From 576e49a594ae19c35ef96044a3d08c92039e3efc Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink Date: Mon, 5 Nov 2018 19:15:02 +0100 Subject: [PATCH] Added more flexibility for getting a record's index This allows us to return a specific value in stead of just a key --- CHANGELOG.md | 4 ++++ composer.json | 2 +- src/Converters/Models/Base.php | 4 ++-- src/Interfaces/ConverterInterface.php | 4 +++- src/Mappers/ModelMapper.php | 8 ++++---- tests/unit/Mappers/ModelMapperTest.php | 4 +++- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a60a8399..3ef40144 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 4.1.0 - 2018-11-06 +### Added +- Added more flexibility for getting a record's index + ### 4.0.17 - 2018-09-24 ### Fixed - Fixed backwards compatibility of element index mapper diff --git a/composer.json b/composer.json index efffb876..08f78246 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "version": "4.0.17", + "version": "4.1.0", "name": "nerds-and-company/schematic", "description": "Craft setup and sync tool", "type": "craft-plugin", diff --git a/src/Converters/Models/Base.php b/src/Converters/Models/Base.php index 12ec3681..ed2d3b59 100644 --- a/src/Converters/Models/Base.php +++ b/src/Converters/Models/Base.php @@ -55,9 +55,9 @@ abstract public function deleteRecord(Model $record): bool; /** * {@inheritdoc} */ - public function getRecordIndex(): string + public function getRecordIndex(Model $record): string { - return 'handle'; + return $record->handle; } /** diff --git a/src/Interfaces/ConverterInterface.php b/src/Interfaces/ConverterInterface.php index 2f4b56d4..740fb6fa 100644 --- a/src/Interfaces/ConverterInterface.php +++ b/src/Interfaces/ConverterInterface.php @@ -39,9 +39,11 @@ public function deleteRecord(Model $record): bool; /** * Gets the record's key to index by. * + * @param Model $record + * * @return string */ - public function getRecordIndex(): string; + public function getRecordIndex(Model $record): string; /** * Get single record definition. diff --git a/src/Mappers/ModelMapper.php b/src/Mappers/ModelMapper.php index 35cd8624..e7fd48bb 100644 --- a/src/Mappers/ModelMapper.php +++ b/src/Mappers/ModelMapper.php @@ -31,8 +31,8 @@ public function export(array $records): array $modelClass = get_class($record); $converter = Craft::$app->controller->module->getConverter($modelClass); if ($converter) { - $index = $converter->getRecordIndex(); - $result[$record->$index] = $converter->getRecordDefinition($record); + $index = $converter->getRecordIndex($record); + $result[$index] = $converter->getRecordDefinition($record); } } @@ -102,8 +102,8 @@ private function getRecordsByHandle(array $records): array foreach ($records as $record) { $modelClass = get_class($record); $converter = Craft::$app->controller->module->getConverter($modelClass); - $index = $converter->getRecordIndex(); - $recordsByHandle[$record->$index] = $record; + $index = $converter->getRecordIndex($record); + $recordsByHandle[$index] = $record; } return $recordsByHandle; diff --git a/tests/unit/Mappers/ModelMapperTest.php b/tests/unit/Mappers/ModelMapperTest.php index e98a980a..da4054ff 100644 --- a/tests/unit/Mappers/ModelMapperTest.php +++ b/tests/unit/Mappers/ModelMapperTest.php @@ -267,7 +267,9 @@ private function getMockConverter(): Converter $mockConverter->expects($this->any()) ->method('getRecordIndex') - ->willReturn('handle'); + ->willReturnCallback(function ($model) { + return $model->handle; + }); $mockConverter->expects($this->any()) ->method('getRecordDefinition')