Skip to content

Instantly share code, notes, and snippets.

/FbesDiff.php Secret

Created November 3, 2016 10:48
Show Gist options
  • Save anonymous/3b018eab308eea686044421f51b64888 to your computer and use it in GitHub Desktop.
Save anonymous/3b018eab308eea686044421f51b64888 to your computer and use it in GitHub Desktop.
<?php
require_once 'finediff.php';
$isInFbes = false;
class FbesDiff extends FineDiff {
public static function renderToTextFromOpcodes( $from, $opcodes ) {
ob_start();
FbesDiff::renderFromOpcodes( $from, $opcodes, [ 'FbesDiff', 'renderToTextFromOpcode' ] );
return ob_get_clean();
}
/** @noinspection PhpUnusedPrivateMethodInspection */
public static function renderToTextFromOpcode( $opcode, $from, $from_offset, $from_len ) {
global $isInFbes;
// Filter out fbes_keep and fbes_remove tags:
// fbes_remove should remain in
// fbes_keep and its contents should remain in
$original = substr( $from, $from_offset, $from_len );
if ( $opcode === 'd' ) {
$fragments = preg_split( '@(?=\[\/?fbes_(remove|keep)])@i', $original );
foreach ( $fragments as $fragment ) {
if ( strcasecmp( '[fbes_keep]', $fragment ) == 0 ) {
$isInFbes = true;
echo $fragment;
} else if ( strcasecmp( '[/fbes_keep]', $fragment ) === 0 ) {
$isInFbes = false;
echo $fragment;
} else if ( preg_match( '@^\[\/?fbes_remove]$@i', $fragment ) ) {
echo $fragment;
} else if ( $isInFbes ) {
echo $fragment;
} else {
echo '';
}
}
}
// Vanilla functionality (that we can't reuse, since parent is private, NGH)
if ( $opcode === 'c' || $opcode === 'i' ) {
echo $original;
}
}
}
<?php
/**
* Created by PhpStorm.
* User: marks
* Date: 02/11/2016
* Time: 20:11
*/
require_once 'FbesDiff.php';
class FbesDiffTest extends PHPUnit_Framework_TestCase {
public function testNormal() {
$input = 'Potato. Potatyo. Potato.';
$opcodes = 'c7d9c8i9: Potayto.';
$target = 'Potato. Potato. Potayto.';
$rendered = FbesDiff::renderToTextFromOpcodes($input, $opcodes);
$this->assertEquals($target, $rendered);
}
public function testApply() {
$input = <<<ENDSTR
NEW DJS,NEW MUSIC!!![fbes_remove] (time schedule coming soon)[/fbes_remove][fbes_keep]
Times: blabla[/fbes_keep]
Ticket price: 12€ includes 2 shot tokens
ENDSTR;
$opcodes = 'c20d13c28d25c4d29c42i:.';
$target = <<<ENDSTR
NEW DJS,NEW MUSIC!!![fbes_remove] (time schedule coming soon)[/fbes_remove][fbes_keep]
Times: blabla[/fbes_keep]
Ticket price: 12€ includes 2 shot tokens.
ENDSTR;
// FbesDiff::renderFromOpcodes( $input, $opcodes, [ 'FbesDiff', 'renderToTextFromOpcode' ] );
$rendered = FbesDiff::renderToTextFromOpcodes($input, $opcodes);
$this->assertEquals($target, $rendered);
}
public function testOther() {
$input = <<<ENDSTR
➡️If you have any question please do not hesitate to contact us ([fbes_remove]https://www.facebook.com/messages/REDACTED[/fbes_remove][fbes_keep]hello@REDACTED.example[/fbes_keep])
ENDSTR;
$opcodes = 'c68d14c44d60ci6: TEST';
$target = <<<ENDSTR
➡️If you have any question please do not hesitate to contact us ([fbes_remove]https://www.facebook.com/messages/REDACTED[/fbes_remove][fbes_keep]hello@REDACTED.example[/fbes_keep]) TEST
ENDSTR;
$rendered = FbesDiff::renderToTextFromOpcodes($input, $opcodes);
$this->assertEquals($target, $rendered);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment