Number::zeroFill()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3
1
<?php
2
namespace MrPrompt\ShipmentCommon\Util;
3
4
/**
5
 * Class Number
6
 * @package Centercob\Common\Util
7
 *
8
 * @author Walter Discher Cechinel <[email protected]>
9
 */
10
abstract class Number
11
{
12
    const FILL_LEFT  = 0;
13
    const FILL_RIGHT = 1;
14
15
    /**
16
     * @param $value
17
     * @param $length
18
     * @param int $fillSide
19
     * @return string
20
     * @throws \InvalidArgumentException
21
     */
22 6
    public static function zeroFill($value = '', $length = 0, $fillSide = self::FILL_LEFT)
23
    {
24 6
        $valueLength = strlen($value);
25
26 6
        if ($valueLength == $length) {
27 1
            return $value;
28
        }
29
30 5
        if ($valueLength > $length) {
31 1
            throw new \InvalidArgumentException(sprintf(
32 1
                'The given value (%s) is invalid',
33 1
                $value
34
            ));
35
        }
36
37 4
        return str_pad($value, $length, 0, $fillSide);
38
    }
39
}
40