src/Model/SKU.php
private function validateSKU(string $sku)
    {
        if (strlen($sku) == 0) {
            throw new SKUException("A SKU cannot be empty");
        }

        // check for white-space
        $containsWhitespace = preg_match($this->whiteSpacePattern, $sku) == 1;
        if ($containsWhitespace) {
            throw new SKUException(sprintf("A SKU cannot contain white space characters: \"%s\"", $sku));
        }

        // uppercase
        $containsUppercaseCharacters = preg_match($this->uppercaseCharactersPattern, $sku) == 1;
        if ($containsUppercaseCharacters) {
            throw new SKUException(sprintf("A SKU cannot contain uppercase characters: \"%s\"", $sku));
        }

        // check for invalid characters
        $containsInvalidCharacters = preg_match($this->invalidCharactersPattern, $sku) == 1;
        if ($containsInvalidCharacters) {
            throw new SKUException(sprintf("The SKU \"%s\" contains invalid characters. A SKU can only contain the following characters: a-z, 0-9 and -",
                $sku));
        }

        // check prefix
        $prefixMatches = [];
        $prefixContainsInvalidCharacters = preg_match($this->invalidPrefixCharacters, $sku, $prefixMatches) == 1;
        if ($prefixContainsInvalidCharacters) {
            throw new SKUException(sprintf("A SKU cannot start with the given characters: \"%s\"",
                implode("", $prefixMatches)));
        }

        // check suffix
        $suffixMatches = [];
        $suffixContainsInvalidCharacters = preg_match($this->invalidSuffixCharacters, $sku, $suffixMatches) == 1;
        if ($suffixContainsInvalidCharacters) {
            throw new SKUException(sprintf("A SKU cannot end with the given characters: \"%s\"",
                implode("", $suffixMatches)));
        }

        // check minimum length
        if (strlen($sku) < $this->minLength) {
            throw new SKUException(sprintf("The given SKU \"%s\" is too short. The minimum length for a SKU is: %s",
                $sku, $this->minLength));
        }

        // check maximum length
        if (strlen($sku) > $this->maxLength) {
            throw new SKUException(sprintf("The given SKU \"%s\" is too long (%s character). The maximum length for a SKU is: %s",
                strlen($sku), $sku, $this->maxLength));
        }
    }
private function validateSKU(string $sku)
    {
        if (strlen($sku) == 0) {
            throw new SKUException("A SKU cannot be empty");
        }

        // check for white-space
        $containsWhitespace = preg_match($this->whiteSpacePattern, $sku) == 1;
        if ($containsWhitespace) {
            throw new SKUException(sprintf("A SKU cannot contain white space characters: \"%s\"", $sku));
        }

        // uppercase
        $containsUppercaseCharacters = preg_match($this->uppercaseCharactersPattern, $sku) == 1;
        if ($containsUppercaseCharacters) {
            throw new SKUException(sprintf("A SKU cannot contain uppercase characters: \"%s\"", $sku));
        }

        // check for invalid characters
        $containsInvalidCharacters = preg_match($this->invalidCharactersPattern, $sku) == 1;
        if ($containsInvalidCharacters) {
            throw new SKUException(sprintf("The SKU \"%s\" contains invalid characters. A SKU can only contain the following characters: a-z, 0-9 and -",
                $sku));
        }

        // check prefix
        $prefixMatches = [];
        $prefixContainsInvalidCharacters = preg_match($this->invalidPrefixCharacters, $sku, $prefixMatches) == 1;
        if ($prefixContainsInvalidCharacters) {
            throw new SKUException(sprintf("A SKU cannot start with the given characters: \"%s\"",
                implode("", $prefixMatches)));
        }

        // check suffix
        $suffixMatches = [];
        $suffixContainsInvalidCharacters = preg_match($this->invalidSuffixCharacters, $sku, $suffixMatches) == 1;
        if ($suffixContainsInvalidCharacters) {
            throw new SKUException(sprintf("A SKU cannot end with the given characters: \"%s\"",
                implode("", $suffixMatches)));
        }

        // check minimum length
        if (strlen($sku) < $this->minLength) {
            throw new SKUException(sprintf("The given SKU \"%s\" is too short. The minimum length for a SKU is: %s",
                $sku, $this->minLength));
        }

        // check maximum length
        if (strlen($sku) > $this->maxLength) {
            throw new SKUException(sprintf("The given SKU \"%s\" is too long (%s character). The maximum length for a SKU is: %s",
                strlen($sku), $sku, $this->maxLength));
        }
    }
Validate the given SKU

@param string $sku A unique identifier for a product (e.g. "fancy-short-1")

@return void

@throws SKUException If the given $sku is invalid