Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
sinan
laravel-mongodb
Commits
c773be2f
Commit
c773be2f
authored
Apr 15, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improving date conversions
parent
76f62009
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
24 deletions
+21
-24
Model.php
src/Jenssegers/Mongodb/Model.php
+9
-21
ModelTest.php
tests/ModelTest.php
+11
-2
User.php
tests/models/User.php
+1
-1
No files found.
src/Jenssegers/Mongodb/Model.php
View file @
c773be2f
...
@@ -92,19 +92,19 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
...
@@ -92,19 +92,19 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
*/
public
function
fromDateTime
(
$value
)
public
function
fromDateTime
(
$value
)
{
{
//
Convert DateTime to MongoDate
//
If the value is already a MongoDate instance, we don't need to parse it.
if
(
$value
instanceof
DateTim
e
)
if
(
$value
instanceof
MongoDat
e
)
{
{
$value
=
new
MongoDate
(
$value
->
getTimestamp
())
;
return
$value
;
}
}
//
Convert timestamp to MongoDate
//
Let Eloquent convert the value to a DateTime instance.
elseif
(
is_numeric
(
$value
)
)
if
(
!
$value
instanceof
DateTime
)
{
{
$value
=
new
MongoDat
e
(
$value
);
$value
=
parent
::
asDateTim
e
(
$value
);
}
}
return
$value
;
return
new
MongoDate
(
$value
->
getTimestamp
())
;
}
}
/**
/**
...
@@ -115,25 +115,13 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
...
@@ -115,25 +115,13 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
*/
protected
function
asDateTime
(
$value
)
protected
function
asDateTime
(
$value
)
{
{
// Convert timestamp
// Convert MongoDate instances.
if
(
is_numeric
(
$value
))
{
return
Carbon
::
createFromTimestamp
(
$value
);
}
// Convert string
if
(
is_string
(
$value
))
{
return
new
Carbon
(
$value
);
}
// Convert MongoDate
if
(
$value
instanceof
MongoDate
)
if
(
$value
instanceof
MongoDate
)
{
{
return
Carbon
::
createFromTimestamp
(
$value
->
sec
);
return
Carbon
::
createFromTimestamp
(
$value
->
sec
);
}
}
return
Carbon
::
instanc
e
(
$value
);
return
parent
::
asDateTim
e
(
$value
);
}
}
/**
/**
...
...
tests/ModelTest.php
View file @
c773be2f
...
@@ -331,13 +331,22 @@ class ModelTest extends TestCase {
...
@@ -331,13 +331,22 @@ class ModelTest extends TestCase {
// test custom date format for json output
// test custom date format for json output
$json
=
$user
->
toArray
();
$json
=
$user
->
toArray
();
$this
->
assertEquals
(
$user
->
birthday
->
format
(
'
U
'
),
$json
[
'birthday'
]);
$this
->
assertEquals
(
$user
->
birthday
->
format
(
'
l jS \of F Y h:i:s A
'
),
$json
[
'birthday'
]);
$this
->
assertEquals
(
$user
->
created_at
->
format
(
'
U
'
),
$json
[
'created_at'
]);
$this
->
assertEquals
(
$user
->
created_at
->
format
(
'
l jS \of F Y h:i:s A
'
),
$json
[
'created_at'
]);
// test default date format for json output
// test default date format for json output
$item
=
Item
::
create
(
array
(
'name'
=>
'sword'
));
$item
=
Item
::
create
(
array
(
'name'
=>
'sword'
));
$json
=
$item
->
toArray
();
$json
=
$item
->
toArray
();
$this
->
assertEquals
(
$item
->
created_at
->
format
(
'Y-m-d H:i:s'
),
$json
[
'created_at'
]);
$this
->
assertEquals
(
$item
->
created_at
->
format
(
'Y-m-d H:i:s'
),
$json
[
'created_at'
]);
$user
=
User
::
create
(
array
(
'name'
=>
'Jane Doe'
,
'birthday'
=>
time
()));
$this
->
assertInstanceOf
(
'Carbon\Carbon'
,
$user
->
birthday
);
$user
=
User
::
create
(
array
(
'name'
=>
'Jane Doe'
,
'birthday'
=>
'Monday 8th of August 2005 03:12:46 PM'
));
$this
->
assertInstanceOf
(
'Carbon\Carbon'
,
$user
->
birthday
);
$user
=
User
::
create
(
array
(
'name'
=>
'Jane Doe'
,
'birthday'
=>
'2005-08-08'
));
$this
->
assertInstanceOf
(
'Carbon\Carbon'
,
$user
->
birthday
);
}
}
public
function
testIdAttribute
()
public
function
testIdAttribute
()
...
...
tests/models/User.php
View file @
c773be2f
...
@@ -87,6 +87,6 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
...
@@ -87,6 +87,6 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
protected
function
getDateFormat
()
protected
function
getDateFormat
()
{
{
return
'
U
'
;
return
'
l jS \of F Y h:i:s A
'
;
}
}
}
}
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