Completed
Push — master ( 3a5c6d...7f7360 )
by Emmanuel
02:10
created

Guesser::getColsTypeMapping()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 23
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.0
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2017 Emmanuel Dyan
10
 *
11
 * @package edyan/neuralyzer
12
 *
13
 * @license GNU General Public License v2.0
14
 *
15
 * @link https://github.com/edyan/neuralyzer
16
 */
17
18
namespace Inet\Neuralyzer;
19
20
use Inet\Neuralyzer\Exception\NeuralizerGuesserException;
21
22
/**
23
 * Guesser to map field type to Faker Class
24
 */
25
class Guesser implements GuesserInterface
26
{
27
28
    /**
29
     * Returns the version of your guesser
30
     *
31
     * @return string
32
     */
33
    public function getVersion(): string
34
    {
35
        return '1.0.0b';
36
    }
37
38
    /**
39
     * Returns an array of fieldName => Faker class
40
     *
41
     * @return array
42
     */
43
    public function getColsNameMapping(): array
44
    {
45
        // can contain regexp
46
        return [
47
            // Adress and coordinates
48
            '.*\..*street.*'              => ['method' => 'streetAddress'],
49
            '.*\..*postalcode.*'          => ['method' => 'postcode'],
50
            '.*\..*city.*'                => ['method' => 'city'],
51
            '.*\..*state.*'               => ['method' => 'state'],
52
            '.*\..*country.*'             => ['method' => 'country'],
53
            '.*\..*phone.*'               => ['method' => 'phoneNumber'],
54
55
            // Internet
56
            '.*\.email_address'           => ['method' => 'email'],
57
            '.*\.url'                     => ['method' => 'url'],
58
59
            // Text
60
            '.*\.(comments|description)'  => ['method' => 'sentence', 'params' => [20]],
61
62
            // Person
63
            '.*\.first_name'              => ['method' => 'firstName'],
64
            '.*\.last_name'               => ['method' => 'lastName'],
65
        ];
66
    }
67
68
    /**
69
     * Retruns an array of fieldType => Faker class
70
     *
71
     * @return array
72
     */
73
    public function getColsTypeMapping(): array
74
    {
75
        return [
76
            // Strings
77
            'char'      => ['method' => 'sentence', 'params' => [4]],
78
            'varchar'   => ['method' => 'sentence', 'params' => [4]],
79
80
            // Text & Blobs
81
            'text'      => ['method' => 'sentence', 'params' => [20]],
82
            'blob'      => ['method' => 'sentence', 'params' => [20]],
83
            'longtext'  => ['method' => 'sentence', 'params' => [70]],
84
            'longblob'  => ['method' => 'sentence', 'params' => [70]],
85
86
            // DateTime
87
            'date'      => ['method' => 'date', 'params' => ['Y-m-d', 'now']],
88
            'datetime'  => ['method' => 'date', 'params' => ['Y-m-d H:i:s', 'now']],
89
            'timestamp' => ['method' => 'date', 'params' => ['Y-m-d H:i:s', 'now']],
90
            'time'      => ['method' => 'date', 'params' => ['H:i:s', 'now']],
91
92
            // Integer
93
            'tinyint'   => ['method' => 'randomNumber', 'params' => [2]],
94
            'smallint'  => ['method' => 'randomNumber', 'params' => [4]],
95
            'mediumint' => ['method' => 'randomNumber', 'params' => [6]],
96
            'int'       => ['method' => 'randomNumber', 'params' => [9]],
97
            'bigint'    => ['method' => 'randomNumber', 'params' => [15]],
98
99
            // Decimal
100
            'float'     => ['method' => 'randomFloat', 'params' => [2, 0, 999999]],
101
            'decimal'   => ['method' => 'randomFloat', 'params' => [2, 0, 999999]],
102
            'double'    => ['method' => 'randomFloat', 'params' => [2, 0, 999999]],
103
        ];
104
    }
105
106
    /**
107
     * Will map cols first by looking for field name then by looking for field type
108
     * if the first returned nothing
109
     *
110
     * @param string $table
111
     * @param string $name
112
     * @param string $type
113
     * @param mixed  $len    Used to get options from enum (stored in length)
114
     *
115
     * @return array
116
     */
117
    public function mapCol(string $table, string $name, string $type, string $len = null): array
118
    {
119
        // Try to find by colsName
120
        $colsName = $this->getColsNameMapping();
121
        foreach ($colsName as $colRegex => $params) {
122
            preg_match("/^$colRegex\$/", $table. '.' . $name, $matches);
123
            if (!empty($matches)) {
124
                return $params;
125
            }
126
        }
127
128
        // Hardcoded types
129
        if ($type === 'enum') {
130
            return [
131
                'method' => 'randomElement',
132
                'params' => [explode("','", substr($len, 1, -1))]
133
            ];
134
        }
135
136
        // Try to find by fieldType
137
        $colsType = $this->getColsTypeMapping();
138
        if (!array_key_exists($type, $colsType)) {
139
            throw new NeuralizerGuesserException("Can't guess the type $type");
140
        }
141
142
        return $colsType[$type];
143
    }
144
}
145