aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/scssphp/source-span/src/SimpleSourceLocation.php
blob: 591f6e4b0c7f6e2991246f7c05cefed66efb9479 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php

namespace SourceSpan;

use League\Uri\Contracts\UriInterface;

final class SimpleSourceLocation extends SourceLocationMixin
{
    private readonly int $line;
    private readonly int $column;

    /**
     * Creates a new location indicating $offset within $sourceUrl.
     *
     * $line and $column default to assuming the source is a single line. This
     * means that $line defaults to 0 and $column defaults to $offset.
     */
    public function __construct(
        private readonly int $offset,
        private readonly ?UriInterface $sourceUrl = null,
        ?int $line = null,
        ?int $column = null,
    ) {
        $this->line = $line ?? 0;
        $this->column = $column ?? $offset;

        if ($offset < 0) {
            throw new \OutOfRangeException('Offset may not be negative.');
        }

        if ($line !== null && $line < 0) {
            throw new \OutOfRangeException('Line may not be negative.');
        }

        if ($column !== null && $column < 0) {
            throw new \OutOfRangeException('Column may not be negative.');
        }
    }

    public function getOffset(): int
    {
        return $this->offset;
    }

    public function getLine(): int
    {
        return $this->line;
    }

    public function getColumn(): int
    {
        return $this->column;
    }

    public function getSourceUrl(): ?UriInterface
    {
        return $this->sourceUrl;
    }
}