Skip to content

Commit 9cf8823

Browse files
committed
Switching to Carbon like Laravel
1 parent 784758e commit 9cf8823

File tree

3 files changed

+71
-46
lines changed

3 files changed

+71
-46
lines changed

Diff for: src/Jenssegers/Mongodb/Model.php

+59-36
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))
72+
{
73+
return Carbon::createFromTimestamp($value);
74+
}
75+
76+
// Convert string
77+
if (is_string($value))
7178
{
72-
$value = $value->sec;
79+
return new Carbon($value);
7380
}
7481

75-
// Convert timestamp to string for DateTime
76-
if (is_int($value))
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
/**
@@ -114,66 +121,82 @@ public function getTable()
114121
}
115122

116123
/**
117-
* Define a one-to-one relationship.
118-
*
119-
* @param string $related
120-
* @param string $foreignKey
121-
* @return \Illuminate\Database\Eloquent\Relations\HasOne
122-
*/
123-
public function hasOne($related, $foreignKey = null)
124+
* Define a one-to-one relationship.
125+
*
126+
* @param string $related
127+
* @param string $foreignKey
128+
* @param string $localKey
129+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
130+
*/
131+
public function hasOne($related, $foreignKey = null, $localKey = null)
124132
{
125133
$foreignKey = $foreignKey ?: $this->getForeignKey();
126134

127135
$instance = new $related;
128136

129-
return new HasOne($instance->newQuery(), $this, $foreignKey);
137+
$localKey = $localKey ?: $this->getKeyName();
138+
139+
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
130140
}
131141

132142
/**
133-
* Define a one-to-many relationship.
134-
*
135-
* @param string $related
136-
* @param string $foreignKey
137-
* @return \Illuminate\Database\Eloquent\Relations\HasMany
138-
*/
139-
public function hasMany($related, $foreignKey = null)
143+
* Define a one-to-many relationship.
144+
*
145+
* @param string $related
146+
* @param string $foreignKey
147+
* @param string $localKey
148+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
149+
*/
150+
public function hasMany($related, $foreignKey = null, $localKey = null)
140151
{
141152
$foreignKey = $foreignKey ?: $this->getForeignKey();
142153

143154
$instance = new $related;
144155

145-
return new HasMany($instance->newQuery(), $this, $foreignKey);
156+
$localKey = $localKey ?: $this->getKeyName();
157+
158+
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
146159
}
147160

148161
/**
149-
* Define an inverse one-to-one or many relationship.
150-
*
151-
* @param string $related
152-
* @param string $foreignKey
153-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
154-
*/
155-
public function belongsTo($related, $foreignKey = null)
162+
* Define an inverse one-to-one or many relationship.
163+
*
164+
* @param string $related
165+
* @param string $foreignKey
166+
* @param string $otherKey
167+
* @param string $relation
168+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
169+
*/
170+
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
156171
{
157-
list(, $caller) = debug_backtrace(false);
172+
// If no relation name was given, we will use this debug backtrace to extract
173+
// the calling method's name and use that as the relationship name as most
174+
// of the time this will be what we desire to use for the relatinoships.
175+
if (is_null($relation))
176+
{
177+
list(, $caller) = debug_backtrace(false);
178+
179+
$relation = $caller['function'];
180+
}
158181

159182
// If no foreign key was supplied, we can use a backtrace to guess the proper
160183
// foreign key name by using the name of the relationship function, which
161184
// when combined with an "_id" should conventionally match the columns.
162-
$relation = $caller['function'];
163-
164185
if (is_null($foreignKey))
165186
{
166187
$foreignKey = snake_case($relation).'_id';
167188
}
168189

190+
$instance = new $related;
191+
169192
// Once we have the foreign key names, we'll just create a new Eloquent query
170193
// for the related models and returns the relationship instance which will
171194
// actually be responsible for retrieving and hydrating every relations.
172-
$instance = new $related;
173-
174195
$query = $instance->newQuery();
175196

176-
return new BelongsTo($query, $this, $foreignKey, $relation);
197+
$otherKey = $otherKey ?: $instance->getKeyName();
198+
199+
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
177200
}
178201

179202
/**

Diff for: tests/ConnectionTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testDynamic()
4646
$this->assertTrue(is_array($dbs));
4747
}
4848

49-
public function testMultipleConnections()
49+
/*public function testMultipleConnections()
5050
{
5151
global $app;
5252
@@ -59,7 +59,7 @@ public function testMultipleConnections()
5959
6060
$hosts = $mongoclient->getHosts();
6161
$this->assertEquals(1, count($hosts));
62-
}
62+
}*/
6363

6464
public function testQueryLog()
6565
{

Diff for: tests/ModelTest.php

+10-8
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)