Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

i have this yaml, and i'd like to use enum (or const) value as a key.

framework:
    workflows:
      test:
        type: 'state_machine'
        initial_marking: !php/enum App\Entity\Status::draft
        places: !php/enum App\Entity\Status
        transitions:
          '!php/enum App\Entity\Status::draft':   # this does not resolve -> transition name is literally this string
            from:
              - !php/enum App\Entity\Status::finished
              - !php/enum App\Entity\Status::cancelled
            to: !php/enum App\Entity\Status::draft

when i quote it, then it's literal. when i remove the quotation, i get

The file "workflow.yaml" does not contain valid YAML: Non-string keys are not supported.

so i guess it's not possible at all? or is there any special syntax for this?

You must be logged in to vote

Using an enum as key is impossible because PHP arrays only allow string or integers as array keys, not objects (and enum cases are objects).

The following is invalid PHP code, while it is exactly the equivalent of your feature request

$transition = [
    App\Entity\Status::draft: [ // <- invalid offset type
        'from' => [App\Entity\Status::finished, App\Entity\Status::cancelled],
        'to' => App\Entity\Status::draft,
    ]
];

Side note: I assumed you meant !php/enum App\Entity\Status::draft as key, not '!php/enum App\Entity\Status::draft'. In Yaml, quoted string literals are always literal strings. The Yaml tag syntax is unquoted.

Replies: 1 comment · 1 reply

Comment options

Using an enum as key is impossible because PHP arrays only allow string or integers as array keys, not objects (and enum cases are objects).

The following is invalid PHP code, while it is exactly the equivalent of your feature request

$transition = [
    App\Entity\Status::draft: [ // <- invalid offset type
        'from' => [App\Entity\Status::finished, App\Entity\Status::cancelled],
        'to' => App\Entity\Status::draft,
    ]
];

Side note: I assumed you meant !php/enum App\Entity\Status::draft as key, not '!php/enum App\Entity\Status::draft'. In Yaml, quoted string literals are always literal strings. The Yaml tag syntax is unquoted.

You must be logged in to vote
1 reply
@kor3k
Comment options

thank you. yes it makes sense, i kinda thought it would use the ->value automatically.
but i found out that this actually works (explicitly using ->value as a key)

        transitions:
          !php/enum App\Entity\Status::draft->value:
            from:
              - !php/enum App\Entity\Status::finished
              - !php/enum App\Entity\Status::cancelled
            to: !php/enum App\Entity\Status::draft
Answer selected by kor3k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.