1<?php
2
3
4namespace ComboStrap;
5
6
7class DataType
8{
9
10    /**
11     * The property name when the type value is persisted
12     */
13    public const PROPERTY_NAME = "type";
14
15
16    /**
17     * An object with several children metadata
18     * An entity
19     * A group of metadata
20     */
21    public const TABULAR_TYPE_VALUE = "tabular";
22    /**
23     * Text with carriage return
24     */
25    public const PARAGRAPH_TYPE_VALUE = "paragraph";
26    /**
27     * True/False
28     */
29    public const BOOLEAN_TYPE_VALUE = "boolean";
30
31    /**
32     * A couple of words without any carriage return
33     */
34    public const TEXT_TYPE_VALUE = "text";
35    /**
36     * Date Time
37     */
38    public const DATETIME_TYPE_VALUE = "datetime";
39    /**
40     * A string but in Json
41     */
42    public const JSON_TYPE_VALUE = "json";
43
44    /**
45     * Integer
46     */
47    public const INTEGER_TYPE_VALUE = "integer";
48
49
50    /**
51     * The constant value
52     */
53    public const TYPES = [
54        DataType::TEXT_TYPE_VALUE,
55        DataType::TABULAR_TYPE_VALUE,
56        DataType::DATETIME_TYPE_VALUE,
57        DataType::PARAGRAPH_TYPE_VALUE,
58        DataType::JSON_TYPE_VALUE,
59        DataType::BOOLEAN_TYPE_VALUE,
60    ];
61
62    /**
63     * @throws ExceptionCombo
64     */
65    public static function toInteger($targetValue): int
66    {
67        if (is_int($targetValue)) {
68            return $targetValue;
69        }
70        if (!is_string($targetValue) && !is_float($targetValue)) {
71            $varExport = var_export($targetValue, true);
72            throw new ExceptionCombo("The value passed is not a numeric/nor a string. We can not translate it to an integer. Value: $varExport");
73        }
74        /**
75         * Float 12.845 will return 12
76         */
77        $int = intval($targetValue);
78        if (
79            $int === 0 &&
80            "$targetValue" !== "0"
81        ) {
82            throw new ExceptionCombo("The value ($targetValue) can not be cast to an integer.");
83        }
84        return $int;
85    }
86
87    public static function toBoolean($value)
88    {
89        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
90    }
91
92    public static function toFloat($value): float
93    {
94        return floatval($value);
95    }
96
97
98}
99