|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of gpupo/pipe2 |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Gilmar Pupo <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* For more information, see |
|
12
|
|
|
* <https://opensource.gpupo.com/pipe2/>. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Gpupo\Pipe2\Converter; |
|
16
|
|
|
|
|
17
|
|
|
use Gpupo\Pipe2\Traits\DocumentContainerTrait; |
|
18
|
|
|
use Gpupo\Pipe2\Traits\ParserTrait; |
|
19
|
|
|
|
|
20
|
|
|
abstract class ConverterAbstract |
|
21
|
|
|
{ |
|
22
|
|
|
use ParserTrait; |
|
23
|
|
|
use DocumentContainerTrait; |
|
24
|
|
|
|
|
25
|
|
|
protected $input; |
|
26
|
|
|
protected $output; |
|
27
|
|
|
protected $channel; |
|
28
|
|
|
protected $slug; |
|
29
|
|
|
protected $idParameters; |
|
30
|
|
|
protected $schema; |
|
31
|
|
|
protected $normalizer; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(Array $parameters) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->input = $parameters['input']; |
|
36
|
|
|
$this->output = $parameters['output']; |
|
37
|
|
|
$this->channel = $parameters['channel']; |
|
38
|
|
|
$this->slug = $parameters['slug']; |
|
39
|
|
|
$this->idParameters = $parameters['id']; |
|
40
|
|
|
$this->setSchema(); |
|
41
|
|
|
$this->setNormalizer(); |
|
42
|
|
|
$this->factoryDocument($parameters['formatOutput']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function factoryDocument($formatOutput) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->setDocument(new Document($this->schema, $this->slug), $formatOutput); |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getNormalizer() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->normalizer; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function execute() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->appendItens($this->parser()); |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function appendItens($list) |
|
65
|
|
|
{ |
|
66
|
|
|
foreach ($list as $item) { |
|
67
|
|
|
$itemElement = $this->document->createElement('sphinx:document'); |
|
|
|
|
|
|
68
|
|
|
$id = $this->idParameters['prefix'].$item[$this->idParameters['field']]; |
|
69
|
|
|
$itemElement->setAttribute('id', $id); |
|
70
|
|
|
|
|
71
|
|
|
if (!array_key_exists('sku', $item) || empty($item['sku'])) { |
|
72
|
|
|
$item['sku'] = $id; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->populateDocument($itemElement, $item); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function fieldReduce(Array $item) |
|
80
|
|
|
{ |
|
81
|
|
|
$list = []; |
|
82
|
|
|
|
|
83
|
|
|
foreach ($item as $value) { |
|
84
|
|
|
$value['tag'] = $this->schema->normalizeFieldName($value['tag']); |
|
85
|
|
|
if ($this->schema->tagInSchema($value['tag'])) { |
|
86
|
|
|
$list[] = $value; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $list; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function extended($item) |
|
94
|
|
|
{ |
|
95
|
|
|
return $item; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function addSlugs(Array $item) |
|
99
|
|
|
{ |
|
100
|
|
|
if (empty($this->slug)) { |
|
101
|
|
|
return $item; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
foreach ($this->schema->getSluggables() as $key) { |
|
105
|
|
|
if (array_key_exists($key, $item)) { |
|
106
|
|
|
$item[$key.'_slug'] = $this->getNormalizer()->slugify($item[$key]); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $item; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function parser() |
|
114
|
|
|
{ |
|
115
|
|
|
$list = []; |
|
116
|
|
|
|
|
117
|
|
|
foreach ($this->parser_create() as $data) { |
|
118
|
|
|
if ($data['tag'] !== 'item') { |
|
119
|
|
|
continue; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$item = array_merge(['channel' => $this->channel], $this->parserItems($data)); |
|
123
|
|
|
|
|
124
|
|
|
$normalized = $this->getNormalizer()->normalizeArrayValues($this->schema->getKeys(), $item); |
|
125
|
|
|
|
|
126
|
|
|
$list[$item['id']] = $this->addSlugs($this->extended($normalized)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $list; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
protected function parser_create() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->parserFromFile($this->input); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: