Exemple #1
0
 function testToString()
 {
     $foo = \arc\lambda::prototype(['foofoo' => function () {
         return 'foofoo';
     }, '__toString' => function () {
         return 'foobar';
     }]);
     $tst = (string) $foo;
     $this->assertEquals('foobar', $tst);
 }
Exemple #2
0
function aggregator($client)
{
    $aggregator = \arc\lambda::prototype();
    $aggregator->renderItem = function ($item) {
        if ($item->enclosure['url']) {
            $img = h::a(['href' => $item->link->nodeValue], h::img(['src' => $item->enclosure['url'], 'alt' => '']));
        } else {
            $img = '';
        }
        $pubDate = h::time(relativeTime(strtotime(current($item->xpath('.//pubDate|.//dc:date')))));
        return h::article(['class' => 'white-panel'], $img, h::h2(h::a(['href' => $item->link->nodeValue], $item->title->nodeValue)), $pubDate, h::p(['class' => 'description'], h::raw(strip_tags($item->description->nodeValue))));
    };
    $aggregator->renderItems = function ($items) {
        $itemsString = '';
        if (count($items)) {
            foreach ($items as $item) {
                $itemsString .= $this->renderItem($item);
            }
        } else {
            $itemsString = h::div(['class' => 'warning'], 'No articles in this feed.');
        }
        return h::raw($itemsString);
    };
    $aggregator->renderFeeds = function ($feeds) {
        return implode(array_map(function ($feed, $index) {
            return h::div(['class' => 'feed'], h::h2($feed->channel->title->nodeValue), h::form(['class' => 'remove', 'method' => 'POST'], h::button(['name' => 'removeFeed', 'value' => $index], 'Remove')), h::a(['href' => $feed->channel->link->nodeValue], $feed->channel->link->nodeValue));
        }, $feeds, array_keys($feeds)));
    };
    $aggregator->getPubDate = function ($item) {
        return strtotime(current($item->xpath('.//pubDate|.//dc:date')));
    };
    $aggregator->getItems = function ($feeds) {
        $allitems = call_user_func_array('array_merge', array_map(function ($rss) {
            return array_map(function ($item) use($rss) {
                $item->appendChild($rss->channel->cloneNode());
                return $item;
            }, $rss->getElementsByTagName('item'));
        }, $feeds));
        usort($allitems, function ($a, $b) {
            return $this->getPubDate($a) < $this->getPubDate($b);
        });
        return $allitems;
    };
    $aggregator->getFeed = function ($url) use($client) {
        $feed = $client->get($url);
        try {
            $rss = \arc\xml::parse($feed);
        } catch (\Exception $e) {
            $rss = \arc\html::parse($feed);
        }
        $rss->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
        return $rss;
    };
    $aggregator->render = function ($feeds) {
        $feeds = array_map(function ($feed) {
            return $this->getFeed($feed);
        }, $feeds);
        $items = $this->getItems($feeds);
        return h::section(['class' => 'items'], h::raw($this->renderItems($items))) . h::section(['class' => 'feeds'], h::raw($this->renderFeeds($feeds)));
    };
    return $aggregator;
}