1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\LaravelPayum\Storage; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Payum\Core\Model\Identity; |
7
|
|
|
use Payum\Core\Storage\AbstractStorage; |
8
|
|
|
|
9
|
|
|
class EloquentStorage extends AbstractStorage |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* $modelResolver. |
13
|
|
|
* |
14
|
|
|
* @var \Closure |
15
|
|
|
*/ |
16
|
|
|
protected $modelResolver; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $modelClass |
20
|
|
|
*/ |
21
|
8 |
|
public function __construct($modelClass) |
22
|
|
|
{ |
23
|
8 |
|
parent::__construct($modelClass); |
24
|
1 |
|
$this->modelResolver = function () { |
25
|
1 |
|
return new $this->modelClass(); |
26
|
|
|
}; |
27
|
8 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* setModelResolver. |
31
|
|
|
* |
32
|
|
|
* @return static |
33
|
|
|
*/ |
34
|
5 |
|
public function setModelResolver(Closure $closure) |
35
|
|
|
{ |
36
|
5 |
|
$this->modelResolver = $closure; |
37
|
5 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* create. |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
43
|
|
|
*/ |
44
|
4 |
|
public function create() |
45
|
|
|
{ |
46
|
4 |
|
$resolver = $this->modelResolver; |
47
|
|
|
|
48
|
4 |
|
return $resolver(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* findBy. |
53
|
|
|
* |
54
|
|
|
* @param array $criteria |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
1 |
|
public function findBy(array $criteria) |
58
|
|
|
{ |
59
|
1 |
|
$model = $this->create(); |
60
|
1 |
|
$query = $model->newQuery(); |
61
|
1 |
|
foreach ($criteria as $name => $value) { |
62
|
1 |
|
$query = $query->where($name, '=', $value); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $query->get()->all(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* doUpdateModel. |
70
|
|
|
* |
71
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
72
|
|
|
*/ |
73
|
1 |
|
protected function doUpdateModel($model) |
74
|
|
|
{ |
75
|
1 |
|
$model->save(); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* doDeleteModel. |
80
|
|
|
* |
81
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
82
|
|
|
*/ |
83
|
1 |
|
protected function doDeleteModel($model) |
84
|
|
|
{ |
85
|
1 |
|
$model->delete(); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* doGetIdentity. |
90
|
|
|
* |
91
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
92
|
|
|
* @return \Payum\Core\Model\Identity |
93
|
|
|
*/ |
94
|
1 |
|
protected function doGetIdentity($model) |
95
|
|
|
{ |
96
|
1 |
|
return new Identity($model->getKey(), $model); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* doFind. |
101
|
|
|
* |
102
|
|
|
* @param mixed $id |
103
|
|
|
* @return object|null |
104
|
|
|
*/ |
105
|
2 |
|
protected function doFind($id) |
106
|
|
|
{ |
107
|
2 |
|
return $this->create()->find($id); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|