TYPO3  7.6
Renderer.php
Go to the documentation of this file.
1 <?php
2 
19 namespace cogpowered\FineDiff\Render;
20 
22 use InvalidArgumentException;
23 
24 abstract class Renderer implements RendererInterface
25 {
34  public function process($from_text, $opcodes)
35  {
36  // Validate opcodes
37  if (!is_string($opcodes) && !($opcodes instanceof OpcodesInterface)) {
38  throw new InvalidArgumentException('Invalid opcodes type');
39  } else {
40  $opcodes = ($opcodes instanceof OpcodesInterface) ? $opcodes->generate() : $opcodes;
41  }
42 
43  // Holds the generated string that is returned
44  $output = '';
45 
46  $opcodes_len = strlen($opcodes);
47  $from_offset = 0;
48  $opcodes_offset = 0;
49 
50  while ($opcodes_offset < $opcodes_len) {
51 
52  $opcode = substr($opcodes, $opcodes_offset, 1);
53  $opcodes_offset++;
54  $n = intval(substr($opcodes, $opcodes_offset));
55 
56  if ($n) {
57  $opcodes_offset += strlen(strval($n));
58  } else {
59  $n = 1;
60  }
61 
62  if ($opcode === 'c') {
63  // copy n characters from source
64  $data = $this->callback('c', $from_text, $from_offset, $n);
65  $from_offset += $n;
66  } else if ($opcode === 'd') {
67  // delete n characters from source
68  $data = $this->callback('d', $from_text, $from_offset, $n);
69  $from_offset += $n;
70  } else /* if ( $opcode === 'i' ) */ {
71  // insert n characters from opcodes
72  $data = $this->callback('i', $opcodes, $opcodes_offset + 1, $n);
73  $opcodes_offset += 1 + $n;
74  }
75 
76  $output .= $data;
77  }
78 
79  return $output;
80  }
81 }