Commit d42321d1 authored by Jan Kuchař's avatar Jan Kuchař

Merge branch 'mixed-keys-test' into 'master'

Mixed keys test

See merge request !8
parents 4c66fedf 67155b08
Pipeline #14368 passed with stages
in 36 seconds
......@@ -4,6 +4,7 @@ namespace Grifart\Enum\Internal;
use Grifart\Enum\Enum;
use Grifart\Enum\MissingValueDeclarationException;
use Grifart\Enum\UsageException;
final class Meta
{
......@@ -34,13 +35,26 @@ final class Meta
*/
private function buildScalarToValueMapping(array $values): array {
$scalarToValues = [];
// check type of all scalar values
$keyType = null;
foreach($values as $value) {
$scalar = $value->getScalar();
if ($keyType === NULL) {
$keyType = \gettype($scalar);
}
if ($keyType !== \gettype($scalar)) {
throw new UsageException('Mixed types of scalar value. Keys must either all string or all int.');
}
}
foreach($values as $value) {
$scalar = $value->getScalar();
if (isset($scalarToValues[$scalar])) {
throw new \LogicException('You have provided duplicated values scalar names.');
throw new \LogicException('You have provided duplicated scalar values.');
}
if(!$this->hasConstantForScalar($scalar)) {
throw new \LogicException("Provided instance contains scalar value '$scalar'. But no corresponding constant of enum was found.");
throw new \LogicException("Provided instance contains scalar value '$scalar'. But no corresponding constant was found.");
}
$scalarToValues[$scalar] = $value;
......
<?php declare(strict_types=1);
use Grifart\Enum\MissingValueDeclarationException;
require __DIR__ . '/../bootstrap.php';
/**
* @method static EnumMixedKeys VALUE_STRING()
* @method static EnumMixedKeys VALUE_INT()
*/
class EnumMixedKeys extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
private const VALUE_STRING = '1';
private const VALUE_INT = 1;
}
\Tester\Assert::exception(
function() {
// first access triggers initial enum validation
EnumMixedKeys::VALUE_INT();
},
\Grifart\Enum\UsageException::class,
'Mixed types of scalar value. Keys must either all string or all int.'
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment