Completed
Push — master ( 677b17...0972fb )
by Ben
02:08
created

DateTime::output()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Benrowe\Formatter\Providers;
4
5
use Benrowe\Formatter\AbstractFormatterProvider;
6
use Carbon\Carbon;
7
use Carbon\CarbonInterval;
8
9
/**
10
 * DateTime Formatter
11
 *
12
 * Contains formatter for datetime values
13
 *
14
 * @package Benrowe\Formatter
15
 */
16
class DateTime extends AbstractFormatterProvider
17
{
18
    /**
19
     * Format the datetime as a date
20
     *
21
     * @param  mixed $value
22
     * @param  string $format
23
     * @return string
24
     */
25 1
    public function asDate($value, $format = 'Y-m-d')
26
    {
27 1
        return $this->output($value, $format);
28
    }
29
30
    /**
31
     * Format the datetime as a date
32
     *
33
     * @param  mixed $value
34
     * @param  string $format
35
     * @return string
36
     */
37 1
    public function asTime($value, $format = 'H:i:s')
38
    {
39 1
        return $this->output($value, $format);
40
    }
41
42
    /**
43
     * Format the datetime as a date
44
     *
45
     * @param  mixed $value
46
     * @param  string $format
47
     * @return string
48
     */
49 1
    public function asDateTime($value, $format = 'Y-m-d H:i:s')
50
    {
51 1
        return $this->output($value, $format);
52
    }
53
54
    /**
55
     * Take in a number of seconds and display that as a human readable amount of time
56
     *
57
     * @param  int $value
58
     * @return string
59
     * @todo implement
60
     */
61
    public function asHuman($value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        // $int = CarbonInterval::seconds($value);
64
65
        return '';
66
    }
67
68
    /**
69
     * Generate the output from the value
70
     *
71
     * @param  mixed $value the datetime value
72
     * @param  string $format format string
73
     * @return string
74
     */
75 3
    private function output($value, $format)
76
    {
77 3
        return $this
78 3
            ->normaliseValue($value)
79 3
            ->format($format);
80
    }
81
82
    /**
83
     * Convert the datetime input into a instance of Carbon
84
     *
85
     * @param  mixed $value the datetime value
86
     * @return Carbon
87
     */
88 3
    private function normaliseValue($value)
89
    {
90 3
        $carbon = $this->getCarbon();
91
        // sniff the value type
92 3
        if (is_int($value)) {
93 3
            return $carbon->createFromTimestamp($value);
94
        }
95
        return $carbon->parse($value);
96
    }
97
98
    /**
99
     * Get an instance of the carbon datetime handler
100
     *
101
     * @return Carbon
102
     * @throws FormatterException if carbon isn't available
103
     */
104 3
    private function getCarbon()
105
    {
106 3
        if (!class_exists('Carbon\Carbon')) {
107
            throw new FormatterException(
108
                'Unable to load the nesbot/carbon dependency.'
109
            );
110
        }
111 3
        return new Carbon;
112
    }
113
}
114