|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of gpupo/netshoes-sdk |
|
5
|
|
|
* Created by Gilmar Pupo <[email protected]> |
|
6
|
|
|
* For the information of copyright and license you should read the file |
|
7
|
|
|
* LICENSE which is distributed with this source code. |
|
8
|
|
|
* Para a informação dos direitos autorais e de licença você deve ler o arquivo |
|
9
|
|
|
* LICENSE que é distribuído com este código-fonte. |
|
10
|
|
|
* Para obtener la información de los derechos de autor y la licencia debe leer |
|
11
|
|
|
* el archivo LICENSE que se distribuye con el código fuente. |
|
12
|
|
|
* For more information, see <http://www.g1mr.com/>. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Gpupo\NetshoesSdk\Entity; |
|
16
|
|
|
|
|
17
|
|
|
use Gpupo\CommonSdk\Entity\EntityInterface; |
|
18
|
|
|
use Gpupo\CommonSdk\Entity\ManagerAbstract; |
|
19
|
|
|
use Gpupo\CommonSdk\Entity\ManagerInterface; |
|
20
|
|
|
|
|
21
|
|
|
abstract class AbstractManager extends ManagerAbstract implements ManagerInterface |
|
22
|
|
|
{ |
|
23
|
3 |
|
protected function fetchDefaultParameters() |
|
24
|
|
|
{ |
|
25
|
|
|
return [ |
|
26
|
3 |
|
'buId' => 'ZT', |
|
27
|
|
|
]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
7 |
|
public function factoryMap($operation, array $parameters = null) |
|
31
|
|
|
{ |
|
32
|
7 |
|
return parent::factoryMap($operation, array_merge($this->fetchDefaultParameters(), (array) $parameters)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return Gpupo\Common\Entity\CollectionAbstract|false |
|
37
|
|
|
*/ |
|
38
|
2 |
|
protected function fetchPrepare($data) |
|
39
|
|
|
{ |
|
40
|
2 |
|
if (empty($data)) { |
|
41
|
|
|
return false; |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
return $this->factoryEntityCollection($data); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
protected function factoryEntityCollection($data) |
|
48
|
|
|
{ |
|
49
|
2 |
|
return $this->factoryNeighborObject($this->getEntityName().'Collection', $data); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function save(EntityInterface $entity, $route = 'save') |
|
53
|
|
|
{ |
|
54
|
1 |
|
return $this->execute($this->factoryMap($route), $entity->toJson($route)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return Gpupo\Common\Entity\CollectionAbstract|false |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function findById($itemId) |
|
61
|
|
|
{ |
|
62
|
3 |
|
$data = parent::findById($itemId); |
|
63
|
|
|
|
|
64
|
3 |
|
if (empty($data) || $data->get('type') === 'Resource_Not_Found') { |
|
65
|
1 |
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
return $this->factoryEntity($data->toArray()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
* |
|
74
|
|
|
* @codeCoverageIgnore |
|
75
|
|
|
*/ |
|
76
|
|
|
public function update(EntityInterface $entity, EntityInterface $existent = null) |
|
77
|
|
|
{ |
|
78
|
|
|
$text = 'Chamada a Atualização de entity '.$this->entity; |
|
79
|
|
|
|
|
80
|
|
|
return $this->log('debug', $text, [ |
|
81
|
|
|
'entity' => $entity, |
|
82
|
|
|
'existent' => $existent, |
|
83
|
|
|
]); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.