PHP code example of benkle / feeding

1. Go to this page and download the library: Download benkle/feeding library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

benkle / feeding example snippets


$reader = new \Benkle\Feeding\Reader();
$feed = $reader->read('http://xkcd.com/atom.xml');

echo $feed->getTitle() . PHP_EOL;
foreach ($feed->getItems() as $item) {
    echo "\t" . $item->getTitle() . PHP_EOL;
}

class MyRule implements \Benkle\Feeding\Interfaces\RuleInterface {
    public function canHandle(\DOMNode $node, \Benkle\Feeding\Interfaces\NodeInterface $target)
    {
        return $node->nodeName == 'title' && $target instanceof \Benkle\Feeding\Interfaces\ChannelInterface;
    }

    public function handle(\Benkle\Feeding\Parser $parser, \DOMNode $node, \Benkle\Feeding\Interfaces\NodeInterface $target)
    {
        /** \Benkle\Feeding\Interfaces\ChannelInterface $target */
        $target->setTitle($node->nodeValue);
    }
}

/** @var \Benkle\Feeding\Interfaces\StandardInterface $standard */
foreach ($reader->getStandards() as $standard) {
    $standard->getRules()->add(new MyRule(), 5); // Rules are ordered by priority
}

class MyStandard implements \Benkle\Feeding\Interfaces\StandardInterface {
    use \Benkle\Feeding\Traits\WithParserTrait;
    use \Benkle\Feeding\Traits\WithRuleSetTrait;

    public function __construct()
    {
        $this->getRules()->add(new MyRule(), 5);
    }

    public function newFeed()
    {
        return new \Benkle\Feeding\Feed();
    }

    public function getRootNode(\DOMDocument $dom)
    {
        return $dom->getElementsByTagName('feed')->item(0);
    }


    public function canHandle(\DOMDocument $dom)
    {
        return $dom->getElementsByTagName('feed')->length > 0;
    }
}

$reader->getStandards()->add(new MyStandard(), 5);

$reader->setDomParser(new class implements \Benkle\Feeding\Interfaces\DOMParserInterface {
    public function parse($source)
    {
        $parser = new \MyFancy\DomParser();
        return $parser->parse($source);
    }
});

$reader->setDomParser(
    new \Benkle\Feeding\DOMParsers\FallbackStackParser(
        new \Benkle\Feeding\DOMParsers\PHPDOMParser(),
        new \Benkle\Feeding\DOMParsers\MastermindsHTML5Parser()
    )
);

$reader->setFileAccess(new class implements \Benkle\Feeding\Interfaces\FileAccessInterface {
    private $myFs;

    public function __construct()
    {
        $this->myFs = new MyFs();
    }

    public function exists($filename)
    {
        return $this->myFs->exists($filename);
    }

    public function get($filename)
    {
        return $this->myFs->open($filename);
    }
});

$reader = new \Benkle\Feeding\BareReader();
$reader->setDomParser(new \Benkle\Feeding\DOMParsers\PHPDOMParser());
$reader->getStandards()->add(new \Benkle\Feeding\Standards\RSS\RSS20Standard());