DBCreateTable   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 13
c 3
b 1
f 1
dl 0
loc 56
ccs 16
cts 16
cp 1
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __toString() 0 6 5
A __set() 0 7 2
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