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

ValueParser::parse()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 7.392

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 7
nop 2
dl 0
loc 14
ccs 4
cts 5
cp 0.8
crap 7.392
rs 8.8333
c 0
b 0
f 0
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
}