Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Merge ArrayMapNamingStrategy into MapNamingStrategy #82

Conversation

weierophinney
Copy link
Member

Per #9, the two perform almost identical functionality; the main difference is which argument is first in the constructor, and the fact that MapNamingStrategy allowed a second argument for the opposite direction.

This patch merges the two into MapNamingStrategy. The class is now final (as was ArrayMapNamingStrategy, and accepts either a $hydrationMap or an $extractionMap or both. If both are missing, it raises an exception. If one or the other is missing, the value becomes that of the array_flip of the other value present.

The patch includes a migration document section detailing the changes, and expands the MapNamingStrategy documentation to cover all use cases.

Fixes #9

src/NamingStrategy/MapNamingStrategy.php Outdated Show resolved Hide resolved
src/NamingStrategy/MapNamingStrategy.php Outdated Show resolved Hide resolved
src/NamingStrategy/MapNamingStrategy.php Outdated Show resolved Hide resolved
src/NamingStrategy/MapNamingStrategy.php Outdated Show resolved Hide resolved
src/NamingStrategy/MapNamingStrategy.php Outdated Show resolved Hide resolved
src/NamingStrategy/MapNamingStrategy.php Outdated Show resolved Hide resolved
src/NamingStrategy/MapNamingStrategy.php Outdated Show resolved Hide resolved
test/NamingStrategy/MapNamingStrategyTest.php Show resolved Hide resolved
@weierophinney weierophinney force-pushed the feature/9-merge-map-naming-strategies branch from c179169 to 8464c4c Compare December 3, 2018 15:35
@weierophinney
Copy link
Member Author

@Ocramius I've created three named constructors:

  • createFromExtractionMap(array $extractionMap)
  • createFromHydrationMap(array $hydrationMap)
  • createFromAssymetricMap(array $extractionMap, array $hydrationMap

This solved the various issues I had with phpstan, and, as you note, it makes the behavior far more clear.

I've added tests to check for non-string keys as well; these are almost like those for values, but I had to remove the array and object cases, as PHP considers those invalid index values and throws fatals anyways.

Thanks for the review!

weierophinney added a commit to weierophinney/zend-hydrator that referenced this pull request Dec 3, 2018
Per zendframework#9, the two perform almost identical functionality; the main
difference is _which argument is first in the constructor_, and the fact
that `MapNamingStrategy` allowed a second argument for the opposite
direction.

This patch merges the two into `MapNamingStrategy`. The class is now
final (as was `ArrayMapNamingStrategy`, and accepts either a
`$hydrationMap` or an `$extractionMap` or both. If both are missing, it
raises an exception. If one or the other is missing, the value becomes
that of the `array_flip` of the other value present.

The patch includes a migration document section detailing the changes,
and expands the `MapNamingStrategy` documentation to cover all use
cases.
…ctor

phpstan was complaining that the value passed to `flipMapping()` could
be null, and that the assignment to each of `extractionMap` and
`hydrationMap` could potentially be null but should not be. This patch
adds explicit `is_array()` checks before attempting to call
`flipMapping()` with a value, and casting to `array` during assignment
to properties.
Removes the existing constructor, and creates three named constructors:

- `public static function createFromExtractionMap(array $extractionMap) : self`
- `public static function createFromHydrationMap(array $hydrationMap) : self`
- `public static function createFromAssymetricMap(array $extractionMap, array $hydrationMap) : self`

Doing so removes all the various constructor checks we were previously
using.

The patch also updates `flipMapping()` as follows:

- It requires string values _only_; integer are not allowed.
- It also checks the keys, as they can only be string values.

Finally, it updates the typehints on both the extraction and hydration
maps to `array<string, string`, as the keys are also important.
@weierophinney weierophinney force-pushed the feature/9-merge-map-naming-strategies branch from 966fe17 to 7cabf9c Compare December 3, 2018 17:24
@weierophinney weierophinney merged commit 7cabf9c into zendframework:develop Dec 3, 2018
@weierophinney weierophinney deleted the feature/9-merge-map-naming-strategies branch December 3, 2018 17:25
Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weierophinney I don't see a private function __construct for the MapNamingStrategy

@Ocramius
Copy link
Member

Ocramius commented Dec 3, 2018

Moved discussion point into #85

@Ocramius
Copy link
Member

Ocramius commented Dec 3, 2018

Feedback handled in #86

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants