Passed
Push — master ( c103e2...560387 )
by Emmanuel
03:39
created

Guesser::getVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
crap 1
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 8
    public function getVersion(): string
34
    {
35 8
        return '1.0.0b';
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
            // Adress and coordinates
48 15
            '.*\..*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.*'                 => ['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 9
    public function getColsTypeMapping($length): array
74
    {
75
        return [
76
            // Strings
77 9
            'string'       => ['method' => 'sentence', 'params' => [$length]],
78
79
            // Text & Blobs
80
            'text'       => ['method' => 'sentence', 'params' => [20]],
81
            'blob'       => ['method' => 'sentence', 'params' => [20]],
82
83
            // DateTime
84
            'date'       => ['method' => 'datetime', 'params' => ['now']],
85
            'datetime'   => ['method' => 'datetime', 'params' => ['now']],
86
            'time'       => ['method' => 'datetime', 'params' => ['now']],
87
88
            // Integer
89
            'boolean'    => ['method' => 'boolean',      'params' => [4]],
90
            'smallint'   => ['method' => 'randomNumber', 'params' => [4]],
91
            'integer'    => ['method' => 'randomNumber', 'params' => [9]],
92 9
            'bigint'     => ['method' => 'randomNumber', 'params' => [strlen(mt_getrandmax()) - 1]],
93
94
            // Decimal
95
            'float'      => ['method' => 'randomFloat', 'params' => [2, 0, 999999]],
96
            'decimal'    => ['method' => 'randomFloat', 'params' => [2, 0, 999999]],
97
        ];
98
    }
99
100
    /**
101
     * Will map cols first by looking for field name then by looking for field type
102
     * if the first returned nothing
103
     *
104
     * @param string $table
105
     * @param string $name
106
     * @param string $type
107
     * @param mixed  $len    Used to get options from enum (stored in length)
108
     *
109
     * @return array
110
     */
111 14
    public function mapCol(string $table, string $name, string $type, string $len = null): array
112
    {
113
        // Try to find by colsName
114 14
        $colsName = $this->getColsNameMapping();
115 14
        foreach ($colsName as $colRegex => $params) {
116 14
            preg_match("/^$colRegex\$/", $table. '.' . $name, $matches);
117 14
            if (!empty($matches)) {
118 14
                return $params;
119
            }
120
        }
121
122
        // Hardcoded types
123 9
        if ($type === 'enum') {
124
            return [
125 1
                'method' => 'randomElement',
126 1
                'params' => [explode("','", substr($len, 1, -1))]
127
            ];
128
        }
129
130
        // Try to find by fieldType
131 8
        $colsType = $this->getColsTypeMapping($len);
132 8
        if (!array_key_exists($type, $colsType)) {
133 1
            throw new NeuralizerGuesserException("Can't guess the type $type");
134
        }
135
136 7
        return $colsType[$type];
137
    }
138
}
139