Passed
Push — master ( 17e6cc...350d8b )
by Aleksandr
41:29 queued 06:24
created

ValueParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 19
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 14 7
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 * @author    Alex Kuperwood <[email protected]>
5
 * @copyright 2025 Alex Kuperwood
6
 * @license   https://opensource.org/license/mit  The MIT License
7
 */
8
9
namespace Kuperwood\Eav\Value;
10
11
use DateTime;
12
use Kuperwood\Eav\Enum\ATTR_TYPE;
13
use InvalidArgumentException;
14
15
class ValueParser
16
{
17
    /**
18
     * @throws \Exception
19
     */
20 1
    public function parse($type, $value)
21
    {
22
        switch ($type) {
23
            case ATTR_TYPE::INTEGER:
24
            case ATTR_TYPE::STRING:
25
            case ATTR_TYPE::TEXT:
26
            case ATTR_TYPE::MANUAL:
27 1
                return $value;
28
            case ATTR_TYPE::DATETIME:
29 1
                return (new DateTime($value))->format('Y-m-d H:i:s');
30
            case ATTR_TYPE::DECIMAL:
31 1
                return rtrim(rtrim($value, '0'), '.');
32
            default:
33
                throw new InvalidArgumentException("Unknown attribute type: " . $type);
34
        }
35
    }
36
}