kaja47 / Matcher

Powerful tool for extracting data from XML and HTML using XPath and pure magic
https://funkcionalne.k47.cz/2014/05/php-dom-simplexml-a-matcher.html
Other
92 stars 7 forks source link

In :: multi cannot be used number as index in array #10

Open pilec opened 9 years ago

pilec commented 9 years ago

this code run properly:

$m = Matcher::multi('//div[@id="siteTable"]/div[contains(@class, "thing")]', [
            'id' => '@data-fullname', //string as index
        ])->fromHtml();

        $f = file_get_contents('http://www.reddit.com/');

but if i try make these little change:

$m = Matcher::multi('//div[@id="siteTable"]/div[contains(@class, "thing")]', [
            1 => '@data-fullname', //number as index
        ])->fromHtml();

        $f = file_get_contents('http://www.reddit.com/');

        $extractedData = $m($f);

I`ve got error: Warning array_merge(): Argument #2 is not an array File: .../vendor/atrox/matcher/Matcher.php:281

kaja47 commented 9 years ago

This is expected behavior. Numeric keys are treated differently, they cause their value to be merged one level higher.

Matcher::multi(path, [
  'a' => pathToA,
  'b' => pathToB,
  'c' => pathToC,
  'd' => pathToD
]);

is the same as

Matcher::multi(path, [
  'a' => pathToA,
  [                // implicit numeric key
    'b' => pathToB,
    'c' => pathToC
  ],
  'd' => pathToD
]);

It's used for matching paths with common prefixes or matching with custom functions.

Matcher::multi(path, [
  'id' => pathToId,
  Matcher::single('reallyLongCommonPathPrefix', [ // field title and date aren't nested
    'title' => pathToTitle,
    'date' => pathToDate
  ]),
]);
Matcher::multi(path, [
  'id' => pathToId,
  function ($node) { // field title and date aren't nested
    return [
      'title' => f($node),
      'date' => g($node)
    ];
  }
]);

But it's for sure, it can give better error message.