Skip to content

[DomCrawler] Added return of element name in extract() method #29127

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

Merged

Conversation

andrey-helldar
Copy link

@andrey-helldar andrey-helldar commented Nov 7, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
License MIT
Doc PR symfony/symfony-docs#10646

Situation: you need to get an array of keys and values.
The current package code does not allow this to be done easily.
The changes made to the code will allow you to return the required data set.

use Symfony\Component\DomCrawler\Crawler;

$crawler = new Crawler($content);

$crawler
    ->filter('ItemsList > Item')
    ->each(function (Crawler $element) {
        $data = $element
            ->children()
            ->extract(['_name', '_text']);

        var_dump($data);
    });

// Result:
array:2 [
  0 => array:2 [
    0 => "id",
    1 => "1"
  ],
  1 => array:2 [
    0 => "title",
    1 => "Foo Bar"
  ]
]

andrey-helldar pushed a commit to andrey-helldar/symfony-docs that referenced this pull request Nov 7, 2018
@andrey-helldar andrey-helldar changed the title Added return of element name in extract() method [DomCrawler] Added return of element name in extract() method Nov 7, 2018
@jakzal
Copy link
Contributor

jakzal commented Nov 7, 2018

_key isn't accurate. It's a node name, so maybe _name would be more suitable?

@andrey-helldar
Copy link
Author

The hardest thing in development is to come up with a variable name. :)

I considered different names: _name, _key, _tag... and settled on two: _key and _name,

I think you're right. _name more appropriate name.

@nicolas-grekas
Copy link
Member

Thank you @andrey-helldar.

@nicolas-grekas nicolas-grekas merged commit 79162c1 into symfony:master Dec 1, 2018
nicolas-grekas added a commit that referenced this pull request Dec 1, 2018
…)` method (andrey-helldar)

This PR was squashed before being merged into the 4.3-dev branch (closes #29127).

Discussion
----------

[DomCrawler] Added return of element name in `extract()` method

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#10646

Situation: you need to get an array of keys and values.
The current package code does not allow this to be done easily.
The changes made to the code will allow you to return the required data set.
```php
use Symfony\Component\DomCrawler\Crawler;

$crawler = new Crawler($content);

$crawler
    ->filter('ItemsList > Item')
    ->each(function (Crawler $element) {
        $data = $element
            ->children()
            ->extract(['_name', '_text']);

        var_dump($data);
    });

// Result:
array:2 [
  0 => array:2 [
    0 => "id",
    1 => "1"
  ],
  1 => array:2 [
    0 => "title",
    1 => "Foo Bar"
  ]
]
```

Commits
-------

79162c1 [DomCrawler] Added return of element name in `extract()` method
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Dec 3, 2018
… method (andrey-helldar)

This PR was squashed before being merged into the master branch (closes #10646).

Discussion
----------

[DomCrawler] Added return of element name in `extract()` method

See a PR: symfony/symfony#29127

<!--

If your pull request fixes a BUG, use the oldest maintained branch that contains
the bug (see https://symfony.com/roadmap for the list of maintained branches).

If your pull request documents a NEW FEATURE, use the same Symfony branch where
the feature was introduced (and `master` for features of unreleased versions).

-->

Commits
-------

b89cd59 [DomCrawler] Added return of element name in `extract()` method
@amouhzi
Copy link
Contributor

amouhzi commented Jan 19, 2019

Hello,

And if an attribute with the name _text exists? What's going to happen ?

@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
@fabpot fabpot mentioned this pull request May 9, 2019
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.

7 participants