. */ namespace App\Services\LogSystem; use Jfcherng\Diff\DiffHelper; class LogDiffFormatter { /** * Format the diff between the given data, depending on the type of the data. * If the diff is not possible, an empty string is returned. * @param $old_data * @param $new_data */ public function formatDiff($old_data, $new_data): string { if (is_string($old_data) && is_string($new_data)) { return $this->diffString($old_data, $new_data); } if (is_numeric($old_data) && is_numeric($new_data)) { return $this->diffNumeric($old_data, $new_data); } return ''; } private function diffString(string $old_data, string $new_data): string { return DiffHelper::calculate($old_data, $new_data, 'Combined', [ //Diff options 'context' => 2, ], [ //Render options 'detailLevel' => 'char', 'showHeader' => false, ]); } private function diffNumeric($old_data, $new_data): string { if ((!is_numeric($old_data)) || (!is_numeric($new_data))) { throw new \InvalidArgumentException('The given data is not numeric.'); } $difference = $new_data - $old_data; //Positive difference if ($difference > 0) { return sprintf('+%s', $difference); } elseif ($difference < 0) { return sprintf('%s', $difference); } else { return sprintf('%s', $difference); } } }