Completed
Pull Request — master (#537)
by Daniel
04:10
created

DateDifference::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Services\Helper;
4
5
use Carbon\Carbon;
6
7
class DateDifference
8
{
9
    /** @var string */
10
    private $format;
11
    /** @var bool */
12
    private $diffForHumans;
13
14
    public function __construct(string $format, bool $diffForHumans)
15
    {
16
        $this->format = $format;
17
        $this->diffForHumans = $diffForHumans;
18
    }
19
20
    public function format(Carbon $earlier): string
21
    {
22
        if ($this->diffForHumans) {
23
            return $this->diffForHumans($earlier);
24
        }
25
        return $earlier->formatLocalized($this->format);
26
    }
27
28
    private function diffForHumans(Carbon $earlier): string
29
    {
30
        if ($earlier->diffInHours() < 1) {
31
            return $earlier->diffForHumans(null, true) . ' temu';
32
        }
33
        if ($earlier->isToday()) {
34
            return 'dziś, ' . $earlier->format('H:i');
35
        }
36
        if ($earlier->isYesterday()) {
37
            return 'wczoraj, ' . $earlier->format('H:i');
38
        }
39
        return $earlier->formatLocalized($this->format);
40
    }
41
}
42