|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wambo\Catalog\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Wambo\Catalog\Exception\SlugException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Slug represents a human-readable, descriptive URL fragment for the product (e.g. |
|
9
|
|
|
* "fancy-t-shirt-1-with-ice-cream-pooping-unicorn") |
|
10
|
|
|
* |
|
11
|
|
|
* @package Wambo\Catalog\Model |
|
12
|
|
|
*/ |
|
13
|
|
|
class Slug |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string $whiteSpacePattern A regular expression that matches white-space characters |
|
17
|
|
|
*/ |
|
18
|
|
|
private $whiteSpacePattern = '/[\s]/'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string $invalidCharactersPattern A regular expression that matches all characters that are invalid for a |
|
22
|
|
|
* Slug |
|
23
|
|
|
*/ |
|
24
|
|
|
private $invalidCharactersPattern = '/[^\p{L}\w-_:.,+]/u'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int $minLength Defines the minimum length for a Slug |
|
28
|
|
|
*/ |
|
29
|
|
|
private $minLength = 2; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var int $maxLength Defines the maximum length of a Slug |
|
33
|
|
|
*/ |
|
34
|
|
|
private $maxLength = 64; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $slug; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Create a new SlugValidator instance. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $slug human-readable, descriptive URL fragment for the product (e.g. |
|
44
|
|
|
* "fancy-t-shirt-1-with-ice-cream-pooping-unicorn") |
|
45
|
|
|
* |
|
46
|
|
|
* @throws SlugException If the given $slug is invalid |
|
47
|
|
|
*/ |
|
48
|
45 |
|
public function __construct(string $slug) |
|
49
|
|
|
{ |
|
50
|
45 |
|
$this->validateSlug($slug); |
|
51
|
31 |
|
$this->slug = $slug; |
|
52
|
31 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get a string representation of the current Slug. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
5 |
|
public function __toString() |
|
60
|
|
|
{ |
|
61
|
5 |
|
return $this->slug; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Validate the given Slug |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $sku A unique identifier for a product (e.g. "fancy-short-1") |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
* |
|
71
|
|
|
* @throws SlugException If the given $sku is invalid |
|
72
|
|
|
*/ |
|
73
|
45 |
|
private function validateSlug(string $sku) |
|
74
|
|
|
{ |
|
75
|
45 |
|
if (strlen($sku) == 0) { |
|
76
|
1 |
|
throw new SlugException("A Slug cannot be empty"); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// check for white-space |
|
80
|
44 |
|
$containsWhitespace = preg_match($this->whiteSpacePattern, $sku) == 1; |
|
81
|
44 |
|
if ($containsWhitespace) { |
|
82
|
4 |
|
throw new SlugException(sprintf("A Slug cannot contain white space characters: \"%s\"", $sku)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// check for invalid characters |
|
86
|
40 |
|
$containsInvalidCharacters = preg_match($this->invalidCharactersPattern, $sku) == 1; |
|
87
|
40 |
|
if ($containsInvalidCharacters) { |
|
88
|
5 |
|
throw new SlugException(sprintf("The Slug \"%s\" contains invalid characters. A Slug can only contain the following characters: a-z, 0-9 and -", |
|
89
|
|
|
$sku)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// check minimum length |
|
93
|
35 |
|
if (strlen($sku) < $this->minLength) { |
|
94
|
3 |
|
throw new SlugException(sprintf("The given Slug \"%s\" is too short. The minimum length for a Slug is: %s", |
|
95
|
3 |
|
$sku, $this->minLength)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// check maximum length |
|
99
|
32 |
|
if (strlen($sku) > $this->maxLength) { |
|
100
|
1 |
|
throw new SlugException(sprintf("The given Slug \"%s\" is too long (%s character). The maximum length for a Slug is: %s", |
|
101
|
1 |
|
strlen($sku), $sku, $this->maxLength)); |
|
102
|
|
|
} |
|
103
|
31 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |