|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SpeckCatalog\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\ServiceManager\ServiceLocatorAwareInterface; |
|
6
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
7
|
|
|
use SpeckCatalog\Model\AbstractModel; |
|
8
|
|
|
use SpeckCatalog\Mapper\AbstractMapper; |
|
9
|
|
|
use Zend\EventManager\EventManagerAwareInterface; |
|
10
|
|
|
use Zend\EventManager\EventManagerAwareTrait; |
|
11
|
|
|
|
|
12
|
|
|
class AbstractService implements ServiceLocatorAwareInterface, EventManagerAwareInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use EventManagerAwareTrait; |
|
15
|
|
|
|
|
16
|
|
|
protected $serviceLocator; |
|
17
|
|
|
protected $enabledOnly = false; |
|
18
|
|
|
|
|
19
|
|
|
public function search(array $params) |
|
20
|
|
|
{ |
|
21
|
|
|
return $this->getEntityMapper()->search($params); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
5 |
|
public function find(array $data, $populate = false, $recursive = false) |
|
25
|
|
|
{ |
|
26
|
5 |
|
$result = $this->getEntityMapper()->find($data); |
|
27
|
5 |
|
if (!$result) { |
|
28
|
2 |
|
return false; |
|
29
|
|
|
} |
|
30
|
3 |
|
if ($populate) { |
|
31
|
|
|
//$populate may be an array of things to populate |
|
32
|
1 |
|
$this->populate($result, $recursive, $populate); |
|
33
|
1 |
|
} |
|
34
|
3 |
|
return $result; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function findRow(array $data) |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->getEntityMapper()->findRow($data); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function findRows(array $data) |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->getEntityMapper()->findRows($data); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function findMany(array $data, $populate = false, $recursive = false) |
|
48
|
|
|
{ |
|
49
|
|
|
$result = $this->getEntityMapper()->findMany($data); |
|
50
|
|
|
if (!count($result)) { |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
if ($populate) { |
|
54
|
|
|
foreach ($result as $res) { |
|
55
|
|
|
//$populate may be an array of things to populate |
|
56
|
|
|
$this->populate($res, $recursive, $populate); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
return $result; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function populate($model, $recursive = false, $children = true) |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $model; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getModel($construct = null) |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->getEntityMapper()->getModel($construct); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public function getAll() |
|
73
|
|
|
{ |
|
74
|
1 |
|
return $this->getEntityMapper()->getAll(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
31 |
|
public function getEntityMapper() |
|
78
|
|
|
{ |
|
79
|
31 |
|
if (is_string($this->entityMapper) && strstr($this->entityMapper, '_mapper')) { |
|
80
|
1 |
|
$this->entityMapper = $this->getServiceLocator()->get($this->entityMapper); |
|
|
|
|
|
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
31 |
|
if ($this->entityMapper instanceof AbstractMapper) { |
|
84
|
30 |
|
$this->entityMapper->setEnabledOnly($this->enabledOnly()); |
|
85
|
30 |
|
} |
|
86
|
|
|
|
|
87
|
31 |
|
return $this->entityMapper; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
31 |
|
public function setEntityMapper($entityMapper) |
|
91
|
|
|
{ |
|
92
|
31 |
|
$this->entityMapper = $entityMapper; |
|
93
|
31 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
12 |
|
public function getServiceLocator() |
|
97
|
|
|
{ |
|
98
|
12 |
|
return $this->serviceLocator; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
12 |
|
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) |
|
102
|
|
|
{ |
|
103
|
12 |
|
$this->serviceLocator = $serviceLocator; |
|
104
|
12 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
5 |
|
public function update($dataOrModel, array $originalValues = null) |
|
108
|
|
|
{ |
|
109
|
5 |
|
return $this->getEntityMapper()->update($dataOrModel, $originalValues); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
7 |
|
public function insert($model) |
|
113
|
|
|
{ |
|
114
|
7 |
|
return $this->getEntityMapper()->insert($model); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function delete(array $where) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->getEntityMapper()->delete($where); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function usePaginator($options = array()) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->getEntityMapper()->usePaginator($options); |
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
public function setEnabledOnly($bool) |
|
129
|
|
|
{ |
|
130
|
1 |
|
$this->enabledOnly = $bool; |
|
131
|
1 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
32 |
|
public function enabledOnly() |
|
135
|
|
|
{ |
|
136
|
32 |
|
return $this->enabledOnly; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: