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\Normalizer; |
16
|
|
|
|
17
|
|
|
use Cocur\Slugify\Slugify; |
18
|
|
|
|
19
|
|
|
abstract class NormalizerAbstract |
20
|
|
|
{ |
21
|
|
|
public function normalize($field, $value) |
22
|
|
|
{ |
23
|
|
|
$methodName = 'normalize'.ucfirst($field); |
24
|
|
|
|
25
|
|
|
if (method_exists($this, $methodName)) { |
26
|
|
|
$value = $this->$methodName($value); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return trim($value); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function normalizeArrayValues(Array $keys, Array $item) |
33
|
|
|
{ |
34
|
|
|
foreach ($keys as $field) { |
35
|
|
|
if (array_key_exists($field, $item)) { |
36
|
|
|
$item[$field] = $this->normalize($field, $item[$field]); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $item; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function normalizeLink($url) |
44
|
|
|
{ |
45
|
|
|
$parse = parse_url($url); |
46
|
|
|
|
47
|
|
|
if (array_key_exists('query', $parse)) { |
48
|
|
|
parse_str($parse['query'], $params); |
49
|
|
|
|
50
|
|
|
$blackList = [ |
51
|
|
|
'utm_campaign', |
52
|
|
|
'utm_source', |
53
|
|
|
'utm_medium', |
54
|
|
|
'utm_term', |
55
|
|
|
'utm_item', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
foreach ($blackList as $key) { |
59
|
|
|
unset($params[$key]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$newUrl = ''; |
64
|
|
|
|
65
|
|
|
if (array_key_exists('scheme', $parse)) { |
66
|
|
|
$newUrl .= $parse['scheme'].'://'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (array_key_exists('host', $parse)) { |
70
|
|
|
$newUrl .= $parse['host']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$newUrl .= $parse['path']; |
74
|
|
|
|
75
|
|
|
if (!empty($params)) { |
76
|
|
|
$newUrl .= '?'.http_build_query($params); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $newUrl; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function normalizeColor($value) |
83
|
|
|
{ |
84
|
|
|
return $this->nullToUndefined($value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function nullToUndefined($value) |
88
|
|
|
{ |
89
|
|
|
if (empty($value)) { |
90
|
|
|
return 'undefined'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $value; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected $slugifyTool; |
97
|
|
|
|
98
|
|
|
public function slugify($value) |
99
|
|
|
{ |
100
|
|
|
if (!$this->slugifyTool) { |
101
|
|
|
$this->slugifyTool = new Slugify(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->slugifyTool->slugify($value); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|