src/Extractor/ContentExtractor.php
private function extractEntityFromPattern($entity, $pattern, $returnCallback = null)
    {
        // we define the default callback here
        if (!\is_callable($returnCallback)) {
            $returnCallback = function ($e) {
                return trim($e);
            };
        }

        $elems = $this->xpath->evaluate($pattern, $this->readability->dom);
        $entityValue = null;

        if (\is_string($elems) && '' !== trim($elems)) {
            $entityValue = $returnCallback($elems);

            $this->logger->info("{$entity} expression evaluated as string: {{$entity}}", [$entity => $entityValue]);
            $this->logger->info('...XPath match: {pattern}', ['pattern', $pattern]);
        } elseif ($elems instanceof \DOMNodeList && $elems->length > 0) {
            $entityValue = $returnCallback($elems->item(0)->textContent);

            $this->logger->info("{$entity} matched: {{$entity}}", [$entity => $entityValue]);
            $this->logger->info('...XPath match: {pattern}', ['pattern', $pattern]);

            // remove entity from document
            try {
                $elems->item(0)->parentNode->removeChild($elems->item(0));
            } catch (\DOMException $e) {
                // do nothing
            }
        }

        if (null !== $entityValue) {
            $this->{$entity} = $entityValue;

            return true;
        }

        return false;
    }
private function extractEntityFromPattern($entity, $pattern, $returnCallback = null)
    {
        // we define the default callback here
        if (!\is_callable($returnCallback)) {
            $returnCallback = function ($e) {
                return trim($e);
            };
        }

        $elems = $this->xpath->evaluate($pattern, $this->readability->dom);
        $entityValue = null;

        if (\is_string($elems) && '' !== trim($elems)) {
            $entityValue = $returnCallback($elems);

            $this->logger->info("{$entity} expression evaluated as string: {{$entity}}", [$entity => $entityValue]);
            $this->logger->info('...XPath match: {pattern}', ['pattern', $pattern]);
        } elseif ($elems instanceof \DOMNodeList && $elems->length > 0) {
            $entityValue = $returnCallback($elems->item(0)->textContent);

            $this->logger->info("{$entity} matched: {{$entity}}", [$entity => $entityValue]);
            $this->logger->info('...XPath match: {pattern}', ['pattern', $pattern]);

            // remove entity from document
            try {
                $elems->item(0)->parentNode->removeChild($elems->item(0));
            } catch (\DOMException $e) {
                // do nothing
            }
        }

        if (null !== $entityValue) {
            $this->{$entity} = $entityValue;

            return true;
        }

        return false;
    }
private function extractEntityFromPattern ( $ entity , $ pattern , $ returnCallback = null ) { // we define the default callback here if ( ! \ is_callable ( $ returnCallback ) ) { $ returnCallback = function ( $ e ) { return trim ( $ e ) ; } ; } $ elems = $ this -> xpath -> evaluate ( $ pattern , $ this -> readability -> dom ) ; $ entityValue = null ; if ( \ is_string ( $ elems ) && '' !== trim ( $ elems ) ) { $ entityValue = $ returnCallback ( $ elems ) ; $ this -> logger -> info ( "{$entity} expression evaluated as string: {{$entity}}" , [ $ entity => $ entityValue ] ) ; $ this -> logger -> info ( '...XPath match: {pattern}' , [ 'pattern' , $ pattern ] ) ; } elseif ( $ elems instanceof \ DOMNodeList && $ elems -> length > 0 ) { $ entityValue = $ returnCallback ( $ elems -> item ( 0 ) -> textContent ) ; $ this -> logger -> info ( "{$entity} matched: {{$entity}}" , [ $ entity => $ entityValue ] ) ; $ this -> logger -> info ( '...XPath match: {pattern}' , [ 'pattern' , $ pattern ] ) ; // remove entity from document try { $ elems -> item ( 0 ) -> parentNode -> removeChild ( $ elems -> item ( 0 ) ) ; } catch ( \ DOMException $ e ) { // do nothing } } if ( null !== $ entityValue ) { $ this -> { $ entity } = $ entityValue ; return true ; } return false ; }
Extract and apply a callback to an entity according to a pattern.

The $entity argument is used as the name of the property to set in
the current ContentExtractor instance (variable reference) and as
the name to use in log messages

Example: extractEntityFromPattern('title', $pattern) will search
for pattern and set the found value in $this->title

@param string   $entity         Entity to look for ('title', 'date')
@param string   $pattern        Pattern to look for
@param callable $returnCallback Function to apply on the value

@return bool Telling if the entity has been found
Extract and apply a callback to an entity according to a pattern .