Passed
Push — master ( 3ccc44...cef086 )
by Emmanuel
02:16 queued 15s
created

Guesser::mapCol()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 14
cts 14
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 7
nop 4
crap 5
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.1
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2018 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 Edyan\Neuralyzer;
19
20
use Edyan\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 47
    public function getVersion(): string
34
    {
35 47
        return '3.0';
36
    }
37
38
    /**
39
     * Returns an array of fieldName => Faker class
40
     *
41
     * @return array
42
     */
43 15
    public function getColsNameMapping(): array
44
    {
45
        // can contain regexp
46
        return [
47
            // Internet
48 15
            '.*email.*'                  => ['method' => 'email'],
49
            '.*url'                      => ['method' => 'url'],
50
51
            // Adress and coordinates
52
            '.*address.*'                => ['method' => 'streetAddress'],
53
            '.*street.*'                 => ['method' => 'streetAddress'],
54
            '.*postalcode.*'             => ['method' => 'postcode'],
55
            '.*city.*'                   => ['method' => 'city'],
56
            '.*state.*'                  => ['method' => 'state'],
57
            '.*country.*'                => ['method' => 'country'],
58
            '.*phone.*'                  => ['method' => 'phoneNumber'],
59
60
            // Text
61
            '.*\.(comments|description)' => ['method' => 'sentence', 'params' => [20]],
62
63
            // Person
64
            '.*first_?name'              => ['method' => 'firstName'],
65
            '.*last_?name'               => ['method' => 'lastName'],
66
        ];
67
    }
68
69
70
    /**
71
     * Returns an array of fieldType => Faker method
72
     * @param  mixed $length  Field's length
73
     *
74
     * @return array
75
     */
76 9
    public function getColsTypeMapping($length): array
77
    {
78
        return [
79
            // Strings
80 9
            'string'     => ['method' => 'sentence', 'params' => [$length]],
81
82
            // Text & Blobs
83
            'text'       => ['method' => 'sentence', 'params' => [20]],
84
            'blob'       => ['method' => 'sentence', 'params' => [20]],
85
86
            // DateTime
87
            'date'       => ['method' => 'date',     'params' => ['Y-m-d']],
88
            'datetime'   => ['method' => 'date',     'params' => ['Y-m-d H:i:s']],
89
            'time'       => ['method' => 'time',     'params' => ['H:i:s']],
90
91
            // Integer
92
            'boolean'    => ['method' => 'boolean',      'params' => [4]],
93
            'smallint'   => ['method' => 'randomNumber', 'params' => [4]],
94
            'integer'    => ['method' => 'randomNumber', 'params' => [9]],
95 9
            'bigint'     => ['method' => 'randomNumber', 'params' => [strlen(mt_getrandmax()) - 1]],
96
97
            // Decimal
98
            'float'      => ['method' => 'randomFloat', 'params' => [2, 0, 999999]],
99
            'decimal'    => ['method' => 'randomFloat', 'params' => [2, 0, 999999]],
100
        ];
101
    }
102
103
    /**
104
     * Will map cols first by looking for field name then by looking for field type
105
     * if the first returned nothing
106
     *
107
     * @param string $table
108
     * @param string $name
109
     * @param string $type
110
     * @param mixed  $len    Used to get options from enum (stored in length)
111
     *
112
     * @return array
113
     */
114 14
    public function mapCol(string $table, string $name, string $type, string $len = null): array
115
    {
116
        // Try to find by colsName
117 14
        $colsName = $this->getColsNameMapping();
118 14
        foreach ($colsName as $colRegex => $params) {
119 14
            preg_match("/^$colRegex\$/i", $table. '.' . $name, $matches);
120 14
            if (!empty($matches)) {
121 14
                return $params;
122
            }
123
        }
124
125
        // Hardcoded types
126 9
        if ($type === 'enum') {
127
            return [
128 1
                'method' => 'randomElement',
129 1
                'params' => [explode("','", substr($len, 1, -1))]
130
            ];
131
        }
132
133
        // Try to find by fieldType
134 8
        $colsType = $this->getColsTypeMapping($len);
135 8
        if (!array_key_exists($type, $colsType)) {
136 1
            $msg = "Can't guess the type $type ({$table}.{$name})";
137 1
            throw new NeuralizerGuesserException($msg);
138
        }
139
140 7
        return $colsType[$type];
141
    }
142
}
143