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

Enum cases support for build query #884

Open
lynicidn opened this issue Oct 11, 2024 · 2 comments
Open

Enum cases support for build query #884

lynicidn opened this issue Oct 11, 2024 · 2 comments
Labels
status:ready for adoption Feel free to implement this issue. type:feature New feature

Comments

@lynicidn
Copy link
Contributor

lynicidn commented Oct 11, 2024

What steps will reproduce the problem?

$query->andWhere(['category_name' => CategoryEnum::blablacat])

What is the expected result?

Am want suggest add logic to query for transformate enum case object to case->value

$query->andWhere(['category_name' => CategoryEnum::blablacat->value])

What do you get instead?

this suggestion

Additional info

Q A
Version 3.x?
PHP version 8.x+
Operating system any
@lynicidn
Copy link
Contributor Author

к ознакомлению https://php.watch/versions/8.1/enums

я тут узнал, что можно оказывается методы засовывать в енум, и так же не запрещено вешать интерфейсы и метод __toString не запрещен в енумах, в связи с новыми входными данными, возможно рассмотреть превращение не UnitCase в $unit->value, а просто сделать поддержку всех Stringable интерфейсов, тогда неудобство с енум уйдет в какой то трейт

enum HTTPStatus: int {
    case OK = 200;
    case ACCESS_DENIED = 403;
    case NOT_FOUND = 404;

    public function label(): string {
        return static::getLabel($this);
    }

    public static function getLabel(self $value): string {
        return match ($value) {
            HTTPStatus::OK => 'OK',
            HTTPStatus::ACCESS_DENIED => 'Access Denied',
            HTTPStatus::NOT_FOUND => 'Page Not Found',
        };
    }
}

echo HTTPStatus::ACCESS_DENIED->label(); // "Access Denied"
echo HTTPStatus::getLabel(HTTPStatus::ACCESS_DENIED); // "Access Denied"

@vjik
Copy link
Member

vjik commented Oct 11, 2024

Need to add BackedEnum support. Integer/string enums implements this interface.

@samdark samdark added status:ready for adoption Feel free to implement this issue. type:feature New feature labels Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:feature New feature
Projects
None yet
Development

No branches or pull requests

3 participants