Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace Packback\Lti1p3\DeepLinkResources;
4
 
5
use DateTime;
6
use Packback\Lti1p3\Concerns\Arrayable;
7
use Packback\Lti1p3\LtiException;
8
 
9
class DateTimeInterval
10
{
11
    use Arrayable;
12
    public const ERROR_NO_START_OR_END = 'Either a start or end time must be specified.';
13
    public const ERROR_START_GT_END = 'The start time cannot be greater than end time.';
14
 
15
    public function __construct(
16
        private ?DateTime $start = null,
17
        private ?DateTime $end = null
18
    ) {
19
        $this->validateStartAndEnd();
20
    }
21
 
22
    public static function new(): self
23
    {
24
        return new DateTimeInterval();
25
    }
26
 
27
    public function getArray(): array
28
    {
29
        if (!isset($this->start) && !isset($this->end)) {
30
            throw new LtiException(self::ERROR_NO_START_OR_END);
31
        }
32
 
33
        $this->validateStartAndEnd();
34
 
35
        return [
36
            'startDateTime' => $this->start?->format(DateTime::ATOM),
37
            'endDateTime' => $this->end?->format(DateTime::ATOM),
38
        ];
39
    }
40
 
41
    public function setStart(?DateTime $start): self
42
    {
43
        $this->start = $start;
44
 
45
        return $this;
46
    }
47
 
48
    public function getStart(): ?DateTime
49
    {
50
        return $this->start;
51
    }
52
 
53
    public function setEnd(?DateTime $end): self
54
    {
55
        $this->end = $end;
56
 
57
        return $this;
58
    }
59
 
60
    public function getEnd(): ?DateTime
61
    {
62
        return $this->end;
63
    }
64
 
65
    /**
66
     * @throws LtiException
67
     */
68
    private function validateStartAndEnd(): void
69
    {
70
        if (isset($this->start) && isset($this->end) && $this->start > $this->end) {
71
            throw new LtiException(self::ERROR_START_GT_END);
72
        }
73
    }
74
}