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 Copy implements OperationInterface
22{
23    /**
24     * @var int
25     */
26    protected $len;
27
28    /**
29     * Set the initial length.
30     *
31     * @param int $len Length of string.
32     */
33    public function __construct(int $len)
34    {
35        $this->len = $len;
36    }
37
38    public function getFromLen(): int
39    {
40        return $this->len;
41    }
42
43    public function getToLen(): int
44    {
45        return $this->len;
46    }
47
48    public function getOpcode(): string
49    {
50        if ($this->len === 1) {
51            return 'c';
52        }
53
54        return "c{$this->len}";
55    }
56
57    /**
58     * Increase the length of the string.
59     *
60     * @param int $size Amount to increase the string length by.
61     * @return int New length
62     */
63    public function increase(int $size): int
64    {
65        return $this->len += $size;
66    }
67}
68