Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate enum variants factory methods with all common methods #5

Closed
kunicmarko20 opened this issue Oct 2, 2019 · 0 comments
Closed

Comments

@kunicmarko20
Copy link
Owner

From:

final class Version
{
    private const V1 = 'v1';

    private const V2 = 'v2';
}

to:

final class Version
{
    private const V1 = 'v1';

    private const V2 = 'v2';

    private const ALL_MAP = [
        self::V1,
        self::V2,
    ];

    /**
     * @var string
     */
    private $value;

    private function __construct(string $value)
    {
        Assert::that($value)->inArray(Self::ALL_MAP);

        $this->value = $value;
    }

    public static function fromString(string $value): self
    {
        return new self($value);
    }

    public static function v1(): self
    {
        return new self(self::V1);
    }

    public static function v2(): self
    {
        return new self(self::V2);
    }

    public function toString(): string
    {
        return $this->value;
    }

    public function equals(Enum $other): bool
    {
        return $this->value === $other->value;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant