Skip to content

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Aug 24, 2016

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

Example (for @nicolas-grekas :trollface: )

<?php

require __DIR__.'/vendor/autoload.php';

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\Node\NameNode;

$el = new ExpressionLanguage();

$expression = $el->parse('a + b + 12', ['a','b']);

function dump_with_highlight($node)
{
    $dump = '';

    foreach ($node->toArray() as $v) {
        if (is_scalar($v)) {
            $dump .= $v;
        } elseif ($v instanceof NameNode) {
            $dump .= sprintf('`%s`', dump_with_highlight($v));
        } else {
            $dump .= dump_with_highlight($v);
        }
    }

    return $dump;
}

echo dump_with_highlight($expression->getNodes());

Result:

((<strong>a</strong> + <strong>b</strong>) + 12)

@nicolas-grekas
Copy link
Member

Thank you @lyrixx.

@nicolas-grekas nicolas-grekas merged commit b6d0050 into symfony:master Aug 24, 2016
nicolas-grekas added a commit that referenced this pull request Aug 24, 2016
…classe to ease its usage (lyrixx)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[ExpressionLanguage] Move the ::dump method to the Node classe to ease its usage

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

Example (for @nicolas-grekas :trollface: )

```php
<?php

require __DIR__.'/vendor/autoload.php';

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\Node\NameNode;

$el = new ExpressionLanguage();

$expression = $el->parse('a + b + 12', ['a','b']);

function dump_with_highlight($node)
{
    $dump = '';

    foreach ($node->toArray() as $v) {
        if (is_scalar($v)) {
            $dump .= $v;
        } elseif ($v instanceof NameNode) {
            $dump .= sprintf('`%s`', dump_with_highlight($v));
        } else {
            $dump .= dump_with_highlight($v);
        }
    }

    return $dump;
}

echo dump_with_highlight($expression->getNodes());
```
Result:

```
((<strong>a</strong> + <strong>b</strong>) + 12)
```

Commits
-------

b6d0050 [ExpressionLanguage] Move the ::dump method to the Node classe to ease its usage
@nicolas-grekas nicolas-grekas deleted the el-dump-with-ease branch August 24, 2016 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants