Created
March 14, 2018 16:09
-
-
Save thatside/4fc671ad79619775819be74dc39f33df to your computer and use it in GitHub Desktop.
Enum-like example
This file contains hidden or 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
<?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); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace Library; | |
abstract class EnumLike | |
{ | |
protected $value; | |
protected function __construct(string $value) | |
{ | |
$this->value = $value; | |
} | |
public function __toString() : string | |
{ | |
return $this->value; | |
} | |
} |
This file contains hidden or 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
<?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