Skip to content
PHP Ecosystem
Explore

Lines

API reference › Scaffold › Merge › Lines

final classPHPForge\Scaffold\Merge\Lines
Sourcesrc/Merge/Lines.php

Splits, inspects and rebuilds text line by line while keeping the original end-of-line bytes.

MethodDescriptionDefined by
detectEol() staticReturns the dominant end-of-line sequence: "\r\n", "\r", or "\n" (also for content without one).Lines
endsWithEol() staticReturns whether the content ends with an end-of-line sequence.Lines
normalise() staticConverts "\r\n" and "\r" terminators to "\n".Lines
rebuild() staticRebuilds consumer segments verbatim with inserted lines and appended blocks, each block after one blank line.Lines
split() staticSplits content normalised to "\n" into lines, ignoring the terminator of the last line.Lines
splitRaw() staticSplits raw content into lines that keep their original terminator.Lines
public static function detectEol(string $content): string

Returns the dominant end-of-line sequence: "\r\n", "\r", or "\n" (also for content without one).

Parameters

ParameterTypeDescription
$contentstringContent to inspect.

Returns

string: "\r\n" when present, otherwise "\r" when present, otherwise "\n".

public static function endsWithEol(string $content): bool

Returns whether the content ends with an end-of-line sequence.

Parameters

ParameterTypeDescription
$contentstringContent to inspect.

Returns

bool: true when the content ends with "\n" or "\r".

public static function normalise(string $content): string

Converts "\r\n" and "\r" terminators to "\n".

Parameters

ParameterTypeDescription
$contentstringContent to normalise.

Returns

string: Content with "\n" terminators only.

public static function rebuild(list<array{string, string}> $segments, array<int, list<string>> $insertAfter, list<list<string>> $blocks, string $eol, bool $finalEol): string

Rebuilds consumer segments verbatim with inserted lines and appended blocks, each block after one blank line.

Parameters

ParameterTypeDescription
$segmentslist<array{string, string}>Consumer [text, terminator] segments.
$insertAfterarray<int, list<string>>Lines to insert after the segment with the given index; index -1 inserts before the first segment.
$blockslist<list<string>>Blocks of lines appended at the end, in order.
$eolstringEnd-of-line sequence used for inserted lines.
$finalEolboolWhether the result must end with an end-of-line sequence.

Returns

string: Rebuilt content.

public static function split(string $content): list<string>

Splits content normalised to "\n" into lines, ignoring the terminator of the last line.

Parameters

ParameterTypeDescription
$contentstringContent normalised to "\n" terminators.

Returns

list<string>: Lines; empty for empty content.

public static function splitRaw(string $content): list<array{string, string}>

Splits raw content into lines that keep their original terminator.

Parameters

ParameterTypeDescription
$contentstringContent with any mix of "\r\n", "\r" and "\n" terminators.

Returns

list<array{string, string}>: [text, terminator] pairs; the last terminator is empty when the content does not end with an end-of-line sequence.