phpnelmio-alicealice-fixtures

Using alice fixtures function to calculate values


I'm using alice fixtures https://github.com/nelmio/alice and I'm curious whether it's possible to for example multiply <current()>. Let's have this code:

entity{1..5}:
    value: <current()> * 100

You get the idea. It would create 5 entities first with value 100, second value 200 etc. Is there a way to do it?


Solution

  • Not possible, and not likely to be added natively to the bundle!

    However, it's not a stretch to create a custom provider, as per the suggestion of theofidry, a contributor to the bundle.

    theofidry:

    The problem with what you would like to do is that the string <current()> * @offer_price_<current()>value must, besides being parsed and the value resolved, be evaluated. That's not something that will be added. If you want to do this kind of thing, you should add a custom provider:

    total_item_price_{1..4}:
       value: <calculateItemValue(<current()>, @offer_price_<current()>)>
    

    Source: https://github.com/nelmio/alice/issues/295#issuecomment-217748524