Passed
Push — master ( f47009...00a57d )
by belamov
02:28
created

TimestampRange::__toString()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Belamov\PostgresRange\Ranges;
4
5
use Carbon\CarbonImmutable;
6
7
/**
8
 * Class TimestampRange.
9
 *
10
 * @method CarbonImmutable|null from()
11
 * @method CarbonImmutable|null to()
12
 */
13
class TimestampRange extends Range
14
{
15
    use StringifiesBoundariesFromDateTimeInterface;
16
17
    /**
18
     * @param  string  $boundary
19
     * @return CarbonImmutable
20
     */
21
    protected function transformBoundary(string $boundary): CarbonImmutable
22
    {
23
        return CarbonImmutable::parse(str_replace('"', '', $boundary));
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function getBoundaryFormat(): string
30
    {
31
        return 'Y-m-d H:i:s';
32
    }
33
34
    public function forSql(): string
35
    {
36
        return "'$this'::tsrange";
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function __toString()
43
    {
44
        return sprintf(
45
            '%s%s,%s%s',
46
            $this->fromBound,
47
            $this->from ? "\"{$this->from}\"" : '',
48
            $this->to ? "\"{$this->to}\"" : '',
49
            $this->toBound
50
        );
51
    }
52
}
53