-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from TomHAnderson/hotfix/cleanup
Hotfix/cleanup
- Loading branch information
Showing
12 changed files
with
190 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace ZF\Doctrine\GraphQL\Hydrator; | ||
|
||
use Zend\Hydrator\HydratorPluginManager; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Instantiator\Instantiator; | ||
use ZF\Doctrine\GraphQL\Context; | ||
|
||
/** | ||
* This tool centralizes all extract operations for the module. | ||
* By doing so caching and optimization of operations can be applied | ||
* by overriding the this class alias in the config. | ||
*/ | ||
class HydratorExtractToolDefault implements | ||
HydratorExtractToolInterface | ||
{ | ||
protected $hydratorManager; | ||
|
||
public function __construct(HydratorPluginManager $hydratorManager) | ||
{ | ||
$this->hydratorManager = $hydratorManager; | ||
} | ||
|
||
// Extract an array of entities and return a collection | ||
public function extractToCollection($entityArray, string $hydratorAlias, $options) | ||
{ | ||
$options = $this->optionsToArray($options); | ||
$hydrator = $this->hydratorManager->build($hydratorAlias, $options); | ||
|
||
$resultCollection = new ArrayCollection(); | ||
foreach ($entityArray as $value) { | ||
if (is_array($value)) { | ||
$resultCollection->add($value); | ||
} else { | ||
$resultCollection->add($hydrator->extract($value)); | ||
} | ||
} | ||
|
||
return $resultCollection; | ||
} | ||
|
||
// Extract a single entity | ||
public function extract($entity, string $hydratorAlias, $options) | ||
{ | ||
if (is_array($entity)) { | ||
return $entity; | ||
} | ||
|
||
$options = $this->optionsToArray($options); | ||
$hydrator = $this->hydratorManager->build($hydratorAlias, $options); | ||
|
||
return $hydrator->extract($entity); | ||
} | ||
|
||
public function getFieldArray(string $entityClassName, string $hydratorAlias, $options) | ||
{ | ||
$instantiator = new Instantiator(); | ||
$entity = $instantiator->instantiate($entityClassName); | ||
|
||
$options = $this->optionsToArray($options); | ||
$hydrator = $this->hydratorManager->build($hydratorAlias, $options); | ||
|
||
return array_keys($hydrator->extract($entity)); | ||
} | ||
|
||
private function optionsToArray($options) | ||
{ | ||
if ($options instanceof Context) { | ||
$options = $options->toArray(); | ||
} | ||
|
||
return $options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace ZF\Doctrine\GraphQL\Hydrator; | ||
|
||
use Interop\Container\ContainerInterface; | ||
|
||
class HydratorExtractToolDefaultFactory | ||
{ | ||
public function __invoke( | ||
ContainerInterface $container, | ||
$requestedName, | ||
array $options = null | ||
) { | ||
$hydratorManager = $container->get('HydratorManager'); | ||
|
||
return new HydratorExtractToolDefault($hydratorManager); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace ZF\Doctrine\GraphQL\Hydrator; | ||
|
||
interface HydratorExtractToolInterface | ||
{ | ||
public function extract($entity, string $hydratorAlias, $options); | ||
public function extractToCollection($entityArray, string $hydratorAlias, $options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.