Completed
Push — master ( 92b395...0143b5 )
by Dmitry
02:11
created

IntlFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace hiqdev\php\units\yii2\formatters;
4
5
use hiqdev\php\units\FormatterInterface;
6
use hiqdev\php\units\formatters\SimpleFormatter;
7
use hiqdev\php\units\Quantity;
8
use hiqdev\php\units\Unit;
9
use Yii;
10
use yii\i18n\Formatter;
11
12
/**
13
 * Class IntlFormatter
14
 *
15
 * @author Dmytro Naumenko <[email protected]>
16
 */
17
class IntlFormatter implements FormatterInterface
18
{
19
    /**
20
     * @var SimpleFormatter
21
     */
22
    protected $simpleFormatter;
23
    /**
24
     * @var Formatter
25
     */
26
    private $formatter;
27
28
    public function __construct(Formatter $formatter)
29
    {
30
        $this->formatter = $formatter;
31
        $this->simpleFormatter = new SimpleFormatter();
32
    }
33
34
    /**
35
     * @param Quantity $quantity
36
     * @return string
37
     */
38
    public function format(Quantity $quantity): string
39
    {
40
        $format = $this->formatter;
41
        $format->sizeFormatBase = 1000;
42
43
        // TODO: split to different classes
44
        switch ($quantity->getUnit()->getMeasure()) {
45
            case 'bit':
46
                return $format->asShortSize($quantity->convert(Unit::create('byte'))->getQuantity(), 2);
47
            case 'bps':
48
                return Yii::t('hiqdev.units', '{quantity, number} {unit}', [
49
                    'quantity' => $quantity->getQuantity(),
50
                    'unit' => (function (string $unitName) {
51
                        switch ($unitName) {
52
                            case 'bps': return Yii::t('hiqdev.units', 'b/s');
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
53
                            case 'kbps': return Yii::t('hiqdev.units', 'Kb/s');
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
54
                            case 'mbps': return Yii::t('hiqdev.units', 'Mb/s');
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
55
                            case 'gbps': return Yii::t('hiqdev.units', 'Gb/s');
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
56
                        }
57
                        return Yii::t('hiqdev.units', '');
58
                    })($quantity->getUnit()->getName())
59
                ]);
60
            case 'item':
61
                return Yii::t('hiqdev.units', '{quantity, plural, one{# item} other{# items}}', ['quantity' => $quantity->getQuantity()]);
62
        }
63
64
        return $this->simpleFormatter->format($quantity);
65
    }
66
}
67