1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule; |
4
|
|
|
use NilPortugues\Example\Domain\UserId; |
5
|
|
|
use NilPortugues\Example\Persistence\Eloquent\User; |
6
|
|
|
use NilPortugues\Example\Persistence\Eloquent\UserRepository; |
7
|
|
|
use NilPortugues\Example\Service\UserAdapter; |
8
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Filter; |
9
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Order; |
10
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Sort; |
11
|
|
|
|
12
|
|
|
include_once '../vendor/autoload.php'; |
13
|
|
|
|
14
|
|
|
//------------------------------------------------------------------------------------------------------------- |
15
|
|
|
// - Create database if does not exist |
16
|
|
|
//------------------------------------------------------------------------------------------------------------- |
17
|
|
|
$capsule = new Capsule(); |
18
|
|
|
|
19
|
|
|
//Adds MongoDb support. |
20
|
|
|
$capsule->getDatabaseManager()->extend('mongodb', function ($config) { |
21
|
|
|
return new Jenssegers\Mongodb\Connection($config); |
22
|
|
|
}); |
23
|
|
|
|
24
|
|
|
//Create connection |
25
|
|
|
$capsule->addConnection([ |
26
|
|
|
'driver' => 'mongodb', |
27
|
|
|
'host' => 'localhost', |
28
|
|
|
'port' => 27017, |
29
|
|
|
'database' => 'users', |
30
|
|
|
'username' => '', |
31
|
|
|
'password' => '', |
32
|
|
|
'options' => [ |
33
|
|
|
'db' => 'admin', |
34
|
|
|
], |
35
|
|
|
], |
36
|
|
|
'default' |
37
|
|
|
); |
38
|
|
|
$capsule->bootEloquent(); |
39
|
|
|
$capsule->setAsGlobal(); |
40
|
|
|
|
41
|
|
|
//------------------------------------------------------------------------------------------------------------- |
42
|
|
|
// - Create dummy data for the same of the example. |
43
|
|
|
//------------------------------------------------------------------------------------------------------------- |
44
|
|
|
User::query()->delete(); |
45
|
|
|
|
46
|
|
|
$model = new User(); |
47
|
|
|
$model->id = 1; |
|
|
|
|
48
|
|
|
$model->name = 'Admin User'; |
|
|
|
|
49
|
|
|
$model->created_at = '2016-02-18'; |
|
|
|
|
50
|
|
|
$model->save(); |
51
|
|
|
|
52
|
|
|
for ($i = 2; $i <= 20; ++$i) { |
53
|
|
|
$model = new User(); |
54
|
|
|
$model->id = $i; |
|
|
|
|
55
|
|
|
$model->name = 'Dummy User '.$i; |
|
|
|
|
56
|
|
|
$model->created_at = (new DateTime())->setDate(2016, rand(1, 12), rand(1, 27)); |
|
|
|
|
57
|
|
|
$model->save(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
//------------------------------------------------------------------------------------------------------------- |
61
|
|
|
// - getUserAction |
62
|
|
|
//------------------------------------------------------------------------------------------------------------- |
63
|
|
|
$userAdapter = new UserAdapter(); |
64
|
|
|
$repository = new UserRepository($userAdapter); |
65
|
|
|
|
66
|
|
|
$userId = new UserId(1); |
67
|
|
|
print_r($repository->find($userId)); |
68
|
|
|
|
69
|
|
|
//------------------------------------------------------------------------------------------------------------- |
70
|
|
|
// - getUsersRegisteredLastMonth |
71
|
|
|
//------------------------------------------------------------------------------------------------------------- |
72
|
|
|
|
73
|
|
|
$filter = new Filter(); |
74
|
|
|
$filter->must()->notIncludeGroup('id', [2, 5]); |
75
|
|
|
$filter->must()->beGreaterThan('created_at', new DateTime('2016-03-01')); |
76
|
|
|
|
77
|
|
|
$sort = new Sort(); |
78
|
|
|
$sort->setOrderFor('created_at', new Order('ASC')); |
79
|
|
|
|
80
|
|
|
print_r($repository->findBy($filter, $sort)); |
81
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.