Skip to content

Commit c8dc0d0

Browse files
committed
New part 3
1 parent 7a444f6 commit c8dc0d0

File tree

10 files changed

+466
-1
lines changed

10 files changed

+466
-1
lines changed

src/Abstracts/Api.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@
22

33
namespace Swader\Diffbot\Abstracts;
44

5+
use Swader\Diffbot\Exceptions\DiffbotException;
6+
57
/**
68
* Class Api
79
* @package Swader\Diffbot\Abstracts
810
*/
9-
abstract class Api
11+
abstract class Api implements \Swader\Diffbot\Interfaces\Api
1012
{
1113
/** @var int Timeout value in ms - defaults to 30s if empty */
1214
private $timeout = 30000;
1315

1416
/** @var string The URL onto which to unleash the API in question */
1517
private $url;
1618

19+
/** @var string API URL to which to send the request */
20+
protected $apiUrl;
21+
22+
/** @var array Settings on which optional fields to fetch */
23+
protected $fieldSettings = [];
24+
1725
public function __construct($url)
1826
{
1927
if (!is_string($url)) {
@@ -60,4 +68,27 @@ public function setTimeout($timeout = null)
6068
$this->timeout = $timeout;
6169
return $this;
6270
}
71+
72+
public function __call($name, $arguments)
73+
{
74+
$prefix = substr(lcfirst($name), 0, 3);
75+
$field = lcfirst(substr($name, 3));
76+
77+
$fields = static::getOptionalFields();
78+
if (in_array($field, $fields)) {
79+
if ($prefix == 'get') {
80+
return (isset($this->fieldSettings[$field])) ? $this->fieldSettings[$field] : false;
81+
}
82+
83+
if ($prefix == 'set') {
84+
if (is_bool($arguments[0]) || $arguments[0] === null) {
85+
$this->fieldSettings[$field] = (bool)$arguments[0];
86+
return $this;
87+
}
88+
throw new \InvalidArgumentException('Only booleans and null are allowed as optional field flags!');
89+
}
90+
throw new \BadMethodCallException('Prefix "'.$prefix.'" not allowed.');
91+
}
92+
throw new \BadMethodCallException($name . ': such a field does not exist for this API class.');
93+
}
6394
}

src/Api/Analyze.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Api;
4+
5+
use Swader\Diffbot\Abstracts\Api;
6+
7+
class Analyze extends Api
8+
{
9+
/** @var string API URL to which to send the request */
10+
protected $apiUrl = 'http://api.diffbot.com/v3/analyze';
11+
12+
protected static $optionalFields = [
13+
'links',
14+
'meta',
15+
'querystring'
16+
];
17+
18+
public static function getOptionalFields()
19+
{
20+
return self::$optionalFields;
21+
}
22+
23+
public function call()
24+
{
25+
26+
}
27+
}

src/Api/Article.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Api;
4+
5+
use Swader\Diffbot\Abstracts\Api;
6+
7+
class Article extends Api
8+
{
9+
/** @var string API URL to which to send the request */
10+
protected $apiUrl = 'http://api.diffbot.com/v3/article';
11+
12+
protected static $optionalFields = [
13+
'links',
14+
'meta',
15+
'querystring'
16+
];
17+
18+
public static function getOptionalFields()
19+
{
20+
return self::$optionalFields;
21+
}
22+
23+
public function setImagesHeight($bool = null)
24+
{
25+
$this->fieldSettings['images(height)'] = ($bool) ? (bool)$bool : false;
26+
return $this;
27+
}
28+
29+
public function getImagesHeight()
30+
{
31+
return (isset($this->fieldSettings['images(height)'])) ? $this->fieldSettings['images(height)'] : false;
32+
}
33+
34+
public function setImagesWidth($bool = null)
35+
{
36+
$this->fieldSettings['images(width)'] = ($bool) ? (bool)$bool : false;
37+
return $this;
38+
}
39+
40+
public function getImagesWidth()
41+
{
42+
return (isset($this->fieldSettings['images(width)'])) ? $this->fieldSettings['images(width)'] : false;
43+
}
44+
45+
public function setVideosNaturalHeight($bool = null)
46+
{
47+
$this->fieldSettings['videos(naturalHeight)'] = ($bool) ? (bool)$bool : false;
48+
return $this;
49+
}
50+
51+
public function getVideosNaturalHeight()
52+
{
53+
return (isset($this->fieldSettings['videos(naturalHeight)']))
54+
? $this->fieldSettings['videos(naturalHeight)'] : false;
55+
}
56+
57+
public function setVideosNaturalWidth($bool = null)
58+
{
59+
$this->fieldSettings['videos(naturalWidth)'] = ($bool) ? (bool)$bool : false;
60+
return $this;
61+
}
62+
63+
public function getVideosNaturalWidth()
64+
{
65+
return (isset($this->fieldSettings['videos(naturalWidth)']))
66+
? $this->fieldSettings['videos(naturalWidth)'] : false;
67+
}
68+
69+
public function call()
70+
{
71+
72+
}
73+
}

src/Api/Image.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Api;
4+
5+
use Swader\Diffbot\Abstracts\Api;
6+
7+
class Image extends Api
8+
{
9+
/** @var string API URL to which to send the request */
10+
protected $apiUrl = 'http://api.diffbot.com/v3/image';
11+
12+
protected static $optionalFields = [
13+
'height',
14+
'width',
15+
'links',
16+
'meta',
17+
'querystring',
18+
'breadcrumb',
19+
20+
'mentions',
21+
'ocr',
22+
'faces'
23+
];
24+
25+
public static function getOptionalFields()
26+
{
27+
return self::$optionalFields;
28+
}
29+
30+
public function call()
31+
{
32+
33+
}
34+
}

src/Api/Product.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Api;
4+
5+
use GuzzleHttp\Client;
6+
use Swader\Diffbot\Abstracts\Api;
7+
8+
class Product extends Api
9+
{
10+
/** @var string API URL to which to send the request */
11+
protected $apiUrl = 'http://api.diffbot.com/v3/product';
12+
13+
protected static $optionalFields = [
14+
'sku',
15+
'mpn',
16+
'shippingAmount',
17+
'saveAmount',
18+
'saveAmountDetails',
19+
'offerPriceDetails',
20+
'regularPriceDetails',
21+
'prefixCode',
22+
'productOrigin',
23+
'links',
24+
'meta',
25+
'querystring',
26+
'breadcrumb',
27+
'availability',
28+
'colors',
29+
'size'
30+
];
31+
32+
public static function getOptionalFields()
33+
{
34+
return self::$optionalFields;
35+
}
36+
37+
public function call()
38+
{
39+
}
40+
}

src/Diffbot.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace Swader\Diffbot;
44

55
use Swader\Diffbot\Exceptions\DiffbotException;
6+
use Swader\Diffbot\Api\Product;
7+
use Swader\Diffbot\Api\Image;
8+
use Swader\Diffbot\Api\Analyze;
9+
use Swader\Diffbot\Api\Article;
610

711
/**
812
* Class Diffbot
@@ -58,4 +62,58 @@ private static function validateToken($token)
5862
}
5963
return true;
6064
}
65+
66+
/**
67+
* Returns the token that has been defined.
68+
* @return null|string
69+
*/
70+
public function getToken()
71+
{
72+
return ($this->instanceToken) ? $this->instanceToken : self::$token;
73+
}
74+
75+
/**
76+
* Creates a Product API interface
77+
*
78+
* @param $url string Url to analyze
79+
* @return Product
80+
*/
81+
public function createProductAPI($url)
82+
{
83+
return new Product($url);
84+
}
85+
86+
/**
87+
* Creates an Article API interface
88+
*
89+
* @param $url string Url to analyze
90+
* @return Product
91+
*/
92+
public function createArticleAPI($url)
93+
{
94+
return new Article($url);
95+
}
96+
97+
/**
98+
* Creates an Image API interface
99+
*
100+
* @param $url string Url to analyze
101+
* @return Product
102+
*/
103+
public function createImageAPI($url)
104+
{
105+
return new Image($url);
106+
}
107+
108+
/**
109+
* Creates an Analyze API interface
110+
*
111+
* @param $url string Url to analyze
112+
* @return Product
113+
*/
114+
public function createAnalyzeAPI($url)
115+
{
116+
return new Analyze($url);
117+
}
118+
61119
}

src/Interfaces/Api.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Interfaces;
4+
5+
interface Api
6+
{
7+
public static function getOptionalFields();
8+
9+
public function setTimeout($timeout = null);
10+
11+
public function call();
12+
}

0 commit comments

Comments
 (0)