Stevebauman\Inventory\Traits\InventoryTrait::generateSku PHP Метод

generateSku() публичный Метод

If an item already has an SKU, the SKU record will be returned. If an item does not have a category, it will return false.
public generateSku ( ) : boolean | mixed
Результат boolean | mixed
    public function generateSku()
    {
        // Make sure sku generation is enabled and the item has a category, if not we'll return false.
        if (!$this->skusEnabled() || !$this->hasCategory()) {
            return false;
        }
        // If the item already has an SKU, we'll return it
        if ($this->hasSku()) {
            return $this->sku;
        }
        // Get the set SKU code length from the configuration file
        $codeLength = Config::get('inventory.sku_code_length');
        // Get the set SKU prefix length from the configuration file
        $prefixLength = Config::get('inventory.sku_prefix_length');
        // Get the set SKU separator
        $skuSeparator = Config::get('inventory.sku_separator');
        // Make sure we trim empty spaces in the separator if
        // it's a string, otherwise we'll set it to NULL
        $skuSeparator = is_string($skuSeparator) ? trim($skuSeparator) : null;
        // Trim the category name to remove blank spaces, then grab
        // the first 3 letters of the string, and uppercase them
        $prefix = strtoupper(substr(trim($this->category->getAttribute('name')), 0, intval($prefixLength)));
        // We'll make sure the prefix length is greater
        // than zero before we try and generate an SKU
        if (strlen($prefix) > 0) {
            // Create the numerical code by the items ID to
            // accompany the prefix and pad left zeros
            $code = str_pad($this->getKey(), $codeLength, '0', STR_PAD_LEFT);
            // Return and process the generation
            return $this->processSkuGeneration($this->getKey(), $prefix . $skuSeparator . $code);
        }
        // Always return false on generation failure
        return false;
    }