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
aa4d483f
Commit
aa4d483f
authored
Jun 18, 2015
by
Valentinas Bartusevičius
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
DateInterval Type now is stored as Y-m-d H:i:s interval representation.
parent
bd9931ea
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
8 deletions
+24
-8
DateIntervalType.php
lib/Doctrine/DBAL/Types/DateIntervalType.php
+20
-4
DateIntervalTest.php
tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php
+4
-4
No files found.
lib/Doctrine/DBAL/Types/DateIntervalType.php
View file @
aa4d483f
...
...
@@ -10,6 +10,8 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
*/
class
DateIntervalType
extends
Type
{
const
DATEINTERVAL_PATTERN
=
'#(?P<date>\d{4}-\d{2}-\d{2}).(?P<time>\d{2}:\d{2}:\d{2})#'
;
/**
* {@inheritdoc}
*/
...
...
@@ -31,8 +33,19 @@ class DateIntervalType extends Type
*/
public
function
convertToDatabaseValue
(
$value
,
AbstractPlatform
$platform
)
{
return
(
$value
!==
null
)
?
$value
->
format
(
'P%yY%mM%dDT%hH%iM%sS'
)
:
null
;
$spec
=
null
;
if
(
$value
!==
null
)
{
/** @var \DateInterval $value */
$spec
=
str_pad
(
$value
->
y
,
4
,
'0'
,
STR_PAD_LEFT
)
.
'-'
.
$value
->
format
(
'%M'
)
.
'-'
.
$value
->
format
(
'%D'
)
.
' '
.
$value
->
format
(
'%H'
)
.
':'
.
$value
->
format
(
'%I'
)
.
':'
.
$value
->
format
(
'%S'
)
;
}
return
$spec
;
}
/**
...
...
@@ -44,10 +57,13 @@ class DateIntervalType extends Type
return
$value
;
}
if
(
preg_match
(
self
::
DATEINTERVAL_PATTERN
,
$value
,
$parts
)
!==
1
)
{
throw
ConversionException
::
conversionFailedFormat
(
$value
,
$this
->
getName
(),
'Y-m-d H:i:s'
);
}
try
{
$interval
=
new
\DateInterval
(
$value
);
$interval
=
new
\DateInterval
(
'P'
.
$parts
[
'date'
]
.
'T'
.
$parts
[
'time'
]
);
}
catch
(
\Exception
$e
)
{
throw
ConversionException
::
conversionFailedFormat
(
$value
,
$this
->
getName
(),
'P
xYxMxDTxHxMxS
'
);
throw
ConversionException
::
conversionFailedFormat
(
$value
,
$this
->
getName
(),
'P
Y-m-dTH:i:s
'
);
}
...
...
tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php
View file @
aa4d483f
...
...
@@ -19,9 +19,9 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
public
function
testDateIntervalConvertsToDatabaseValue
()
{
$interval
=
new
\DateInterval
(
'P1DT1H2M3S'
);
$interval
=
new
\DateInterval
(
'P
2Y
1DT1H2M3S'
);
$expected
=
$interval
->
format
(
'P%yY%mM%dDT%hH%iM%sS'
)
;
$expected
=
'0002-00-01 01:02:03'
;
$actual
=
$this
->
_type
->
convertToDatabaseValue
(
$interval
,
$this
->
_platform
);
$this
->
assertEquals
(
$expected
,
$actual
);
...
...
@@ -29,9 +29,9 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
public
function
testDateIntervalConvertsToPHPValue
()
{
$date
=
$this
->
_type
->
convertToPHPValue
(
'
P1DT1H2M3S
'
,
$this
->
_platform
);
$date
=
$this
->
_type
->
convertToPHPValue
(
'
0002-00-01 01:02:03
'
,
$this
->
_platform
);
$this
->
assertInstanceOf
(
'DateInterval'
,
$date
);
$this
->
assertEquals
(
'P
0
Y0M1DT1H2M3S'
,
$date
->
format
(
'P%yY%mM%dDT%hH%iM%sS'
));
$this
->
assertEquals
(
'P
2
Y0M1DT1H2M3S'
,
$date
->
format
(
'P%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