Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Config] Transform a value into an array #21817

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

Closed
javiereguiluz opened this issue Mar 1, 2017 · 6 comments
Closed

[Config] Transform a value into an array #21817

javiereguiluz opened this issue Mar 1, 2017 · 6 comments

Comments

@javiereguiluz
Copy link
Member

javiereguiluz commented Mar 1, 2017

Q A
Bug report? no
Feature request? yes
BC Break report? no
Symfony version 3.x

In config trees is common to see code like the following to turn a value into an array:

->beforeNormalization()
   ->ifTrue(function ($v) { return !is_array($v); })
    ->then(function ($v) { return array($v); })
->end()

// ...

->beforeNormalization()->ifString()->then(function ($v) { return array($v); })->end()

You can see it tens of times in Symfony, popular bundles and elsewhere. Could we add a helper method to remove this boilerplate code? Thanks!

@ro0NL
Copy link
Contributor

ro0NL commented Mar 1, 2017

See also #20223

Some generic if/then's for casting purposes would be really nice.

@javiereguiluz
Copy link
Member Author

@ro0NL I partially agree with you. It's OK to add more if/then helpers ... but only if they are used enough. In the case of the string/number -> array conversion, it's a very common need (even in the Symfony code itself).

@ro0NL
Copy link
Contributor

ro0NL commented Mar 2, 2017

I'd vote for a generic solution, covering all types;

if()->is('string')->then()->castTo('array')
if()->is('numeric')->then()->castTo('int')

Something like that.

@fabpot
Copy link
Member

fabpot commented Mar 2, 2017

I don't like the generic solution you suggest @ro0NL as it has many drawbacks. The main one being discoverability and autocompletion (think IDEs).

As @javiereguiluz suggested, we only need some of them, the most used ones. Let's do that.

@ro0NL
Copy link
Contributor

ro0NL commented Mar 2, 2017

Agree. My proposed syntax may not be suitable; i actually meant #20223 (comment) and indeed for common types conversions only.

@ro0NL
Copy link
Contributor

ro0NL commented Mar 3, 2017

Rethinking the numeric to int conversion from #20223, it looks convenient but im not sure it's good practice. Ie. TTL values and such are probably best defined as an integer node anyway (being strict).. @chalasr wdyt?

Not saying a generic conversion api doesnt workout, but it's most likely overkill.

What about tackling both usecases pointed out by @javiereguiluz with a nice one-liner..

->beforeNormalization()
   ->castToArray()
   ->ifTrue($something)
   ->then($profit)
->end()

castToArray wraps the ifPart closure.

Any thoughts?

fabpot added a commit that referenced this issue Mar 6, 2017
This PR was squashed before being merged into the 3.3-dev branch (closes #21893).

Discussion
----------

Added a castToArray() config helper

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21817
| License       | MIT
| Doc PR        | -

Commits
-------

3a589df Added a castToArray() config helper
@fabpot fabpot closed this as completed Mar 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants