1<?php
2
3declare(strict_types=1);
4
5/*
6 * FINE granularity DIFF
7 *
8 * (c) 2011 Raymond Hill (http://raymondhill.net/blog/?p=441)
9 * (c) 2013 Robert Crowe (http://cogpowered.com)
10 * (c) 2021 Christian Kuhn
11 *
12 * For the full copyright and license information, please view
13 * the LICENSE file that was distributed with this source code.
14 */
15
16namespace cogpowered\FineDiff\Parser\Operations;
17
18/**
19 * Generates the opcode for a copy operation.
20 */
21class Insert implements OperationInterface
22{
23    /**
24     * @var string
25     */
26    protected $text;
27
28    /**
29     * Sets the text that the operation is working with.
30     */
31    public function __construct(string $text)
32    {
33        $this->text = $text;
34    }
35
36    public function getFromLen(): int
37    {
38        return 0;
39    }
40
41    public function getToLen(): int
42    {
43        return mb_strlen($this->text);
44    }
45
46    public function getText(): string
47    {
48        return $this->text;
49    }
50
51    public function getOpcode(): string
52    {
53        $to_len = mb_strlen($this->text);
54        if ($to_len === 1) {
55            return "i:{$this->text}";
56        }
57        return "i{$to_len}:{$this->text}";
58    }
59}
60