Skip to content

Commit a6c7981

Browse files
committed
Also allow building from an array directly
1 parent 25871cd commit a6c7981

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ SetBuilder::buildSet($myOriginalSet, './compressed.txt');
113113
// Encode/Compress with the prefix algorithm and gzip on top (the .gz suffix determines that):
114114
SetBuilder::buildSet($myOriginalSet, './compressed.gz');
115115

116+
// If your dictionary is not a line-feed separated file, but you have an array, you can also build it from an
117+
// array directly:
118+
SetBuilder::buildFromArray($myArray, './compressed.gz');
119+
116120
// You then ship either "compressed.txt" or "compressed.gz" with your application. Instantiating
117121
// is then done as follows:
118122
$set = new FastSet(__DIR__ . '/dict');

composer-dependency-analyser.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
4+
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;
5+
6+
return (new Configuration())
7+
->ignoreErrorsOnExtension('ext-zlib', [ErrorType::SHADOW_DEPENDENCY]) // Optional, only used when building/using gzip encoded files
8+
;

src/SetBuilder.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public static function buildSet(string $sourcePath, string $outputPath): void
2727
}
2828
fclose($inputHandle);
2929

30+
self::buildFromArray($terms, $outputPath);
31+
}
32+
33+
/**
34+
* @param array<string> $terms
35+
* @param string $outputPath If your output path ends on ".gz", the set will also get compressed on top.
36+
*/
37+
public static function buildFromArray(array $terms, string $outputPath): void
38+
{
3039
sort($terms, SORT_STRING);
3140

3241
$outputHandle = self::openForWritingPossiblyGzip($outputPath);
@@ -35,7 +44,7 @@ public static function buildSet(string $sourcePath, string $outputPath): void
3544

3645
foreach ($terms as $term) {
3746
$commonPrefixLength = self::commonPrefixByteLength($previousTerm, $term);
38-
$suffix = substr($term, $commonPrefixLength);
47+
$suffix = substr((string) $term, $commonPrefixLength);
3948

4049
// <prefixLen>\t<suffix>\n
4150
fwrite($outputHandle, (string) $commonPrefixLength);

0 commit comments

Comments
 (0)