-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUrl.php
64 lines (48 loc) · 1.84 KB
/
Url.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace MageSuite\SeoLinkMasking\Helper;
class Url extends \Magento\Framework\App\Helper\AbstractHelper
{
const DEFAULT_SPACE_REPLACEMENT_CHAR = '+';
/**
* @var \MageSuite\SeoLinkMasking\Helper\Configuration
*/
protected $configuration;
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\MageSuite\SeoLinkMasking\Helper\Configuration $configuration
) {
parent::__construct($context);
$this->configuration = $configuration;
}
public function encodeValue($value)
{
if (empty($value)) {
return $value;
}
$value = strtolower($value);
$value = $this->removeExcludedCharacters($value);
$value = urlencode($value);
$spaceReplacementCharacter = $this->configuration->getSpaceReplacementCharacter();
if (empty($spaceReplacementCharacter) || $spaceReplacementCharacter == self::DEFAULT_SPACE_REPLACEMENT_CHAR) {
return $value;
}
return str_replace(self::DEFAULT_SPACE_REPLACEMENT_CHAR, $spaceReplacementCharacter, $value);
}
public function decodeValue($value)
{
$spaceReplacementCharacter = $this->configuration->getSpaceReplacementCharacter();
if ($spaceReplacementCharacter && $spaceReplacementCharacter != self::DEFAULT_SPACE_REPLACEMENT_CHAR) {
$value = str_replace($spaceReplacementCharacter, self::DEFAULT_SPACE_REPLACEMENT_CHAR, $value);
}
return urldecode($value);
}
protected function removeExcludedCharacters($value)
{
$excludedCharacters = $this->configuration->getExcludedCharacters();
if (empty($excludedCharacters)) {
return $value;
}
$result = str_replace($excludedCharacters, '', $value);
return preg_replace('!\s+!', ' ', $result);
}
}