Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
ff369310
Unverified
Commit
ff369310
authored
Jul 02, 2017
by
Luis Galeas
Committed by
Sergei Morozov
Mar 28, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
DateIntervalType (negative support) resolves doctrine/dbal#2578
parent
16937314
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
7 deletions
+35
-7
DateIntervalType.php
lib/Doctrine/DBAL/Types/DateIntervalType.php
+11
-3
DateIntervalTest.php
tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php
+24
-4
No files found.
lib/Doctrine/DBAL/Types/DateIntervalType.php
View file @
ff369310
...
...
@@ -9,6 +9,8 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
*/
class
DateIntervalType
extends
Type
{
const
FORMAT
=
'%RP%YY%MM%DDT%HH%IM%SS'
;
/**
* {@inheritdoc}
*/
...
...
@@ -38,7 +40,7 @@ class DateIntervalType extends Type
}
if
(
$value
instanceof
\DateInterval
)
{
return
$value
->
format
(
'P%YY%MM%DDT%HH%IM%SS'
);
return
$value
->
format
(
self
::
FORMAT
);
}
throw
ConversionException
::
conversionFailedInvalidType
(
$value
,
$this
->
getName
(),
[
'null'
,
'DateInterval'
]);
...
...
@@ -54,9 +56,15 @@ class DateIntervalType extends Type
}
try
{
return
new
\DateInterval
(
$value
);
$interval
=
new
\DateInterval
(
substr
(
$value
,
1
));
if
(
substr
(
$value
,
0
,
1
)
===
'-'
)
{
$interval
->
invert
=
1
;
}
return
$interval
;
}
catch
(
\Exception
$exception
)
{
throw
ConversionException
::
conversionFailedFormat
(
$value
,
$this
->
getName
(),
'P%YY%MM%DDT%HH%IM%SS'
,
$exception
);
throw
ConversionException
::
conversionFailedFormat
(
$value
,
$this
->
getName
(),
self
::
FORMAT
,
$exception
);
}
}
...
...
tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php
View file @
ff369310
...
...
@@ -32,7 +32,7 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
{
$interval
=
new
\DateInterval
(
'P2Y1DT1H2M3S'
);
$expected
=
'P02Y00M01DT01H02M03S'
;
$expected
=
'
+
P02Y00M01DT01H02M03S'
;
$actual
=
$this
->
type
->
convertToDatabaseValue
(
$interval
,
$this
->
platform
);
self
::
assertEquals
(
$expected
,
$actual
);
...
...
@@ -40,9 +40,29 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
public
function
testDateIntervalConvertsToPHPValue
()
{
$date
=
$this
->
type
->
convertToPHPValue
(
'P02Y00M01DT01H02M03S'
,
$this
->
platform
);
self
::
assertInstanceOf
(
'DateInterval'
,
$date
);
self
::
assertEquals
(
'P02Y00M01DT01H02M03S'
,
$date
->
format
(
'P%YY%MM%DDT%HH%IM%SS'
));
$interval
=
$this
->
type
->
convertToPHPValue
(
'+P02Y00M01DT01H02M03S'
,
$this
->
platform
);
self
::
assertInstanceOf
(
'DateInterval'
,
$interval
);
self
::
assertEquals
(
'+P02Y00M01DT01H02M03S'
,
$interval
->
format
(
'%RP%YY%MM%DDT%HH%IM%SS'
));
}
public
function
testNegativeDateIntervalConvertsToDatabaseValue
()
{
$interval
=
new
\DateInterval
(
'P2Y1DT1H2M3S'
);
$interval
->
invert
=
1
;
$expected
=
'-P02Y00M01DT01H02M03S'
;
$actual
=
$this
->
type
->
convertToDatabaseValue
(
$interval
,
$this
->
platform
);
self
::
assertEquals
(
$expected
,
$actual
);
}
public
function
testNegativeDateIntervalConvertsToPHPValue
()
{
$interval
=
$this
->
type
->
convertToPHPValue
(
'-P02Y00M01DT01H02M03S'
,
$this
->
platform
);
self
::
assertInstanceOf
(
'DateInterval'
,
$interval
);
self
::
assertEquals
(
'-P02Y00M01DT01H02M03S'
,
$interval
->
format
(
'%RP%YY%MM%DDT%HH%IM%SS'
));
}
public
function
testInvalidDateIntervalFormatConversion
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment