-
Notifications
You must be signed in to change notification settings - Fork 10
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 #18 from liip/group-without-version
improve configuration for serializing versions and groups
- Loading branch information
Showing
9 changed files
with
339 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
# Changelog | ||
|
||
# 2.0.0 | ||
|
||
* [BC Break]: Configuration of the serializer generator changed to configuration model. | ||
The new format allows to more precisely specify which serializers to generate. | ||
|
||
# 1.0.0 | ||
|
||
Initial release |
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 |
---|---|---|
|
@@ -58,7 +58,7 @@ | |
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.0.x-dev" | ||
"dev-master": "2.x-dev" | ||
} | ||
}, | ||
"config": { | ||
|
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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Liip\Serializer\Configuration; | ||
|
||
class ClassToGenerate implements \IteratorAggregate | ||
{ | ||
/** | ||
* @var GeneratorConfiguration | ||
*/ | ||
private $configuration; | ||
|
||
/** | ||
* A list of group combinations, potentially with a version overwrite. | ||
* | ||
* @see GroupCombination::$groups | ||
* | ||
* @var GroupCombination[] | ||
*/ | ||
private $groupCombinations = []; | ||
|
||
/** | ||
* Fully qualified class name. | ||
* | ||
* @var string | ||
*/ | ||
private $className; | ||
|
||
/** | ||
* Overwrite global default list of versions to generate. | ||
* | ||
* @see GroupCombination::$versions | ||
* | ||
* @var string[]|null | ||
*/ | ||
private $defaultVersions; | ||
|
||
public function __construct(GeneratorConfiguration $configuration, string $className, ?array $defaultVersions = null) | ||
{ | ||
$this->configuration = $configuration; | ||
$this->className = $className; | ||
$this->defaultVersions = null === $defaultVersions ? null : array_map('strval', $defaultVersions); | ||
} | ||
|
||
public function getClassName(): string | ||
{ | ||
return $this->className; | ||
} | ||
|
||
public function getDefaultVersions(): array | ||
{ | ||
if (null !== $this->defaultVersions) { | ||
return $this->defaultVersions; | ||
} | ||
|
||
return $this->configuration->getDefaultVersions(); | ||
} | ||
|
||
public function addGroupCombination(GroupCombination $groupCombination): void | ||
{ | ||
$this->groupCombinations[] = $groupCombination; | ||
} | ||
|
||
public function getIterator() | ||
{ | ||
if ($this->groupCombinations) { | ||
return new \ArrayIterator($this->groupCombinations); | ||
} | ||
|
||
return new \ArrayIterator($this->configuration->getDefaultGroupCombinations($this)); | ||
} | ||
} |
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,114 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Liip\Serializer\Configuration; | ||
|
||
/** | ||
* Configuration for the serializer generator. | ||
* | ||
* The configuration has a list of default group combinations, a list of | ||
* default versions, and a list of classes. For each class, you can overwrite | ||
* the group combinations. For each group combination, you can again overwrite | ||
* the versions to generate. | ||
*/ | ||
class GeneratorConfiguration implements \IteratorAggregate | ||
{ | ||
/** | ||
* A list of group combinations. | ||
* | ||
* @see GroupCombination::$groups | ||
* | ||
* @var string[][] | ||
*/ | ||
private $defaultGroupCombinations; | ||
|
||
/** | ||
* List of versions to generate. An empty string '' means to generate without a version. | ||
* e.g. ['', '2', '3'] | ||
* | ||
* @var string[] | ||
*/ | ||
private $defaultVersions; | ||
|
||
/** | ||
* @var ClassToGenerate[] | ||
*/ | ||
private $classesToGenerate = []; | ||
|
||
public function __construct(array $defaultGroupCombinations, array $defaultVersions) | ||
{ | ||
$this->defaultGroupCombinations = $defaultGroupCombinations ?: [[]]; | ||
$this->defaultVersions = array_map('strval', $defaultVersions) ?: ['']; | ||
} | ||
|
||
/** | ||
* Create configuration from array definition | ||
* | ||
* [ | ||
* 'default_group_combinations' => ['api'], | ||
* 'default_versions' => ['', '1', '2'], | ||
* 'classes' => [ | ||
* Product::class => [ | ||
* 'default_versions' => ['1', '2'], // optional, falls back to global list | ||
* 'group_combinations' => [ // optional, falls back to global default_group_combinations | ||
* [ | ||
* 'groups' => [], // generate without groups | ||
* ], | ||
* [ | ||
* 'groups' => ['api'], // global groups are overwritten, not merged. versions are taken from class default | ||
* ], | ||
* [ | ||
* 'groups' => ['api', 'detail'], | ||
* 'versions' => ['2'], // only generate the combination of api and detail for version 2 | ||
* ], | ||
* ], | ||
* ], | ||
* Other::class => [], // generate this class with default groups and versions | ||
* ] | ||
* ] | ||
*/ | ||
public static function createFomArray(array $config): self | ||
{ | ||
if (!\array_key_exists('classes', $config) || \count($config['classes']) < 1) { | ||
throw new \InvalidArgumentException('You need to specify the classes to generate'); | ||
} | ||
$instance = new self($config['default_group_combinations'] ?? [], $config['default_versions'] ?? []); | ||
foreach ($config['classes'] as $className => $classConfig) { | ||
$classToGenerate = new ClassToGenerate($instance, $className, $classConfig['default_versions'] ?? null); | ||
foreach ($classConfig['group_combinations'] ?? [] as $groupCombination) { | ||
$classToGenerate->addGroupCombination( | ||
new GroupCombination($classToGenerate, $groupCombination['groups'], $groupCombination['versions'] ?? null) | ||
); | ||
} | ||
$instance->addClassToGenerate($classToGenerate); | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
public function addClassToGenerate(ClassToGenerate $classToGenerate): void | ||
{ | ||
$this->classesToGenerate[] = $classToGenerate; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getDefaultVersions(): array | ||
{ | ||
return $this->defaultVersions; | ||
} | ||
|
||
public function getDefaultGroupCombinations(ClassToGenerate $classToGenerate): array | ||
{ | ||
return array_map(static function (array $combination) use ($classToGenerate) { | ||
return new GroupCombination($classToGenerate, $combination); | ||
}, $this->defaultGroupCombinations); | ||
} | ||
|
||
public function getIterator() | ||
{ | ||
return new \ArrayIterator($this->classesToGenerate); | ||
} | ||
} |
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Liip\Serializer\Configuration; | ||
|
||
class GroupCombination | ||
{ | ||
/** | ||
* @var ClassToGenerate | ||
*/ | ||
private $containingClass; | ||
|
||
/** | ||
* One combination of groups to generate. | ||
* | ||
* An empty array means to generate with no groups. | ||
* e.g. ['api', 'details']. | ||
* | ||
* @var string[] | ||
*/ | ||
private $groups; | ||
|
||
/** | ||
* List of versions to generate. | ||
* | ||
* An empty string '' means to generate without a version. | ||
* e.g. ['', '2', '3'] | ||
* | ||
* If not specified, this falls back to the class default. | ||
* If the array is not null, it must have a length > 0. | ||
* | ||
* @var string[]|null | ||
*/ | ||
private $versions; | ||
|
||
public function __construct(ClassToGenerate $containingClass, array $groups, ?array $versions = null) | ||
{ | ||
$this->containingClass = $containingClass; | ||
$this->groups = $groups; | ||
if (null !== $versions && 0 === \count($versions)) { | ||
throw new \InvalidArgumentException('Version list may not be empty. To generate without version, specify an empty string. To use the default versions, pass null.'); | ||
} | ||
$this->versions = $versions; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getGroups(): array | ||
{ | ||
return $this->groups; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getVersions(): array | ||
{ | ||
if (null !== $this->versions) { | ||
return $this->versions; | ||
} | ||
|
||
return $this->containingClass->getDefaultVersions(); | ||
} | ||
} |
Oops, something went wrong.