Failed Conditions
Push — master ( 481c82...217341 )
by Ryan
02:09
created

DBCreateTable::__set()   A

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
/**
8
 * Class to help creating new tables
9
 *
10
 * @author Ryan Prather <[email protected]>
11
 */
12
class DBCreateTable
13
{
14
15
    /**
16
     * Private variable to store the name of the field to create
17
     *
18
     * @var string
19
     */
20
    private $data;
21
22
    /**
23
     * Constructor
24
     *
25
     * @param string $field
26
     * @param string $datatype
27
     */
28 1
    public function __construct(string $field, string $datatype)
29
    {
30 1
        $this->data['field'] = $field;
31 1
        $this->data['datatype'] = $datatype;
32 1
    }
33
34
    /**
35
     * Magic setter method
36
     *
37
     * @param string $name
38
     * @param string|int|null $value
39
     */
40 1
    public function __set(string $name, $value): DBCreateTable
41
    {
42 1
        if (in_array($name, ['field', 'datatype', 'default', 'option'])) {
43 1
            $this->data[$name] = $value;
44
        }
45
46 1
        return $this;
47
    }
48
49
    /**
50
     * Magic method to convert the class to a string
51
     *
52
     * @return string
53
     */
54 1
    public function __toString(): string
55
    {
56 1
        $default = (isset($this->data['default']) ? $this->data['default'] : null);
57 1
        $option = (isset($this->data['option']) ? $this->data['option'] : null);
58
59 1
        return "`{$this->data['field']}` {$this->data['datatype']}" . (!is_null($default) ? " {$default}" : null) . (!is_null($option) ? " {$option}" : null);
60
    }
61
}
62