aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/scssphp/source-span/src/SourceLocationMixin.php
blob: 1649a3d1db2a434f6f377da7ade6ac98e57c3d7c (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
<?php

namespace SourceSpan;

/**
 * A mixin for easily implementing {@see SourceLocation}.
 *
 * @internal
 */
abstract class SourceLocationMixin implements SourceLocation
{
    public function distance(SourceLocation $other): int
    {
        if (!Util::isSameUrl($this->getSourceUrl(), $other->getSourceUrl())) {
            throw new \InvalidArgumentException("Source URLs \"{$this->getSourceUrl()}\" and \"{$other->getSourceUrl()}\" don't match.");
        }

        return abs($this->getOffset() - $other->getOffset());
    }

    public function pointSpan(): SourceSpan
    {
        return new SimpleSourceSpan($this, $this, '');
    }

    public function compareTo(SourceLocation $other): int
    {
        if (!Util::isSameUrl($this->getSourceUrl(), $other->getSourceUrl())) {
            throw new \InvalidArgumentException("Source URLs \"{$this->getSourceUrl()}\" and \"{$other->getSourceUrl()}\" don't match.");
        }

        return $this->getOffset() - $other->getOffset();
    }
}