FormatDate::apply()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.7666
cc 3
nc 3
nop 2
1
<?php
2
/**
3
 * File copied from Waavi/Sanitizer https://github.com/waavi/sanitizer
4
 * Sanitization functionality to be customized within this project before a 1.0 release.
5
 */
6
7
namespace PerfectOblivion\Valid\Sanitizer\Filters;
8
9
use InvalidArgumentException;
10
use Illuminate\Support\Carbon;
11
use PerfectOblivion\Valid\Sanitizer\Contracts\Filter;
12
13
class FormatDate implements Filter
14
{
15
    /**
16
     * Format a date.
17
     *
18
     * @param  string  $value
19
     *
20
     * @return string
21
     */
22
    public function apply($value, $options = [])
23
    {
24
        if (! $value) {
25
            return $value;
26
        }
27
28
        if (count($options) != 2) {
29
            throw new InvalidArgumentException('The Sanitizer Format Date filter requires both the current date format as well as the target format.');
30
        }
31
32
        $currentFormat = trim($options[0]);
33
        $targetFormat = trim($options[1]);
34
35
        return Carbon::createFromFormat($currentFormat, $value)->format($targetFormat);
36
    }
37
}
38