summaryrefslogtreecommitdiffstats
path: root/vendor/fguillot/picofeed/lib/PicoFeed/Generator/YoutubeContentGenerator.php
blob: 198090d4ff933efecf41b157cd5dd23c250c38e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php

namespace PicoFeed\Generator;

use PicoFeed\Base;
use PicoFeed\Parser\Item;

/**
 * Youtube Content Generator
 *
 * @package PicoFeed\Generator
 * @author  Frederic Guillot
 */
class YoutubeContentGenerator extends Base implements ContentGeneratorInterface
{
    /**
     * Execute Content Generator
     *
     * @access public
     * @param  Item $item
     * @return boolean
     */
    public function execute(Item $item)
    {
        if ($item->hasNamespace('yt')) {
            return $this->generateHtmlFromXml($item);
        }

        return $this->generateHtmlFromUrl($item);
    }

    /**
     * Generate HTML
     *
     * @access public
     * @param  Item $item
     * @return boolean
     */
    private function generateHtmlFromXml(Item $item)
    {
        $videoId = $item->getTag('yt:videoId');

        if (! empty($videoId)) {
            $item->setContent('<iframe width="560" height="315" src="//www.youtube.com/embed/'.$videoId[0].'" frameborder="0"></iframe>');
            return true;
        }

        return false;
    }

    /**
     * Generate HTML from item URL
     *
     * @access public
     * @param  Item $item
     * @return bool
     */
    public function generateHtmlFromUrl(Item $item)
    {
        if (preg_match('/youtube\.com\/watch\?v=(.*)/', $item->getUrl(), $matches)) {
            $item->setContent('<iframe width="560" height="315" src="//www.youtube.com/embed/'.$matches[1].'" frameborder="0"></iframe>');
            return true;
        }

        return false;
    }
}