/** * Discover a list of possible xpaths that are collection items * in sorted order where the first element is the most likely. * @return array[string] */ public function discoverScores(Nodes $nodes) { $nonContentNodes = $nodes->find('//*[not(text())]'); $maxSibs = (new Utils())->getMaxSibCount($nodes->getDocument(), $nonContentNodes); arsort($maxSibs); $ancestorCountGrouping = []; foreach ($maxSibs as $path => $count) { if ($count < 2) { continue; } $ancestorCount = substr_count($path, '/'); if (!isset($ancestorCountGrouping[$ancestorCount])) { $ancestorCountGrouping[$ancestorCount] = [$path => $count]; } else { $ancestorCountGrouping[$ancestorCount][$path] = $count; } } ksort($ancestorCountGrouping); $result = []; foreach ($ancestorCountGrouping as $_ => $collectionScores) { foreach ($collectionScores as $path => $score) { $result[$path] = $score; } } return $result; }
/** * Fetch the nodes for the provided context node. If $context * ist NULL the document context is used. * * @throws \InvalidArgumentException * @param string $expression * @param \DOMNode $context * @param int $options * @return array|bool|\DOMNodeList|float|string */ private function fetchNodes($expression, \DOMNode $context = NULL, $options = 0) { $nodes = $this->_nodes->xpath($expression, $context); if (!$nodes instanceof \DOMNodeList) { throw new \InvalidArgumentException('Given selector/expression did not return a node list.'); } $nodes = iterator_to_array($nodes); if (Constraints::hasOption($options, self::REVERSE)) { return array_reverse($nodes, FALSE); } return $nodes; }
public function groupByCallable(Nodes $nodes, callable $fn) { $grouping = []; foreach ($nodes as $node) { $key = $fn($node); if (!isset($grouping[$key])) { $grouping[$key] = []; } $grouping[$key][] = $node; } foreach ($grouping as $key => &$nodeList) { $nodeList = $nodes->spawn($nodeList); } return $grouping; }
/** * Throws an exception if somebody tries to unset one * of the dynamic properties * * @param string $name * @throws \BadMethodCallException */ public function __unset($name) { switch ($name) { case 'attr': case 'css': case 'data': throw new \BadMethodCallException(sprintf('Can not unset property %s::$%s', get_class($this), $name)); } parent::__unset($name); // @codeCoverageIgnoreStart }