Skip to content

Commit 112733d

Browse files
authored
Merge pull request #212 from scy/datetimeinterface
Use DateTimeInterface instead of DateTime
2 parents 9d5d24c + 2c724c9 commit 112733d

File tree

10 files changed

+25
-16
lines changed

10 files changed

+25
-16
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"require": {
77
"php": ">=5.5.9",
88
"ext-curl": "*",
9+
"ext-json": "*",
910
"netresearch/jsonmapper": "~0.11|^1.0",
1011
"monolog/monolog": "~1.12",
1112
"vlucas/phpdotenv": "~1.0|~2.0"

src/Issue/Attachment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Attachment implements \JsonSerializable
1616
/* @var \JiraRestApi\Issue\Reporter */
1717
public $author;
1818

19-
/* @var \DateTime */
19+
/* @var \DateTimeInterface */
2020
public $created;
2121

2222
/* @var int */

src/Issue/Comment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class Comment implements \JsonSerializable
2121
/** @var \JiraRestApi\Issue\Reporter */
2222
public $updateAuthor;
2323

24-
/** @var \DateTime */
24+
/** @var \DateTimeInterface */
2525
public $created;
2626

27-
/** @var \DateTime */
27+
/** @var \DateTimeInterface */
2828
public $updated;
2929

3030
/** @var \JiraRestApi\Issue\Visibility */

src/Issue/IssueField.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class IssueField implements \JsonSerializable
2323
/** @var Reporter|null */
2424
public $reporter;
2525

26-
/** @var \DateTime */
26+
/** @var \DateTimeInterface */
2727
public $created;
2828

29-
/** @var \DateTime */
29+
/** @var \DateTimeInterface */
3030
public $updated;
3131

3232
/** @var string|null */
@@ -92,7 +92,7 @@ class IssueField implements \JsonSerializable
9292
/** @var string|null */
9393
public $resolutiondate;
9494

95-
/** @var \DateTime|null */
95+
/** @var \DateTimeInterface|null */
9696
public $duedate;
9797

9898
/** @var array */
@@ -412,16 +412,16 @@ public function setSecurityId($id)
412412
/**
413413
* set issue's due date.
414414
*
415-
* @param \DateTime|null $duedate due date string or DateTime object
416-
* @param string $format datetime string format.
415+
* @param string|\DateTimeInterface|null $duedate due date string or DateTimeInterface object
416+
* @param string $format datetime string format.
417417
*
418418
* @return $this
419419
*/
420420
public function setDueDate($duedate, $format = 'Y-m-d')
421421
{
422422
if (is_string($duedate)) {
423423
$this->duedate = $duedate;
424-
} elseif ($duedate instanceof \DateTime) {
424+
} elseif ($duedate instanceof \DateTimeInterface) {
425425
$this->duedate = $duedate->format($format);
426426
} else {
427427
$this->duedate = null;

src/Issue/Version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Version implements \JsonSerializable
2222
/** @var bool */
2323
public $released;
2424

25-
/** @var \DateTime|null */
25+
/** @var \DateTimeInterface|null */
2626
public $releaseDate;
2727

2828
/** @var bool */

src/Issue/Worklog.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ public function setComment($comment)
8787
return $this;
8888
}
8989

90+
// Note that in the docblock below, you cannot replace `mixed` by `\DateTimeInterface|string` because JsonMapper doesn't support that,
91+
// see <https://github.com/cweiske/jsonmapper/issues/64#issuecomment-269545585>.
92+
9093
/**
9194
* Function to set start time of worklog.
9295
*
93-
* @param mixed $started started time value(\DateTime|string) e.g. - new DateTime("2016-03-17 11:15:34") or "2016-03-17 11:15:34"
96+
* @param mixed $started started time value(\DateTimeInterface|string) e.g. - new \DateTime("2016-03-17 11:15:34") or "2016-03-17 11:15:34"
9497
*
9598
* @throws JiraException
9699
*
@@ -100,10 +103,10 @@ public function setStarted($started)
100103
{
101104
if (is_string($started)) {
102105
$dt = new \DateTime($started);
103-
} elseif ($started instanceof \DateTime) {
106+
} elseif ($started instanceof \DateTimeInterface) {
104107
$dt = $started;
105108
} else {
106-
throw new JiraException('field only accept date string or DateTime class.'.get_class($started));
109+
throw new JiraException('field only accept date string or DateTimeInterface object.'.get_class($started));
107110
}
108111

109112
// workround micro second
@@ -115,7 +118,7 @@ public function setStarted($started)
115118
/**
116119
* Function to set start time of worklog.
117120
*
118-
* @param \DateTime $started e.g. - new DateTime("2014-04-05 16:00:00")
121+
* @param \DateTimeInterface $started e.g. - new \DateTime("2014-04-05 16:00:00")
119122
*
120123
* @return Worklog
121124
*/

src/JiraClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public function __construct(ConfigurationInterface $configuration = null, Logger
8181
// Fix "\JiraRestApi\JsonMapperHelper::class" syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
8282
$this->json_mapper->undefinedPropertyHandler = [new \JiraRestApi\JsonMapperHelper(), 'setUndefinedProperty'];
8383

84+
// Properties that are annotated with `@var \DateTimeInterface` should result in \DateTime objects being created.
85+
$this->json_mapper->classMap['\\'.\DateTimeInterface::class] = \DateTime::class;
86+
8487
// create logger
8588
if ($logger) {
8689
$this->log = $logger;

src/Version/VersionService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class VersionService extends \JiraRestApi\JiraClient
2424
*/
2525
public function create($version)
2626
{
27-
if ($version->releaseDate instanceof \DateTime) {
27+
if ($version->releaseDate instanceof \DateTimeInterface) {
2828
$version->releaseDate = $version->releaseDate->format('Y-m-d');
2929
}
3030
$data = json_encode($version);
@@ -88,7 +88,7 @@ public function update(Version $version)
8888
throw new JiraException($version->id.' is not a valid version id.');
8989
}
9090

91-
if ($version->releaseDate instanceof \DateTime) {
91+
if ($version->releaseDate instanceof \DateTimeInterface) {
9292
$version->releaseDate = $version->releaseDate->format('Y-m-d');
9393
}
9494

tests/AssigneeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function setUp()
1515
{
1616
$this->mapper = new JsonMapper();
1717
$this->mapper->undefinedPropertyHandler = [new \JiraRestApi\JsonMapperHelper(), 'setUndefinedProperty'];
18+
$this->mapper->classMap['\\'.\DateTimeInterface::class] = \DateTime::class;
1819
}
1920

2021
public function tearDown()

tests/MapperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function setUp()
1717
{
1818
$this->mapper = new JsonMapper();
1919
$this->mapper->undefinedPropertyHandler = [new \JiraRestApi\JsonMapperHelper(), 'setUndefinedProperty'];
20+
$this->mapper->classMap['\\'.\DateTimeInterface::class] = \DateTime::class;
2021
}
2122

2223
public function tearDown()

0 commit comments

Comments
 (0)