Skip to content

Commit 07bd200

Browse files
committed
Merge branch 'master' into develop
2 parents 7178618 + 9cf8823 commit 07bd200

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Jenssegers\Mongodb\Builder as QueryBuilder;
99
use Jenssegers\Mongodb\Relations\BelongsTo;
1010

11+
use Carbon\Carbon;
1112
use DateTime;
1213
use MongoId;
1314
use MongoDate;
@@ -66,19 +67,25 @@ public function fromDateTime($value)
6667
*/
6768
protected function asDateTime($value)
6869
{
69-
// Convert MongoDate to timestamp
70-
if ($value instanceof MongoDate)
70+
// Convert timestamp
71+
if (is_numeric($value))
7172
{
72-
$value = $value->sec;
73+
return Carbon::createFromTimestamp($value);
7374
}
7475

75-
// Convert timestamp to string for DateTime
76-
if (is_int($value))
76+
// Convert string
77+
if (is_string($value))
78+
{
79+
return new Carbon($value);
80+
}
81+
82+
// Convert MongoDate
83+
if ($value instanceof MongoDate)
7784
{
78-
$value = "@$value";
85+
return Carbon::createFromTimestamp($value->sec);
7986
}
8087

81-
return new DateTime($value);
88+
return Carbon::instance($value);
8289
}
8390

8491
/**

tests/ModelTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Carbon;
4+
35
class ModelTest extends PHPUnit_Framework_TestCase {
46

57
public function setUp() {}
@@ -37,7 +39,7 @@ public function testInsert()
3739
$this->assertTrue(isset($user->_id));
3840
$this->assertNotEquals('', (string) $user->_id);
3941
$this->assertNotEquals(0, strlen((string) $user->_id));
40-
$this->assertInstanceOf('DateTime', $user->created_at);
42+
$this->assertInstanceOf('Carbon\Carbon', $user->created_at);
4143

4244
$this->assertEquals('John Doe', $user->name);
4345
$this->assertEquals(35, $user->age);
@@ -57,8 +59,8 @@ public function testUpdate()
5759
$check->save();
5860

5961
$this->assertEquals(true, $check->exists);
60-
$this->assertInstanceOf('DateTime', $check->created_at);
61-
$this->assertInstanceOf('DateTime', $check->updated_at);
62+
$this->assertInstanceOf('Carbon\Carbon', $check->created_at);
63+
$this->assertInstanceOf('Carbon\Carbon', $check->updated_at);
6264
$this->assertEquals(1, User::count());
6365

6466
$this->assertEquals('John Doe', $check->name);
@@ -229,7 +231,7 @@ public function testSoftDelete()
229231
$this->assertEquals(1, $all->count());
230232

231233
$check = $all[0];
232-
$this->assertInstanceOf('DateTime', $check->deleted_at);
234+
$this->assertInstanceOf('Carbon\Carbon', $check->deleted_at);
233235
$this->assertEquals(true, $check->trashed());
234236

235237
$check->restore();
@@ -312,11 +314,11 @@ public function testUnset()
312314
public function testDates()
313315
{
314316
$user = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1')));
315-
$this->assertInstanceOf('DateTime', $user->birthday);
317+
$this->assertInstanceOf('Carbon\Carbon', $user->birthday);
316318

317-
// Re-fetch to be sure
318-
$user = User::find($user->_id);
319-
$this->assertInstanceOf('DateTime', $user->birthday);
319+
$check = User::find($user->_id);
320+
$this->assertInstanceOf('Carbon\Carbon', $check->birthday);
321+
$this->assertEquals($user->birthday, $check->birthday);
320322

321323
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
322324
$this->assertEquals('John Doe', $user->name);

0 commit comments

Comments
 (0)