Commit 2b2dd89d authored by Jan Kuchař's avatar Jan Kuchař

added implementation of __toString() method

parent fb770fef
Pipeline #14393 passed with stages
in 1 minute and 30 seconds
......@@ -123,6 +123,14 @@ abstract class Enum
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();
* Retrieves constant name that is used to access enum value.
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());
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