Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strategies #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Hydrator/Strategy/ClosureStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(callable $extractionClosure = null, callable $hydrat
/**
* {@inheritDoc}
*/
public function extract($value, ExtractionContext $context = null)
public function extract($value, $property, ExtractionContext $context = null)
{
if (!$this->extractionClosure) {
return $value;
Expand All @@ -64,7 +64,7 @@ public function extract($value, ExtractionContext $context = null)
/**
* {@inheritDoc}
*/
public function hydrate($value, HydrationContext $context = null)
public function hydrate($value, $property, HydrationContext $context = null)
{
if (!$this->hydrationClosure) {
return $value;
Expand Down
4 changes: 2 additions & 2 deletions src/Hydrator/Strategy/DateStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getFormat()
/**
* {@inheritDoc}
*/
public function extract($value, ExtractionContext $context = null)
public function extract($value, $property, ExtractionContext $context = null)
{
if (is_int($value)) {
$timestamp = $value;
Expand All @@ -83,7 +83,7 @@ public function extract($value, ExtractionContext $context = null)
/**
* {@inheritDoc}
*/
public function hydrate($value, HydrationContext $context = null)
public function hydrate($value, $property, HydrationContext $context = null)
{
return new DateTime($value);
}
Expand Down
15 changes: 11 additions & 4 deletions src/Hydrator/Strategy/HydratorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
use Hydrator\Context\ExtractionContext;
use Hydrator\Context\HydrationContext;
use Hydrator\HydratorInterface;
use Hydrator\NamingStrategy\ProvidesNamingStrategyTrait;

/**
* This strategy allows to set another hydrator for a given strategy. This is especially
* useful when you have embedded objects and want to hydrate/extract recursively
*/
class HydratorStrategy implements StrategyInterface
{
use ProvidesNamingStrategyTrait;

/**
* @var HydratorInterface
*/
Expand All @@ -44,10 +47,10 @@ public function __construct(HydratorInterface $hydrator)
/**
* {@inheritDoc}
*/
public function extract($value, ExtractionContext $context = null)
public function extract($value, $property, ExtractionContext $context = null)
{
if (is_object($value)) {
return $this->hydrator->extract($value, $context);
return $this->hydrator->extract($value);
}

return $value;
Expand All @@ -56,10 +59,14 @@ public function extract($value, ExtractionContext $context = null)
/**
* {@inheritDoc}
*/
public function hydrate($value, HydrationContext $context = null)
public function hydrate($value, $property, HydrationContext $context = null)
{
if (is_array($value)) {
return $this->hydrator->hydrate($value, $context);
$property = $this->namingStrategy->getNameForHydration($property, $context);
$method = 'get' . $property;
$object = $context->object->{$method}();

return $this->hydrator->hydrate($value, $object);
}

return $value;
Expand Down
10 changes: 6 additions & 4 deletions src/Hydrator/Strategy/StrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ interface StrategyInterface
/**
* Converts the given value so that it can be extracted by the hydrator
*
* @param mixed $value The original value
* @param mixed $value The original value
* @param string $property The name of the property
* @param ExtractionContext|null $context
* @return mixed
*/
public function extract($value, ExtractionContext $context = null);
public function extract($value, $property, ExtractionContext $context = null);

/**
* Converts the given value so that it can be hydrated by the hydrator
*
* @param mixed $value The original value
* @param mixed $value The original value
* @param string $property The name of the property
* @param HydrationContext|null $context
* @return mixed
*/
public function hydrate($value, HydrationContext $context = null);
public function hydrate($value, $property, HydrationContext $context = null);
}