Test Failed
Branch develop (6f7954)
by Andreas
05:13 queued 02:18
created

Slug::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 40
    public function __construct(string $slug)
49
    {
50 40
        $this->validateSlug($slug);
51 26
        $this->slug = $slug;
52 26
    }
53
54
    /**
55
     * Get a string representation of the current Slug.
56
     *
57
     * @return string
58
     */
59
    public function __toString()
60
    {
61
        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 40
    private function validateSlug(string $sku)
74
    {
75 40
        if (strlen($sku) == 0) {
76 1
            throw new SlugException("A Slug cannot be empty");
77
        }
78
79
        // check for white-space
80 39
        $containsWhitespace = preg_match($this->whiteSpacePattern, $sku) == 1;
81 39
        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 35
        $containsInvalidCharacters = preg_match($this->invalidCharactersPattern, $sku) == 1;
87 35
        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 30
        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 27
        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 26
    }
104
105
}