src/Koldy/Validator.php
protected function validateDecimal($value, string $parameter, array $args = [], array $rules = null, array $context = null): ?string
    {
        if (count($args) == 0) {
            throw new ValidatorConfigException("Validator 'decimal' must have argument in validator list for parameter {$parameter}");
        }

        $decimals = (int)$args[0];

        if ($decimals <= 0) {
            throw new ValidatorConfigException("Validator 'decimal' has invalid value which is lower or equal to zero; for parameter={$parameter}");
        }

        if ($decimals > 100) {
            throw new ValidatorConfigException("Validator 'decimal' can't have that large argument; parameter={$parameter}");
        }

        //$value = $this->getValue($parameter);

        if ($value === null) {
            return null;
        }

        if (!is_scalar($value)) {
            return Message::getMessage(Message::PRIMITIVE, [
                'param' => $parameter
            ]);
        }

        $value = trim((string)$value);

        if ($value === '') {
            return null;
        }

        if (!is_numeric($value)) {
            return Message::getMessage(Message::NUMERIC, [
                'param' => $parameter,
                'value' => $value
            ]);
        }

        $parts = explode('.', $value);

        if (count($parts) == 1) {
            return null; // value is not decimal, it's probably integer, so it's ok
        }

        $valueDecimals = $parts[1];

        if (strlen($valueDecimals) > $decimals) {
            return Message::getMessage(Message::DECIMAL, [
                'param' => $parameter,
                'value' => $value,
                'decimals' => $decimals
            ]);
        }

        return null;
    }
protected function validateDecimal($value, string $parameter, array $args = [], array $rules = null, array $context = null): ?string
    {
        if (count($args) == 0) {
            throw new ValidatorConfigException("Validator 'decimal' must have argument in validator list for parameter {$parameter}");
        }

        $decimals = (int)$args[0];

        if ($decimals <= 0) {
            throw new ValidatorConfigException("Validator 'decimal' has invalid value which is lower or equal to zero; for parameter={$parameter}");
        }

        if ($decimals > 100) {
            throw new ValidatorConfigException("Validator 'decimal' can't have that large argument; parameter={$parameter}");
        }

        //$value = $this->getValue($parameter);

        if ($value === null) {
            return null;
        }

        if (!is_scalar($value)) {
            return Message::getMessage(Message::PRIMITIVE, [
                'param' => $parameter
            ]);
        }

        $value = trim((string)$value);

        if ($value === '') {
            return null;
        }

        if (!is_numeric($value)) {
            return Message::getMessage(Message::NUMERIC, [
                'param' => $parameter,
                'value' => $value
            ]);
        }

        $parts = explode('.', $value);

        if (count($parts) == 1) {
            return null; // value is not decimal, it's probably integer, so it's ok
        }

        $valueDecimals = $parts[1];

        if (strlen($valueDecimals) > $decimals) {
            return Message::getMessage(Message::DECIMAL, [
                'param' => $parameter,
                'value' => $value,
                'decimals' => $decimals
            ]);
        }

        return null;
    }
protected function validateDecimal ( $ value , string $ parameter , array $ args = [ ] , array $ rules = null , array $ context = null ) : ? string { if ( count ( $ args ) == 0 ) { throw new ValidatorConfigException ( "Validator 'decimal' must have argument in validator list for parameter {$parameter}" ) ; } $ decimals = ( int ) $ args [ 0 ] ; if ( $ decimals <= 0 ) { throw new ValidatorConfigException ( "Validator 'decimal' has invalid value which is lower or equal to zero; for parameter={$parameter}" ) ; } if ( $ decimals > 100 ) { throw new ValidatorConfigException ( "Validator 'decimal' can't have that large argument; parameter={$parameter}" ) ; } //$value = $this->getValue($parameter); if ( $ value === null ) { return null ; } if ( ! is_scalar ( $ value ) ) { return Message :: getMessage ( Message :: PRIMITIVE , [ 'param' => $ parameter ] ) ; } $ value = trim ( ( string ) $ value ) ; if ( $ value === '' ) { return null ; } if ( ! is_numeric ( $ value ) ) { return Message :: getMessage ( Message :: NUMERIC , [ 'param' => $ parameter , 'value' => $ value ] ) ; } $ parts = explode ( '.' , $ value ) ; if ( count ( $ parts ) == 1 ) { return null ; // value is not decimal, it's probably integer, so it's ok } $ valueDecimals = $ parts [ 1 ] ; if ( strlen ( $ valueDecimals ) > $ decimals ) { return Message :: getMessage ( Message :: DECIMAL , [ 'param' => $ parameter , 'value' => $ value , 'decimals' => $ decimals ] ) ; } return null ; }
Validate if given value has equal or less number of required decimals

@param mixed $value
@param string $parameter
@param array $args
@param array $rules other rules

@return null|string
@throws ValidatorConfigException
@example 'param' => 'decimal:2'
Validate if given value has equal or less number of required decimals