1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* API for Billing |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/billing-hiapi |
6
|
|
|
* @package billing-hiapi |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\billing\hiapi\target; |
12
|
|
|
|
13
|
|
|
use hiqdev\billing\hiapi\models\Target; |
14
|
|
|
use hiqdev\php\billing\target\TargetFactoryInterface; |
15
|
|
|
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator; |
16
|
|
|
use Zend\Hydrator\HydratorInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class TargetHydrator. |
20
|
|
|
* |
21
|
|
|
* @author Andrii Vasyliev <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class TargetHydrator extends GeneratedHydrator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TargetFactoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $targetFactory; |
29
|
|
|
|
30
|
1 |
|
public function __construct(HydratorInterface $hydrator, TargetFactoryInterface $targetFactory) |
31
|
|
|
{ |
32
|
1 |
|
parent::__construct($hydrator); |
33
|
1 |
|
$this->targetFactory = $targetFactory; |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
* @param object|Target $object |
39
|
|
|
*/ |
40
|
|
|
public function extract($object) |
41
|
|
|
{ |
42
|
|
|
$result = array_filter([ |
43
|
|
|
'id' => $object->getId(), |
44
|
|
|
'type' => $object->getType(), |
45
|
|
|
'name' => $object->getName(), |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
return $result; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** {@inheritdoc} */ |
52
|
6 |
View Code Duplication |
public function hydrate(array $data, $object) |
|
|
|
|
53
|
|
|
{ |
54
|
6 |
|
if (!empty($data['type'])) { |
55
|
6 |
|
$data['type'] = $this->targetFactory->shortenType($data['type']); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
return parent::hydrate($data, $object); |
59
|
|
|
} |
60
|
|
|
|
61
|
5 |
|
public function createEmptyInstance(string $className, array $data = []) |
62
|
|
|
{ |
63
|
5 |
|
if (isset($data['type'])) { |
64
|
5 |
|
$className = $this->targetFactory->getClassForType($data['type']); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
5 |
|
return parent::createEmptyInstance($className, $data); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.