FieldsSizeValidator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 77
ccs 9
cts 9
cp 1
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A event_id() 0 7 3
A useLimit() 0 5 1
A __call() 0 18 5
1
<?php
2
3
4
namespace LaravelSixConnex;
5
6
use Illuminate\Support\Str;
7
use LaravelSixConnex\Exceptions\SixConnexFiledSizeException;
8
9
/**
10
 * Class FieldsSizeValidator
11
 * @package LaravelSixConnex
12
 *
13
 * @method string default( $value )
14
 * @method string firstname( $value )
15
 * @method string lastname( $value )
16
 * @method string email( $value )
17
 * @method string password( $value )
18
 * @method string title( $value )
19
 * @method string company( $value )
20
 * @method string profile_image( $value )
21
 * @method string language( $value )
22
 * @method string entitlement_group( $value )
23
 * @method string registration_set( $value )
24
 * @method string address1( $value )
25
 * @method string address2( $value )
26
 * @method string zipcode( $value )
27
 * @method string city( $value )
28
 * @method string state_province( $value )
29
 * @method string country( $value )
30
 * @method string country_code( $value )
31
 * @method string area_code( $value )
32
 * @method string phone_no( $value )
33
 * @method string extension( $value )
34
 * @method string promo_code( $value )
35
 */
36
37
class FieldsSizeValidator
38
{
39
    protected $useLimit = false;
40
41
    protected $fields = [
42
        'default'           => 64,
43
        'firstname'         => 64,
44
        'lastname'          => 64,
45
        'email'             => 64,
46
        'password'          => 30,
47
        'title'             => 64,
48
        'company'           => 64,
49
        'profile_image'     => 512,
50
        'language'          => 32,
51
        'entitlement_group' => 128,
52
        'registration_set'  => 80,
53
        'address1'          => 512,
54
        'address2'          => 512,
55
        'zipcode'           => 16,
56
        'city'              => 64,
57
        'state_province'    => 64,
58
        'country'           => 128,
59
        'country_code'      => 20, // This is the phone number’s country code, not the ISO code of country field.
60
        'area_code'         => 20,
61
        'phone_no'          => 20,
62
        'extension'         => 20,
63
        'promo_code'        => 255,
64
    ];
65
66
    /**
67
     * FieldsSizeValidator constructor.
68
     *
69
     * @param bool $useLimit
70
     */
71 5
    public function __construct(bool $useLimit = false)
72
    {
73 5
        $this->useLimit = $useLimit;
74
    }
75
76 1
    public function useLimit(bool $useLimit = false)
77
    {
78 1
        $this->useLimit = $useLimit;
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * @codeCoverageIgnore
85
     */
86
    public function __call($name, $arguments)
87
    {
88
        if (array_key_exists($name, $this->fields)) {
89
            if (strlen($arguments[0]) > $this->fields[ $name ]) {
90
                if ($this->useLimit && !in_array($name, [
91
                        'email',
92
                        'password',
93
                    ])) {
94
                    return Str::limit($arguments[0], $this->fields[ $name ], '');
95
                }
96
97
                throw new SixConnexFiledSizeException("$name field not valid");
98
            }
99
100
            return $arguments[0];
101
        }
102
103
        throw new \BadMethodCallException("Method $name not exists");
104
    }
105
106
107 1
    public function event_id($id)
108
    {
109 1
        if (!is_numeric($id) || $id <= 0) {
110 1
            throw new SixConnexFiledSizeException('Id field not valid');
111
        }
112
113 1
        return (int) $id;
114
    }
115
}
116