Skip to content

Commit b611c7d

Browse files
committed
Merge pull request #36 from Swader/feature-carbondate
Fixes #34
2 parents bf35b33 + 7c0c182 commit b611c7d

File tree

6 files changed

+140
-67
lines changed

6 files changed

+140
-67
lines changed

CHANGELOG.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
#Changelog
22
All notable changes will be documented in this file
33

4+
## 1.2. - November 8th, 2015
5+
6+
- [Enhancement] Suggested Carbon as helper library
7+
- [Enhancement] Added Carbon methods to date returns from Article and Post entity
8+
49
## 1.1. - November 7th, 2015
510

11+
- [Enhancement] Optimized tests
12+
- [Enhancement] Added new Article Entity getters (Fixes #19)
13+
614
## 1.0.1 - November 1st, 2015
715

8-
- Minor fixes based on feedback from PHP-HTTP team
9-
- authorUrl getter added to Article entity
16+
- [Bug] Minor fixes based on feedback from PHP-HTTP team
17+
- [Bug] authorUrl getter added to Article entity, was missing before
1018

1119
## 1.0.0 - November 1st, 2015
1220

13-
- Converted to PHP-HTTP, removed hard dependency on Guzzle 5
14-
- Modified Travis / Scrutinizer test configuration
15-
- Converted mock JSON files into pure JSON, stripped away headers
21+
- [Feature] Converted to PHP-HTTP, removed hard dependency on Guzzle 5
22+
- [Enhancement] Modified Travis / Scrutinizer test configuration
23+
- [Enhancement] Converted mock JSON files into pure JSON, stripped away headers
1624

1725
## 0.4.4 - June 16th, 2015
1826

composer.json

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,53 @@
11
{
2-
"name": "swader/diffbot-php-client",
3-
"description": "A PHP wrapper for using Diffbot's API",
4-
"keywords": [
5-
"diffbot", "api", "wrapper", "client"
6-
],
7-
"homepage": "https://github.com/swader/diffbot-php-client",
8-
"license": "MIT",
9-
"authors": [
10-
{
11-
"name": "Bruno Skvorc",
12-
"email": "bruno@skvorc.me",
13-
"homepage": "http://bitfalls.com",
14-
"role": "Developer"
15-
}
16-
],
17-
"require": {
18-
"php" : ">=5.4.0",
19-
"php-http/client-implementation": "^1.0",
20-
"php-http/utils": "^0.1.0@dev",
21-
"php-http/discovery": "^0.2.0@dev"
22-
},
23-
"require-dev": {
24-
"symfony/var-dumper": "~2",
25-
"phpunit/phpunit": "^5.0",
26-
"php-http/guzzle6-adapter": "~0.2@dev",
27-
"scrutinizer/ocular": "^1.1"
28-
},
29-
"autoload": {
30-
"psr-4": {
31-
"Swader\\Diffbot\\": "src"
32-
}
33-
},
34-
"autoload-dev": {
35-
"psr-4": {
36-
"Swader\\Diffbot\\Test\\": "tests/"
37-
}
38-
},
39-
"extra": {
40-
"branch-alias": {
41-
"dev-master": "1.1-dev"
42-
}
43-
},
44-
"prefer-stable": true,
45-
"minimum-stability": "dev"
2+
"name": "swader/diffbot-php-client",
3+
"description": "A PHP wrapper for using Diffbot's API",
4+
"keywords": [
5+
"diffbot",
6+
"api",
7+
"wrapper",
8+
"client"
9+
],
10+
"homepage": "https://github.com/swader/diffbot-php-client",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Bruno Skvorc",
15+
"email": "bruno@skvorc.me",
16+
"homepage": "http://bitfalls.com",
17+
"role": "Developer"
18+
}
19+
],
20+
"require": {
21+
"php": ">=5.4.0",
22+
"php-http/client-implementation": "^1.0",
23+
"php-http/utils": "^0.1.0@dev",
24+
"php-http/discovery": "^0.2.0@dev"
25+
},
26+
"require-dev": {
27+
"symfony/var-dumper": "~2",
28+
"phpunit/phpunit": "^5.0",
29+
"php-http/guzzle6-adapter": "~0.2@dev",
30+
"scrutinizer/ocular": "^1.1",
31+
"nesbot/carbon": "^1.21"
32+
},
33+
"autoload": {
34+
"psr-4": {
35+
"Swader\\Diffbot\\": "src"
36+
}
37+
},
38+
"autoload-dev": {
39+
"psr-4": {
40+
"Swader\\Diffbot\\Test\\": "tests/"
41+
}
42+
},
43+
"extra": {
44+
"branch-alias": {
45+
"dev-master": "1.1-dev"
46+
}
47+
},
48+
"suggest": {
49+
"nesbot/carbon": "Turns the date and estimatedDate return values of Article and Post entity into Carbon entities."
50+
},
51+
"prefer-stable": true,
52+
"minimum-stability": "dev"
4653
}

src/Entity/Article.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class Article extends Entity
1414

1515
public function __construct(array $data)
1616
{
17+
if (class_exists('\Carbon\Carbon')) {
18+
$format = 'D, d M o H:i:s e';
19+
\Carbon\Carbon::setToStringFormat($format);
20+
}
21+
1722
parent::__construct($data);
1823
if (isset($this->data['discussion'])) {
1924
$this->discussion = new Discussion($this->data['discussion']);
@@ -51,13 +56,14 @@ public function getHtml()
5156
/**
5257
* Returns date as per http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
5358
* Example date: "Wed, 18 Dec 2013 00:00:00 GMT"
54-
* Note that this is "strtotime" friendly for further conversions
55-
* @todo add more formats as method arguments
56-
* @return string
59+
* This will be a Carbon (https://github.com/briannesbitt/Carbon) instance if Carbon is installed.
60+
* @return \Carbon\Carbon | string
5761
*/
5862
public function getDate()
5963
{
60-
return $this->data['date'];
64+
return (class_exists('\Carbon\Carbon')) ?
65+
new \Carbon\Carbon($this->data['date'], 'GMT') :
66+
$this->data['date'];
6167
}
6268

6369
/**
@@ -248,10 +254,18 @@ public function getPublisherRegion()
248254
* more specific timestamp using various factors. This will not be
249255
* generated for articles older than two days, or articles without an identified date.
250256
*
251-
* @return string | null
257+
* @see Article::getDate() - used when estimatedDate isn't defined
258+
*
259+
* This will be a Carbon (https://github.com/briannesbitt/Carbon) instance if Carbon is installed.
260+
*
261+
* @return \Carbon\Carbon | string
252262
*/
253263
public function getEstimatedDate()
254264
{
255-
return $this->getOrDefault('estimatedDate', $this->getDate());
265+
$date = $this->getOrDefault('estimatedDate', $this->getDate());
266+
267+
return (class_exists('\Carbon\Carbon')) ?
268+
new \Carbon\Carbon($date, 'GMT') :
269+
$date;
256270
}
257271
}

src/Entity/Post.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
class Post extends Entity
88
{
99

10+
public function __construct(array $data)
11+
{
12+
if (class_exists('\Carbon\Carbon')) {
13+
$format = 'D, d M o H:i:s e';
14+
\Carbon\Carbon::setToStringFormat($format);
15+
}
16+
17+
parent::__construct($data);
18+
}
19+
1020
/**
1121
* Should always return "post"
1222
* @return string
@@ -58,13 +68,15 @@ public function getHtml()
5868
/**
5969
* Returns date as per http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
6070
* Example date: "Wed, 18 Dec 2013 00:00:00 GMT"
61-
* Note that this is "strtotime" friendly for further conversions
62-
* @todo add more formats as method arguments
63-
* @return string
71+
* This will be a Carbon (https://github.com/briannesbitt/Carbon) instance if Carbon is installed.
72+
* @return \Carbon\Carbon | string
6473
*/
6574
public function getDate()
6675
{
67-
return $this->data['date'];
76+
77+
return (class_exists('\Carbon\Carbon')) ?
78+
new \Carbon\Carbon($this->data['date'], 'GMT') :
79+
$this->data['date'];
6880
}
6981

7082
/**

tests/Entity/ArticleTest.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,23 @@ public function dateProvider()
8585
return [
8686
[
8787
'Articles/diffbot-sitepoint-basic.json',
88-
"Sun, 27 Jul 2014 00:00:00 GMT"
88+
"Sun, 27 Jul 2014 00:00:00 GMT",
89+
2014
8990
],
9091
[
9192
'Articles/diffbot-sitepoint-extended.json',
92-
"Sun, 27 Jul 2014 00:00:00 GMT"
93+
"Sun, 27 Jul 2014 00:00:00 GMT",
94+
2014
9395
],
9496
[
9597
'Articles/apple-watch-verge-basic.json',
96-
"Wed, 08 Apr 2015 00:00:00 GMT"
98+
"Wed, 08 Apr 2015 00:00:00 GMT",
99+
2015
97100
],
98101
[
99102
'Articles/apple-watch-verge-extended.json',
100-
"Wed, 08 Apr 2015 00:00:00 GMT"
103+
"Wed, 08 Apr 2015 00:00:00 GMT",
104+
2015
101105
]
102106
];
103107
}
@@ -107,12 +111,15 @@ public function dateProvider()
107111
* @param $articles
108112
* @dataProvider dateProvider
109113
*/
110-
public function testDate($file, $articles)
114+
public function testDate($file, $articles, $year)
111115
{
112116
$articles = (is_array($articles)) ? $articles : [$articles];
113117
/** @var Article $entity */
114118
foreach ($this->ei($file) as $i => $entity) {
115119
$this->assertEquals($articles[$i], $entity->getDate());
120+
if (class_exists('\Carbon\Carbon')) {
121+
$this->assertEquals($year, $entity->getDate()->year);
122+
}
116123
}
117124
}
118125

@@ -351,7 +358,7 @@ public function testPublisherRegion($file, $value1)
351358
public function estimatedDateProvider()
352359
{
353360
return [
354-
['Articles/15-11-07/diffbot-sitepoint-basic.json', 'Sun, 27 Jul 2014 00:00:00 GMT'],
361+
['Articles/15-11-07/diffbot-sitepoint-basic.json', 'Sun, 27 Jul 2014 00:00:00 GMT', 2014],
355362
];
356363
}
357364

@@ -360,12 +367,15 @@ public function estimatedDateProvider()
360367
* @param $file
361368
* @param $value1
362369
*/
363-
public function testEstimatedDate($file, $value1)
370+
public function testEstimatedDate($file, $value1, $value2)
364371
{
365372
$value1 = (is_array($value1)) ? $value1 : [$value1];
366373
/** @var Article $entity */
367374
foreach ($this->ei($file) as $i => $entity) {
368375
$this->assertEquals($value1[$i], $entity->getEstimatedDate());
376+
if (class_exists('\Carbon\Carbon')) {
377+
$this->assertEquals($value2, $entity->getDate()->year);
378+
}
369379
}
370380
}
371381

tests/Entity/PostTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,24 @@ public function dateProvider()
512512
"Thu, 30 Apr 2015 16:51:00 GMT",
513513
"Thu, 30 Apr 2015 17:02:00 GMT",
514514
"Fri, 01 May 2015 08:00:00 GMT",
515+
],
516+
[
517+
2015,
518+
2015,
519+
2015,
520+
2015,
521+
2015,
522+
2015,
523+
2015,
524+
2015,
525+
2015,
526+
2015,
527+
2015,
528+
2015,
529+
2015,
530+
2015,
531+
2015,
532+
2015,
515533
]
516534
]
517535
];
@@ -522,13 +540,17 @@ public function dateProvider()
522540
* @param $posts
523541
* @dataProvider dateProvider
524542
*/
525-
public function testDate($file, $posts)
543+
public function testDate($file, $posts, $years)
526544
{
527545
/** @var Discussion $entity */
528546
foreach ($this->ei($file) as $entity) {
529547
/** @var Post $post */
530548
foreach ($entity->getPosts() as $i => $post) {
531549
$this->assertEquals($posts[$i], $post->getDate());
550+
if (class_exists('\Carbon\Carbon')) {
551+
$this->assertEquals($years[$i], $post->getDate()->year);
552+
}
553+
532554
}
533555
}
534556
}

0 commit comments

Comments
 (0)