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

Merge branch 'better-access-to-scalar-value' into 'master'

Better access to scalar value

See merge request !10
parents 3f0d7920 2b2dd89d
Pipeline #14394 passed with stages
in 37 seconds
......@@ -104,23 +104,31 @@ abstract class Enum
// -------- INSTANCE IMPLEMENTATION ---------
/** @var int|string */
private $scalar;
private $scalarValue;
/**
* @param int|string $scalar
* @param int|string $scalarValue
*/
protected function __construct($scalar)
protected function __construct($scalarValue)
{
$this->scalar = $scalar;
$this->scalarValue = $scalarValue;
}
/**
* Provides scalar representation of enum value.
* Returns scalar representation of enum value.
* @return int|string
*/
public function getScalar()
public function toScalar()
{
return $this->scalar;
return $this->scalarValue;
}
public function __toString(): string
{
// as enum does not allow mixed key types (all must be int or all string),
// we can safely convert integers to strings without worrying introducing
// value conflicts
return (string) $this->toScalar();
}
/**
......@@ -132,7 +140,7 @@ abstract class Enum
public function getConstantName(): string
{
return self::getMeta()->getConstantNameForScalar(
$this->getScalar()
$this->toScalar()
);
}
......@@ -152,6 +160,6 @@ abstract class Enum
*/
public function scalarEquals($theOtherScalarValue): bool
{
return $this->getScalar() === $theOtherScalarValue;
return $this->toScalar() === $theOtherScalarValue;
}
}
......@@ -39,7 +39,7 @@ final class Meta
// check type of all scalar values
$keyType = null;
foreach($values as $value) {
$scalar = $value->getScalar();
$scalar = $value->toScalar();
if ($keyType === NULL) {
$keyType = \gettype($scalar);
}
......@@ -49,7 +49,7 @@ final class Meta
}
foreach($values as $value) {
$scalar = $value->getScalar();
$scalar = $value->toScalar();
if (isset($scalarToValues[$scalar])) {
throw new UsageException('You have provided duplicated scalar values.');
}
......
......@@ -11,9 +11,6 @@
namespace Grifart\Enum;
// Project root exceptions:
use Throwable;
class UsageException extends \LogicException {}
abstract class RuntimeException extends \RuntimeException {}
......
<?php
declare(strict_types=1);
require __DIR__ . '/../bootstrap.php';
/**
* @method static EnumString VALUE1()
* @method static EnumString VALUE2()
*/
class EnumString extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
protected const VALUE1 = 'value1';
protected const VALUE2 = 'value2';
}
\Tester\Assert::same('value1', EnumString::VALUE1()->toScalar());
\Tester\Assert::same('value1', (string) EnumString::VALUE1());
\Tester\Assert::same('value2', EnumString::VALUE2()->toScalar());
\Tester\Assert::same('value2', (string) EnumString::VALUE2());
/**
* @method static EnumInt VALUE1()
* @method static EnumInt VALUE2()
*/
class EnumInt extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
protected const VALUE1 = 1;
protected const VALUE2 = 2;
}
\Tester\Assert::same(1, EnumInt::VALUE1()->toScalar());
\Tester\Assert::same('1', (string) EnumInt::VALUE1());
\Tester\Assert::same(2, EnumInt::VALUE2()->toScalar());
\Tester\Assert::same('2', (string) EnumInt::VALUE2());
......@@ -23,5 +23,5 @@ class OrderState extends \Grifart\Enum\Enum
\Tester\Assert::notSame(OrderState::NEW(), OrderState::DELIVERED());
\Tester\Assert::notSame(OrderState::ACTIVE(), OrderState::DELIVERED());
\Tester\Assert::same('active', OrderState::ACTIVE()->getScalar());
\Tester\Assert::same('active', OrderState::ACTIVE()->toScalar());
\Tester\Assert::same(OrderState::ACTIVE(), OrderState::fromScalar('active'));
......@@ -29,5 +29,5 @@ abstract class ReflectionConstantNames2 extends \Grifart\Enum\Enum
}
}
\Tester\Assert::same('new', ReflectionConstantNames2::NEW()->getScalar());
\Tester\Assert::same('active', ReflectionConstantNames2::ACTIVE()->getScalar());
\Tester\Assert::same('new', ReflectionConstantNames2::NEW()->toScalar());
\Tester\Assert::same('active', ReflectionConstantNames2::ACTIVE()->toScalar());
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