Skip to content

Commit 5e0f0da

Browse files
committed
Add DiffHelper::getStyleSheet()
Signed-off-by: Jack Cherng <[email protected]>
1 parent c7e5312 commit 5e0f0da

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

example/demo.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
<?php
2+
3+
include __DIR__ . '/../vendor/autoload.php';
4+
5+
use Jfcherng\Diff\DiffHelper;
6+
7+
?>
18
<!DOCTYPE html>
29
<html>
310
<head>
411
<meta charset="UTF-8" />
512
<title>jfcherng/php-diff - Examples</title>
6-
<link href="./diff-table.css" rel="stylesheet" type="text/css" />
13+
<style><?= DiffHelper::getStyleSheet() ?></style>
714
</head>
815
<body>
916
<?php
1017

11-
include __DIR__ . '/../vendor/autoload.php';
12-
13-
use Jfcherng\Diff\DiffHelper;
14-
1518
// include two sample files for comparison
1619
$oldFilePath = __DIR__ . '/old_file.txt';
1720
$newFilePath = __DIR__ . '/new_file.txt';

src/DiffHelper.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ public static function getAvailableTemplates(): array
7373
return \array_keys(self::getTemplatesInfo());
7474
}
7575

76+
/**
77+
* Get the content of the CSS style sheet for HTML templates.
78+
*
79+
* @throws \LogicException path is a directory
80+
* @throws \RuntimeException path cannot be opened
81+
*
82+
* @return string
83+
*/
84+
public static function getStyleSheet(): string
85+
{
86+
static $filePath = __DIR__ . '/../example/diff-table.css';
87+
88+
$cssFile = new \SplFileObject($filePath, 'r');
89+
90+
return $cssFile->fread($cssFile->getSize());
91+
}
92+
7693
/**
7794
* All-in-one static method to calculate the diff between two strings (or arrays of strings).
7895
*

tests/DiffHelperTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testCalculateUnified(string $old, string $new, array $expecteds)
8585
}
8686

8787
/**
88-
* Test the DiffHelper::calculate with the'Context' template.
88+
* Test the DiffHelper::calculate with the 'Context' template.
8989
*
9090
* @covers \Jfcherng\Diff\DiffHelper::calculate
9191
* @dataProvider calculateDataProvider
@@ -101,4 +101,14 @@ public function testCalculateContext(string $old, string $new, array $expecteds)
101101
DiffHelper::calculate($old, $new, 'Context')
102102
);
103103
}
104+
105+
/**
106+
* Test the DiffHelper::getStyleSheet.
107+
*
108+
* @covers \Jfcherng\Diff\DiffHelper::getStyleSheet
109+
*/
110+
public function testGetStyleSheet(): void
111+
{
112+
$this->assertIsString(DiffHelper::getStyleSheet());
113+
}
104114
}

0 commit comments

Comments
 (0)