Skip to content

Commit 6ace833

Browse files
committed
Improved NewsParser api, now to use getPlatform()
1 parent e513a79 commit 6ace833

7 files changed

Lines changed: 107 additions & 102 deletions

File tree

example/example.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
require_once('../vendor/autoload.php');
44

55
# 1. Initialize the parser
6-
$parser = new \Sprain\NewsParser\Parser\Parser();
6+
$parser = new \Sprain\NewsParser\NewsParser();
77

88
# 2. Optional - set the cache folder
9-
$parser->getCache()->setCacheDir('./cache');
9+
$parser->setCacheDir('./cache');
1010

1111
# 3. Define your platform. The key is country - platform name
12-
$parser->setPlatform('ch-nzz');
12+
$nzz = $parser->getPlatform('ch-nzz');
1313

1414
# 4. Display the platform's favicon
15-
print '<img src="'.$parser->getIconUrl().'">';
15+
print '<img src="'.$nzz->getIconUrl().'">';
1616

1717
# 5. Get articles
18-
var_dump($parser->getRecommendedArticles(5));
18+
var_dump($nzz->getRecommendedArticles(5));
1919

2020
# Options: Clear the cache
2121
# $parser->getCache()->clear();

lib/NewsParser/NewsParser.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Sprain\NewsParser;
4+
5+
use Sprain\NewsParser\Cache\Cache;
6+
7+
class NewsParser
8+
{
9+
protected $cache;
10+
11+
public function getPlatform($platformKey)
12+
{
13+
$platformNameParts = explode('-', $platformKey);
14+
$platformClassName = __NAMESPACE__ . '\\Parser\\Platforms\\'. strtoupper($platformNameParts[0]) . '\\'. ucfirst($platformNameParts[1]) . 'Parser';
15+
16+
$platform = new $platformClassName();
17+
$platform->setCache($this->getCache());
18+
19+
return $platform;
20+
}
21+
22+
public function setCacheDir($cacheDir)
23+
{
24+
$this->getCache()->setCacheDir($cacheDir);
25+
26+
return $this;
27+
}
28+
29+
public function getCache()
30+
{
31+
if (null === $this->cache) {
32+
$this->cache = new Cache();
33+
}
34+
35+
return $this->cache;
36+
}
37+
}

lib/NewsParser/Parser/Interfaces/PlatformParserInterface.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/NewsParser/Parser/Parser.php

Lines changed: 0 additions & 81 deletions
This file was deleted.

lib/NewsParser/Parser/Platforms/CH/NzzParser.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
namespace Sprain\NewsParser\Parser\Platforms\CH;
44

5-
use Sprain\NewsParser\Parser\Interfaces\PlatformParserInterface;
65
use Sprain\NewsParser\Parser\Parser;
76
use Sprain\NewsParser\Parser\Platforms\PlatformParser;
87
use Sunra\PhpSimple\HtmlDomParser;
98

10-
class NzzParser extends PlatformParser implements PlatformParserInterface
9+
class NzzParser extends PlatformParser
1110
{
1211
protected $rootUrl = 'http://www.nzz.ch';
1312

lib/NewsParser/Parser/Platforms/CH/TagesanzeigerParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace Sprain\NewsParser\Parser\Platforms\CH;
44

55
use Sprain\NewsParser\Parser\Exceptions\NotImplementedException;
6-
use Sprain\NewsParser\Parser\Interfaces\PlatformParserInterface;
76
use Sprain\NewsParser\Parser\Parser;
7+
use Sprain\NewsParser\Parser\Platforms\PlatformParser;
88
use Sunra\PhpSimple\HtmlDomParser;
99

10-
class TagesanzeigerParser extends PlatformParser implements PlatformParserInterface
10+
class TagesanzeigerParser extends PlatformParser
1111
{
1212
protected $rootUrl = 'http://www.tagesanzeiger.ch';
1313

lib/NewsParser/Parser/Platforms/PlatformParser.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,72 @@
22

33
namespace Sprain\NewsParser\Parser\Platforms;
44

5+
use Sprain\NewsParser\Cache\Cache;
6+
57
abstract class PlatformParser
68
{
9+
protected $cache;
10+
protected $rootUrl;
11+
12+
abstract function doGetMostReadArticles($limit = null);
13+
14+
abstract function doGetRecommendedArticles($limit = null);
15+
16+
abstract function doGetMostCommentedArticles($limit = null);
17+
18+
public function getMostReadArticles($limit = null)
19+
{
20+
$cacheId = get_called_class().':'.__METHOD__;
21+
22+
if (!$articles = $this->getCache()->fetch($cacheId)) {
23+
$articles = $this->doGetMostReadArticles($limit);
24+
$this->getCache()->save($cacheId, $articles);
25+
}
26+
27+
return $articles;
28+
}
29+
30+
public function getRecommendedArticles($limit = null)
31+
{
32+
$cacheId = get_called_class().':'.__METHOD__;
33+
34+
if (!$articles = $this->getCache()->fetch($cacheId)) {
35+
$articles = $this->doGetRecommendedArticles($limit);
36+
$this->getCache()->save($cacheId, $articles);
37+
}
38+
39+
return $articles;
40+
}
41+
42+
public function getMostCommentedArticles($limit = null)
43+
{
44+
$cacheId = get_called_class().':'.__METHOD__;
45+
46+
if (!$articles = $this->getCache()->fetch($cacheId)) {
47+
$articles = $this->doGetMostCommentedArticles($limit);
48+
$this->getCache()->save($cacheId, $articles);
49+
}
50+
51+
return $articles;
52+
}
53+
54+
public function getIconUrl()
55+
{
56+
return 'http://www.google.com/s2/favicons?domain=' . $this->getRootUrl();
57+
}
58+
759
public function getRootUrl()
860
{
961
return $this->rootUrl;
1062
}
63+
64+
public function setCache(Cache $cache)
65+
{
66+
$this->cache = $cache;
67+
}
68+
69+
public function getCache()
70+
{
71+
return $this->cache;
72+
}
1173
}

0 commit comments

Comments
 (0)