Option::__get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of dimtrovich/db-dumper".
5
 *
6
 * (c) 2024 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\DbDumper;
13
14
final class Option
15
{
16
    // Same as mysqldump.
17
    public const MAXLINESIZE            = 1000000;
18
    public const CHARSET_UTF8           = 'utf8';
19
    public const CHARSET_UTF8MB4        = 'utf8mb4';
20
    public const CHARSET_BINARY         = 'binary';
21
    public const COMPRESSION_GZIP       = 'Gzip';
22
    public const COMPRESSION_BZIP2      = 'Bzip2';
23
    public const COMPRESSION_NONE       = 'None';
24
    public const COMPRESSION_GZIPSTREAM = 'GzipStream';
25
26
    public string $compress                 = self::COMPRESSION_NONE;
27
    public string $default_character_set    = self::CHARSET_UTF8;
28
    public string $where                    = '';
29
    public int $net_buffer_length           = self::MAXLINESIZE;
30
    public array $include_tables            = [];
31
    public array $exclude_tables            = [];
32
    public array $include_views             = [];
33
    public array $init_commands             = [];
34
    public array $no_data                   = [];
35
    public bool $if_not_exists              = false;
36
    public bool $reset_auto_increment       = false;
37
    public bool $add_drop_database          = false;
38
    public bool $add_drop_table             = false;
39
    public bool $add_drop_trigger           = true;
40
    public bool $add_locks                  = true;
41
    public bool $complete_insert            = false;
42
    public bool $databases                  = false;
43
    public bool $disable_keys               = true;
44
    public bool $extended_insert            = true;
45
    public bool $events                     = false;
46
    public bool $hex_blob                   = true;   // faster than escaped onent
47
    public bool $insert_ignore              = false;
48
    public bool $no_autocommit              = true;
49
    public bool $no_create_db               = false;
50
    public bool $no_create_info             = false;
51
    public bool $lock_tables                = true;
52
    public bool $routines                   = false;
53
    public bool $single_transaction         = true;
54
    public bool $skip_triggers              = false;
55
    public bool $skip_tz_utc                = false;
56
    public bool $skip_comments              = false;
57
    public bool $skip_dump_date             = false;
58
    public bool $skip_definer               = false;
59
    public bool $disable_foreign_keys_check = true;
60
61
    /**
62
     * Customised user message to be inserted in the header of the dumped file
63
     */
64
    public string $message = '';
65
66
    private array $options = [];
67
68
    public function __construct(array $options, string $driver = 'mysql')
69
    {
70
        $this->setOptions($options);
71
72
        if ($driver === 'mysql') {
73
            $this->init_commands[] = 'SET NAMES ' . $this->default_character_set;
74
75
            if (false === $this->skip_tz_utc) {
76
                $this->init_commands[] = "SET TIME_ZONE='+00:00'";
77
            }
78
        }
79
80
        // If no include-views is passed in, dump the same views as tables, mimic mysqldump behaviour.
81
        if ($this->include_views === []) {
82
            $this->include_views = $this->include_tables;
83
        }
84
85
        $this->disable_foreign_keys_check = true;
86
    }
87
88
    /**
89
     * Define backup options of database
90
     */
91
    public function setOptions(array $options = []): self
92
    {
93
        unset($options['disable_foreign_keys_check']);
94
95
        foreach ($options as $key => $val) {
96
            if (is_int($key)) {
97
                continue;
98
            }
99
100
            if (! property_exists($this, $key)) {
101
                $key = CaseConverter::toSnake($key);
102
            }
103
104
            if (property_exists($this, $key)) {
105
                if ($key === 'message' && $val !== '' && ! str_starts_with($val, '-- ')) {
106
                    $val = '-- ' . $val;
107
                }
108
109
                $this->{$key} = $val;
110
                unset($options[$key]);
111
            }
112
        }
113
114
        $this->options = array_map([CaseConverter::class, 'toSnake'], $options);
115
116
        return $this;
117
    }
118
119
    public function __get($name)
120
    {
121
        return $this->options[CaseConverter::toSnake($name)] ?? null;
122
    }
123
124
    public function __set($name, $value)
125
    {
126
        $this->options[CaseConverter::toSnake($name)] = $value;
127
    }
128
}
129