each() public method

The anonymous function receives the position and the node wrapped in a Crawler instance as arguments. Example: $crawler->filter('h1')->each(function ($node, $i) { return $node->text(); });
public each ( Closure $closure ) : array
$closure Closure An anonymous function
return array An array of values returned by the anonymous function
 public function format(Crawler $crawler, array $config)
 {
     $locations = $config['locations'];
     $return = $crawler->each(function (Crawler $node, $i) use($locations) {
         // Symfony dom-crawler methods ignore text nodes, so we have to
         // revert to native DOMNode methods instead.
         $children = $node->getNode(0)->childNodes;
         $values = [];
         foreach ($children as $child) {
             // Skip non element or text nodes.
             if (!in_array($child->nodeType, [1, 3])) {
                 continue;
             }
             // Loop through locations but stop after the first truthy value.
             foreach ($locations as $location) {
                 if ('_text' === $location && ($value = trim($child->nodeValue))) {
                     $values[] = $value;
                     continue 2;
                 }
                 // DOMText does not have method hasAttribute.
                 if (1 === $child->nodeType && $child->hasAttribute($location) && ($value = trim($child->getAttribute($location)))) {
                     $values[] = $value;
                     continue 2;
                 }
             }
         }
         // Remove any lingering 'empty' elements.
         $value = implode(' ', array_filter(array_map('trim', $values)));
         if ($this->looksLikeGroupTitle($node)) {
             $value = '%%TITLE%%' . $value . '%%TITLE%%';
         }
         return $value;
     });
     return [array_shift($return)];
 }
 /**
  * @param Crawler $nodes
  */
 public function __construct(Crawler $nodes)
 {
     $this->nodes = [];
     $nodes->each(function ($node) {
         $this->nodes[] = $node;
     });
 }
 protected function getXml(Crawler $crawler)
 {
     $document = new \DOMDocument('1.0', 'utf-8');
     $crawler->each(function (\DOMElement $node) use($document) {
         $document->appendChild($document->importNode($node, true));
     });
     return html_entity_decode($document->saveXML());
 }
Esempio n. 4
0
 public function format(Crawler $crawler, array $config)
 {
     $locations = $config['locations'];
     $return = $crawler->each(function (Crawler $node, $i) use($locations) {
         $values = $node->extract($locations);
         /**
          * Remove falsey values and normalize result...
          *
          * If 1 == count($locations), $values will contain strings,
          * otherwise $values will contain arrays.
          */
         if (is_array($values[0])) {
             $values = array_filter($values[0]);
         }
         if ($this->looksLikeGroupTitle($node)) {
             $values = array_map(function ($value) {
                 return '%%TITLE%%' . $value . '%%TITLE%%';
             }, $values);
         }
         return array_shift($values);
     });
     return array_values(array_filter($return));
 }
Esempio n. 5
0
 /**
  * create card from microdata && save it in cardContainer
  * 
  * @param Crawler $scope
  * @param string $url
  */
 private function setCardFromMD(Crawler $scope, $url)
 {
     $scope->each(function (Crawler $node) use($url) {
         try {
             $type = $this->parser->getCardType($node);
             $card = $this->createCard($type);
         } catch (\RuntimeException $e) {
             $card = $this->createCard('Thing');
         }
         if ($node->attr('itemprop')) {
             $this->childArray[] = $card;
         }
         $card->child = count($node->filter('[itemscope]')) - 1;
         $card->url = $url;
         $this->parser->setCardProperties($node, $card);
         $this->saveCard($card);
     });
 }
Esempio n. 6
0
 private function getCasts(Crawler $links)
 {
     $staffs = [];
     $links->each(function (Crawler $link, $i) use(&$staffs) {
         if (!preg_match('/id=(\\d+)/', $link->attr('href'), $matches)) {
             return;
         }
         $staff = new Staffs();
         list($name, $aka) = CrawlDmmTask::parseNameAndAka(trim($link->text()));
         $staff->name = $name;
         $staff->aka = $aka ? implode(',', $aka) : null;
         $staff->id = $matches[1];
         $staffs[] = $staff;
     });
     return $staffs;
 }