Skip to content

Instantly share code, notes, and snippets.

@thatside
Created March 14, 2018 16:09
Show Gist options
  • Save thatside/4fc671ad79619775819be74dc39f33df to your computer and use it in GitHub Desktop.
Save thatside/4fc671ad79619775819be74dc39f33df to your computer and use it in GitHub Desktop.
Enum-like example
<?php
use Library\EnumLike;
class ContractStatus extends EnumLike
{
const ACTIVE = 'active';
const STOPPING = 'stopping';
const STOPPED = 'stopped';
const TERMINATED = 'terminated';
public static function active() : self
{
return new self(self::ACTIVE);
}
public static function stopping() : self
{
return new self(self::STOPPING);
}
public static function stopped() : self
{
return new self(self::STOPPED);
}
public static function terminated() : self
{
return new self(self::TERMINATED);
}
}
<?php
namespace Library;
abstract class EnumLike
{
protected $value;
protected function __construct(string $value)
{
$this->value = $value;
}
public function __toString() : string
{
return $this->value;
}
}
<?php
namespace Tests;
use Library\EnumLike;
use PHPUnit\Framework\TestCase;
class EnumLikeTest extends TestCase
{
public function testEnumLike()
{
$enumLike = MyEnum::value();
$this->assertEquals(MyEnum::VALUE, $enumLike);
}
}
class MyEnum extends EnumLike
{
const VALUE = 'value';
public static function value()
{
return new self(self::VALUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment