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

Commit

Permalink
Merge 022a61c into 7cabf9c
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Dec 3, 2018
2 parents 7cabf9c + 022a61c commit f925324
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
19 changes: 10 additions & 9 deletions CHANGELOG.md
Expand Up @@ -21,15 +21,16 @@ All notable changes to this project will be documented in this file, in reverse
- The constructor is removed. If you instantiate an instance using
`new MapNamingStrategy(...)`, the strategy will act as a no-op.
- The class is now marked `final`.
- The class offers three new named constructors:
- `MapNamingStrategy::createFromExtractionMap(array $extractionMap) : MapNamingStrategy`
will use the provided extraction map for extraction operations, and flip it
for hydration operations.
- `MapNamingStrategy::createFromHydrationMap(array $hydrationMap) : MapNamingStrategy`
will use the provided hydration map for hydration operations, and flip it
for extraction operations.
- `MapNamingStrategy::createFromAssymetricMap(array $extractionMap, array $hydrationMap) : MapNamingStrategy`
will use the appropriate map based on the requested operation.
- The class offers three new named constructors; one of these MUST be used to
create an instance, as the constructor is now final:
- `MapNamingStrategy::createFromExtractionMap(array $extractionMap) : MapNamingStrategy`
will use the provided extraction map for extraction operations, and flip it
for hydration operations.
- `MapNamingStrategy::createFromHydrationMap(array $hydrationMap) : MapNamingStrategy`
will use the provided hydration map for hydration operations, and flip it
for extraction operations.
- `MapNamingStrategy::createFromAssymetricMap(array $extractionMap, array $hydrationMap) : MapNamingStrategy`
will use the appropriate map based on the requested operation.

- [#80](https://github.com/zendframework/zend-hydrator/pull/80) bumps the minimum supported PHP version to 7.2.

Expand Down
4 changes: 2 additions & 2 deletions docs/book/v3/migration.md
Expand Up @@ -190,8 +190,8 @@ In the first two cases, the constructor will flip the arrays for purposes of the
opposite interaction; e.g., using `createFromExtractionMap()` will create a
hydration map based on an `array_flip()` of the extraction map provided.

If you instantiate the naming strategy without these methods, it will act as a
no-op for purposes of both extraction and hydration.
**You MUST use one of these method to create an instance,** as the constructor
is now marked `private`.

## HydratorPluginManager

Expand Down
13 changes: 13 additions & 0 deletions src/NamingStrategy/MapNamingStrategy.php
Expand Up @@ -77,6 +77,19 @@ public function hydrate(string $name, ?array $data = null) : string
return $this->hydrationMap[$name] ?? $name;
}

/**
* Do not allow direct instantiation of this class.
*
* Users should use one of the named constructors:
*
* - createFromExtractionMap()
* - createFromHydrationMap()
* - createFromAssymetricMap()
*/
private function __construct()
{
}

/**
* Safely flip mapping array.
*
Expand Down
15 changes: 11 additions & 4 deletions test/NamingStrategy/MapNamingStrategyTest.php
Expand Up @@ -9,6 +9,7 @@

namespace ZendTest\Hydrator\NamingStrategy;

use Error;
use PHPUnit\Framework\TestCase;
use Zend\Hydrator\Exception;
use Zend\Hydrator\NamingStrategy\MapNamingStrategy;
Expand Down Expand Up @@ -40,6 +41,12 @@ public function invalidKeyValues() : iterable
yield 'float' => [1.1];
}

public function testConstructorIsPrivate()
{
$this->expectException(Error::class);
new MapNamingStrategy([], []);
}

/**
* @dataProvider invalidMapValues
* @param mixed $invalidValue
Expand Down Expand Up @@ -88,15 +95,15 @@ public function testHydrationMapConstructorRaisesExceptionWhenFlippingExtraction
MapNamingStrategy::createFromHydrationMap([$invalidKey => 'foo']);
}

public function testExtractReturnsVerbatimWhenNoExtractionMapProvided()
public function testExtractReturnsVerbatimWhenEmptyExtractionMapProvided()
{
$strategy = new MapNamingStrategy();
$strategy = MapNamingStrategy::createFromExtractionMap([]);
$this->assertEquals('some_stuff', $strategy->extract('some_stuff'));
}

public function testHydrateReturnsVerbatimWhenNoHydrationMapProvided()
public function testHydrateReturnsVerbatimWhenEmptyHydrationMapProvided()
{
$strategy = new MapNamingStrategy([]);
$strategy = MapNamingStrategy::createFromHydrationMap([]);
$this->assertEquals('some_stuff', $strategy->hydrate('some_stuff'));
}

Expand Down

0 comments on commit f925324

Please sign in to comment.