ATTR_TYPE::randomValue()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.0702

Importance

Changes 0
Metric Value
cc 6
eloc 14
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 16
ccs 7
cts 8
cp 0.875
crap 6.0702
rs 9.2222
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
declare(strict_types=1);
9
10
namespace Kuperwood\Eav\Enum;
11
12
use DateTime;
13
14
use Kuperwood\Eav\Exception\AttributeException;
15
use Kuperwood\Eav\Validation\Constraints\DateConstraint;
16
use Kuperwood\Eav\Validation\Constraints\LengthConstraint;
17
use Kuperwood\Eav\Validation\Constraints\NumericConstraint;
18
use Kuperwood\Eav\Validation\Constraints\RegexConstraint;
19
use Faker\Factory;
20
use InvalidArgumentException;
21
22
class ATTR_TYPE
23
{
24
25
    public const INTEGER = "int";
26
    public const DATETIME = "datetime";
27
    public const DECIMAL = "decimal";
28
    public const STRING = "varchar";
29
    public const TEXT = "text";
30
    public const MANUAL = "manual";
31
32
    private static function getCases(): array
33
    {
34
        return [
35
            self::INTEGER,
36
            self::DATETIME,
37
            self::DECIMAL,
38
            self::STRING,
39
            self::TEXT
40
        ];
41
    }
42
43 1
    public static function isValid(string $type): bool
44
    {
45 1
        if ($type === self::MANUAL) {
46 1
            return false;
47
        }
48
49 1
        return in_array($type, self::getCases());
50
    }
51
52 1
    public static function valueTable(string $name): string
53
    {
54
        switch ($name) {
55
            case self::INTEGER:
56 1
                return sprintf(_VALUE::table(), self::INTEGER);
57
            case self::DATETIME:
58 1
                return sprintf(_VALUE::table(), self::DATETIME);
59
            case self::DECIMAL:
60 1
                return sprintf(_VALUE::table(), self::DECIMAL);
61
            case self::STRING:
62 1
                return sprintf(_VALUE::table(), self::STRING);
63
            case self::TEXT:
64 1
                return sprintf(_VALUE::table(), self::TEXT);
65
            default:
66
                throw new InvalidArgumentException("Invalid type: " . $name);
67
        }
68
    }
69
70 1
    public static function validationRule(string $name): array
71
    {
72
        switch ($name) {
73
            case self::INTEGER:
74 1
                return [
75 1
                    new NumericConstraint()
76 1
                ];
77
            case self::DATETIME:
78 1
                return [
79 1
                    new DateConstraint('Y-m-d H:i:s')
80 1
                ];
81
            case self::DECIMAL:
82 1
                return [
83 1
                    new RegexConstraint('/^[0-9]{1,21}(?:\.[0-9]{1,6})?$/')
84 1
                ];
85
            case self::STRING:
86 1
                return [
87 1
                    new LengthConstraint(1, 191)
88 1
                ];
89
            case self::TEXT:
90 1
                return [
91 1
                    new LengthConstraint(1, 10000)
92 1
                ];
93
            default:
94
                throw new InvalidArgumentException("Invalid type: " . $name);
95
        }
96
    }
97
98 1
    public static function randomValue(string $name)
99
    {
100 1
        $faker = Factory::create();
101
        switch ($name) {
102
            case self::INTEGER:
103 1
                return $faker->randomNumber();
104
            case self::DATETIME:
105 1
                return (new DateTime())->setTimestamp(rand(strtotime('1980-01-01 00:00:00'), strtotime('2025-12-31 23:59:59')))->format('Y-m-d H:i:s.u');
106
            case self::DECIMAL:
107 1
                return $faker->randomFloat(6);
108
            case self::STRING:
109 1
                return $faker->words(2, true);
110
            case self::TEXT:
111 1
                return $faker->text(150);
112
            default:
113
                throw new InvalidArgumentException("Invalid type: " . $name);
114
        }
115
    }
116
117
    /**
118
     * @throws AttributeException
119
     */
120 1
    public static function getCase(string $type): string
121
    {
122
        switch ($type) {
123
            case self::INTEGER:
124 1
                return self::INTEGER;
125
            case self::DATETIME:
126 1
                return self::DATETIME;
127
            case self::DECIMAL:
128 1
                return self::DECIMAL;
129
            case self::STRING:
130 1
                return self::STRING;
131
            case self::TEXT:
132 1
                return self::TEXT;
133
            default:
134 1
                throw AttributeException::unsupportedType($type);
135
        }
136
    }
137
}
138