DBCreateTable::__set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Godsgood33\Php_Db;
6
7
use InvalidArgumentException;
8
9
/**
10
 * Class to help creating new tables
11
 *
12
 * @author Ryan Prather <[email protected]>
13
 *
14
 * @property string $field
15
 * @property string $datatype
16
 * @property string $default
17
 * @property string $option
18
 */
19
class DBCreateTable
20
{
21
22
    /**
23
     * Private variable to store the name of the field to create
24
     *
25
     * @var array
26
     */
27
    private array $data;
28
29
    /**
30
     * Constructor
31
     *
32
     * @param string $field
33
     * @param string $datatype
34
     * @param string $default
35
     * @param string $option
36
     */
37 3
    public function __construct(string $field, string $datatype, ?string $default = null, ?string $option = null)
38
    {
39 3
        $this->data = [
40 3
            'field' => $field,
41 3
            'datatype' => $datatype,
42 3
            'default' => $default,
43 3
            'option' => $option
44
        ];
45 3
    }
46
47
    /**
48
     * Magic setter method
49
     *
50
     * @param string $name
51
     * @param string|int|null $value
52
     *
53
     * @throws InvalidArgumentException
54
     */
55 2
    public function __set(string $name, $value)
56
    {
57 2
        if (!in_array($name, ['field', 'datatype', 'default', 'option'])) {
58 1
            throw new InvalidArgumentException("Invalid property in CreateTable");
59
        }
60
61 1
        $this->data[$name] = $value;
62 1
    }
63
64
    /**
65
     * Magic method to convert the class to a string
66
     *
67
     * @return string
68
     */
69 2
    public function __toString(): string
70
    {
71 2
        $default = (isset($this->data['default']) ? $this->data['default'] : null);
72 2
        $option = (isset($this->data['option']) ? $this->data['option'] : null);
73
74 2
        return "`{$this->data['field']}` {$this->data['datatype']}" . ($default !== null ? " DEFAULT '{$default}'" : null) . (!is_null($option) ? " {$option}" : null);
75
    }
76
}
77