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