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

Commit

Permalink
Improve ObjectHydrator performance
Browse files Browse the repository at this point in the history
  • Loading branch information
kleijnweb committed Aug 28, 2018
1 parent 47410fb commit 65d4551
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Hydrator/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
use KleijnWeb\PhpApi\Descriptions\Hydrator\ProcessorBuilder;
use KleijnWeb\PhpApi\Descriptions\Hydrator\Processors\Processor;

/**
* Wrapper around ProcessorBuilder for compatibility
Expand All @@ -23,8 +24,14 @@ class ObjectHydrator
*/
private $builder;

/**
* @var Processor
*/
private $processors = [];

/**
* ObjectHydrator constructor.
*
* @param ProcessorBuilder $builder
*/
public function __construct(ProcessorBuilder $builder)
Expand All @@ -35,20 +42,30 @@ public function __construct(ProcessorBuilder $builder)
/**
* @param mixed $value
* @param Schema $schema
*
* @return mixed
*/
public function hydrate($value, Schema $schema)
{
return $this->builder->build($schema)->hydrate($value);
return $this->getProcessor($schema)->hydrate($value);
}

/**
* @param mixed $value
* @param Schema $schema
*
* @return mixed
*/
public function dehydrate($value, Schema $schema)
{
return $this->builder->build($schema)->dehydrate($value);
return $this->getProcessor($schema)->dehydrate($value);
}

private function getProcessor(Schema $schema): Processor
{
if (!isset($this->processors[spl_object_id($schema)])) {
$this->processors[spl_object_id($schema)] = $this->builder->build($schema);
}
return $this->processors[spl_object_id($schema)];
}
}

0 comments on commit 65d4551

Please sign in to comment.