Skip to content
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

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

Merged

Conversation

Projects
None yet
7 participants
@andrey-helldar
Copy link
Contributor

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 added 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

This comment has been minimized.

Copy link
Member

commented Nov 7, 2018

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

@andrey-helldar

This comment has been minimized.

Copy link
Contributor Author

commented Nov 7, 2018

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.

@ro0NL

ro0NL approved these changes Nov 8, 2018

Show resolved Hide resolved src/Symfony/Component/DomCrawler/CHANGELOG.md Outdated

@nicolas-grekas nicolas-grekas added this to the next milestone Nov 8, 2018

@fabpot

fabpot approved these changes Nov 12, 2018

@nicolas-grekas nicolas-grekas force-pushed the andrey-helldar:andrey-helldar-patch-1 branch from 74afc0c to 79162c1 Dec 1, 2018

@nicolas-grekas

This comment has been minimized.

Copy link
Member

commented Dec 1, 2018

Thank you @andrey-helldar.

@nicolas-grekas nicolas-grekas merged commit 79162c1 into symfony:master Dec 1, 2018

1 of 3 checks passed

continuous-integration/appveyor/pr Waiting for AppVeyor build to complete
Details
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
fabbot.io Your code looks good.
Details

nicolas-grekas added a commit that referenced this pull request Dec 1, 2018

feature #29127 [DomCrawler] Added return of element name in `extract(…
…)` 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

minor #10646 [DomCrawler] Added return of element name in `extract()`…
… 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

This comment has been minimized.

Copy link
Contributor

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 referenced this pull request May 9, 2019

Merged

Release v4.3.0-BETA1 #31435

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.