GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( da8966...225c03 )
by Marceau
01:56
created

DatePickerField::dateFormatJs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 1
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Akibatech\Crud\Fields;
4
5
use Akibatech\Crud\Exceptions\InvalidArgumentException;
6
use Carbon\Carbon;
7
use Illuminate\Validation\Validator;
8
9
/**
10
 * Class TinymceField
11
 *
12
 * @package Akibatech\Crud\Fields
13
 */
14
class DatePickerField extends Field
15
{
16
    /**
17
     * @var string
18
     */
19
    const TYPE = 'date-picker';
20
21
    /**
22
     * @var string
23
     */
24
    protected $date_format = 'Y-m-d';
25
26
    /**
27
     * @var null|string
28
     */
29
    protected $date_min = null;
30
31
    /**
32
     * @var null|string
33
     */
34
    protected $date_max = null;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getViewName()
40
    {
41
        return 'crud::fields.date-picker';
42
    }
43
44
    /**
45
     * @param   string $format
46
     * @return  self
47
     */
48
    public function withDateFormat($format)
49
    {
50
        $this->date_format = $format;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param   string|Carbon $date
57
     * @return  self
58
     * @throws  InvalidArgumentException
59
     */
60 View Code Duplication
    public function withMinDate($date)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        if ($date instanceof Carbon)
63
        {
64
            $this->date_min = $date->format($this->date_format);
65
        }
66
        else if (is_string($date))
67
        {
68
            $this->date_min = $date;
69
        }
70
        else
71
        {
72
            throw new InvalidArgumentException("Min date must be a string or a Carbon instance.");
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param   string|Carbon $date
80
     * @return  self
81
     * @throws  InvalidArgumentException
82
     */
83 View Code Duplication
    public function withMaxDate($date)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        if ($date instanceof Carbon)
86
        {
87
            $this->date_max = $date->format($this->date_format);
88
        }
89
        else if (is_string($date))
90
        {
91
            $this->date_max = $date;
92
        }
93
        else
94
        {
95
            throw new InvalidArgumentException("Max date must be a string or a Carbon instance.");
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * @param   void
103
     * @return  array
104
     */
105
    public function getViewVariables()
106
    {
107
        $locale = config('app.locale');
108
109
        return [
110
            'date_format' => self::dateFormatJs($this->date_format),
111
            'date_min'    => $this->date_min,
112
            'date_max'    => $this->date_max,
113
            'date_locale' => $locale != 'en' ? $locale : false
114
        ];
115
    }
116
117
    /**
118
     * @param   Validator $validator
119
     * @return  Validator
120
     */
121
    public function beforeValidation(Validator $validator)
122
    {
123
        $rules = [];
124
125
        $rules[] = 'date_format:' . $this->date_format;
126
127 View Code Duplication
        if (!is_null($this->date_min))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
        {
129
            $date = Carbon::createFromFormat($this->date_format, $this->date_min)->subDay()->format($this->date_format);
130
            $rules[] = 'after:' . $date;
131
        }
132
133 View Code Duplication
        if (!is_null($this->date_max))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
        {
135
            $date = Carbon::createFromFormat($this->date_format, $this->date_max)->addDay()->format($this->date_format);
136
            $rules[] = 'before:' . $date;
137
        }
138
139
        $validator->mergeRules($this->identifier, $rules);
140
141
        return $validator;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function getCss()
148
    {
149
        return [
150
            'vendor/crud/fields/datepicker/bootstrap-datepicker3.min.css',
151
        ];
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function getScripts()
158
    {
159
        $locale = config('app.locale');
160
        $base = ['vendor/crud/fields/datepicker/bootstrap-datepicker.min.js'];
161
        $extend = [];
162
163
        if ($locale != 'fr')
164
        {
165
            $extend[] = "vendor/crud/fields/datepicker/bootstrap-datepicker.$locale.min.js";
166
        }
167
168
169
        return array_merge($base, $extend);
170
    }
171
172
    /**
173
     * Convert a PHP date format to a JS date format.
174
     *
175
     * @param   string  $format
176
     * @return  string
177
     */
178
    public static function dateFormatJs($format)
179
    {
180
        $replacements = [
181
            'd' => 'dd',
182
            'j' => 'd',
183
            'D' => 'D',
184
            'l' => 'DD',
185
            'm' => 'mm',
186
            'n' => 'm',
187
            'M' => 'M',
188
            'F' => 'MM',
189
            'y' => 'yy',
190
            'Y' => 'yyyy',
191
        ];
192
193
        foreach ($replacements as $old => $new)
194
        {
195
            $format = preg_replace("#($old){1}#", $new, $format);
196
        }
197
198
        return $format;
199
    }
200
}
201