HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/marisrental/boldt.tech/app/Komma/Kms/Core/Sequence/Parts/NumberPart.php
<?php


namespace App\Komma\Kms\Core\Sequence\Parts;
/**
 * Class NumberPart
 *
 * Defines a number part for a sequence
 * Though it represents a number, it usually will be
 * outputted as a string. This is because it has
 * a minimum length in characters. Example: "0001"
 *
 * @package App\Komma\Kms\Core\Sequence\Parts
 */
class NumberPart extends AbstractPart
{
    /** @var int The number to start with */
    protected $startsWith = 0;

    /** @var int The length of the number in characters */
    protected $length;

    /**
     * NumberPart constructor.
     *
     * @param int $length The length of the numbers in characters. Example, number 10 with length 4 will be noted as "0010" when length is 4.
     * @param string $name A name for the part, for example "Year" or "InvoiceNo"
     */
    public function __construct(int $length, $name = '')
    {
        $this->currentValue = 0;
        $this->name = $name;
        parent::__construct($length);
    }

    /**
     * Calculate the next value
     */
    public function next(): AbstractPart
    {
        $this->currentValue++;
        if(strlen(''.$this->currentValue) > $this->length) $this->overflownBecause = 'the NumberParts value did get bigger in length then "'.$this->length.'" ('.$this->currentValue.').';
        return $this;
    }

    /**
     * Initialize the part with (a part of) the given value.
     *
     * @param string $value
     * @param bool $mayOverflow
     * @return mixed
     */
    public function startingAt(string $value, $mayOverflow = false): AbstractPart
    {
        $this->currentValue = (!$mayOverflow) ? (int) substr($value, 0, $this->length) : $value;
        $this->startsWith = $this->currentValue;
        return $this;
    }

    /**
     * Return the current value of the part
     *
     * @return string
     */
    public function getValue(): string
    {
        $stringVal = str_pad((string) $this->currentValue, $this->length, '0', STR_PAD_LEFT);
        return $stringVal;
    }

    /**
     * Describes in human language what the part consists of.
     * End the description with a dot, not followed by a space.
     *
     * @return string
     */
    public function describe(): string
    {
        $digitOrDigits = $this->length == 1 ? 'digit' : 'digits';
        $name = $this->name == '' ?: ' ('.$this->name.')';

        return 'a number of '.$this->length.' '.$digitOrDigits.$name.'.';
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }
}