diff --git a/CHANGELOG.md b/CHANGELOG.md index a8f305c7..7f0f10ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 4.1.0 - 2018-11-19 +### Added +- Added more flexibility for getting a record's index + ### 4.0.18 - 2018-11-19 ### Added - Delete empty field and site groups on import with force diff --git a/composer.json b/composer.json index 4da763e9..08f78246 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "version": "4.0.18", + "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 9d5dbac3..f2c6b4d6 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')