BagItTools

BagUtils
in package

Utility class to hold static functions.

Tags
author

whikloj

since
1.0.0

Table of Contents

CHARACTER_SETS  = ["utf-8" => "UTF-8", "utf-16" => "UTF-16", "us-ascii" => "US-ASCII", "iso-8859-1" => "ISO-8859-1", "iso-8859-2" => "ISO-8859-2", "iso-8859-3" => "ISO-8859-3", "iso-8859-4" => "ISO-8859-4", "iso-8859-5" => "ISO-8859-5", "iso-8859-6" => "ISO-8859-6", "iso-8859-7" => "ISO-8859-7", "iso-8859-8" => "ISO-8859-8", "iso-8859-9" => "ISO-8859-9", "iso-8859-10" => "ISO-8859-10", "shift_jis" => "Shift_JIS", "euc-jp" => "EUC-JP", "iso-2022-kr" => "ISO-2022-KR", "euc-kr" => "EUC-KR", "iso-2022-jp" => "ISO-2022-JP", "iso-2022-jp-2" => "ISO-2022-JP-2", "iso-8859-6-e" => "ISO-8859-6-E", "iso-8859-6-i" => "ISO-8859-6-I", "iso-8859-8-e" => "ISO-8859-8-E", "iso-8859-8-i" => "ISO-8859-8-I", "gb2312" => "GB2312", "big5" => "Big5", "koi8-r" => "KOI8-R"]
Valid character set MIME names from IANA.
baseInData()  : string
Rebase the path in the data directory as payloads only deal in there.
checkedCopy()  : void
Copy a file and check that the copy succeeded.
checkedFilePut()  : int
Put contents to a file and check it succeeded.
checkedFwrite()  : void
Write to a file resource and check it succeeded.
checkedMkdir()  : void
Make a directory (or directories) and check it succeeds.
checkedTempnam()  : string
Create a temporary file and check it succeeded.
checkedUnlink()  : void
Delete a file/directory and check it succeeded.
checkUnencodedFilepath()  : bool
Check for unencoded newlines, carriage returns or % symbols in a file path.
decodeFilepath()  : string
Decode a file path according to the special rules of the spec.
encodeFilepath()  : string
Encode a file path according to the special rules of the spec.
findAllByPattern()  : array<string|int, mixed>
Return all files that match the pattern, or an empty array.
getAbsolute()  : string
There is a method that deal with Sven Arduwie proposal https://www.php.net/manual/en/function.realpath.php#84012 And runeimp at gmail dot com proposal https://www.php.net/manual/en/function.realpath.php#112367
getAllFiles()  : array<string|int, mixed>
Recursively list all files in a directory, except dot files.
getValidCharset()  : string|null
Check the provided lower case name of a character set against our list. If we have it, return the proper MIME name.
invalidPathCharacters()  : bool
Paths for new and existing files should not have these conditions.
isDotDir()  : bool
Utility to test a filename as . or .
splitFileDataOnLineEndings()  : array<string|int, mixed>
Split the file data on any of the allowed line endings.
__construct()  : mixed
BagUtils constructor.

Constants

CHARACTER_SETS

Valid character set MIME names from IANA.

private mixed CHARACTER_SETS = ["utf-8" => "UTF-8", "utf-16" => "UTF-16", "us-ascii" => "US-ASCII", "iso-8859-1" => "ISO-8859-1", "iso-8859-2" => "ISO-8859-2", "iso-8859-3" => "ISO-8859-3", "iso-8859-4" => "ISO-8859-4", "iso-8859-5" => "ISO-8859-5", "iso-8859-6" => "ISO-8859-6", "iso-8859-7" => "ISO-8859-7", "iso-8859-8" => "ISO-8859-8", "iso-8859-9" => "ISO-8859-9", "iso-8859-10" => "ISO-8859-10", "shift_jis" => "Shift_JIS", "euc-jp" => "EUC-JP", "iso-2022-kr" => "ISO-2022-KR", "euc-kr" => "EUC-KR", "iso-2022-jp" => "ISO-2022-JP", "iso-2022-jp-2" => "ISO-2022-JP-2", "iso-8859-6-e" => "ISO-8859-6-E", "iso-8859-6-i" => "ISO-8859-6-I", "iso-8859-8-e" => "ISO-8859-8-E", "iso-8859-8-i" => "ISO-8859-8-I", "gb2312" => "GB2312", "big5" => "Big5", "koi8-r" => "KOI8-R"]

Methods

baseInData()

Rebase the path in the data directory as payloads only deal in there.

public static baseInData(string $path) : string
Parameters
$path : string

The provided path.

Return values
string

The (possibly) rebased path.

checkedCopy()

Copy a file and check that the copy succeeded.

public static checkedCopy(string $sourceFile, string $destFile) : void
Parameters
$sourceFile : string

The source path.

$destFile : string

The destination path.

Tags
throws
FilesystemException

If the copy() call fails.

see
copy()
Return values
void

checkedFilePut()

Put contents to a file and check it succeeded.

public static checkedFilePut(string $path, string $contents, int $flags) : int
Parameters
$path : string

The path of the file.

$contents : string

The contents to put

$flags : int

Flags to pass on to file_put_contents.

Tags
throws
FilesystemException

On any error putting the contents to the file.

see
file_put_contents()
Return values
int

Number of bytes written to the file.

checkedFwrite()

Write to a file resource and check it succeeded.

public static checkedFwrite(resource $fp, string $content) : void
Parameters
$fp : resource

The file pointer.

$content : string

The content to write.

Tags
throws
FilesystemException

Problem writing to file.

Return values
void

checkedMkdir()

Make a directory (or directories) and check it succeeds.

public static checkedMkdir(string $path[, int $mode = 0777 ][, bool $recursive = false ]) : void
Parameters
$path : string

The path to create.

$mode : int = 0777

The permissions on the new directories.

$recursive : bool = false

Whether to create intermediate directories automatically.

Tags
throws
FilesystemException

If the mkdir() call fails.

see
mkdir()
Return values
void

checkedTempnam()

Create a temporary file and check it succeeded.

public static checkedTempnam([string $directory = "" ][, string $prefix = "" ]) : string
Parameters
$directory : string = ""

The directory to create the file in.

$prefix : string = ""

The prefix to the file.

Tags
throws
FilesystemException

Issues creating the file.

see
tempnam()
Return values
string

The path to the temporary filename.

Delete a file/directory and check it succeeded.

public static checkedUnlink(string $path) : void
Parameters
$path : string

The path to remove.

Tags
throws
FilesystemException

If the call to unlink() fails.

see
unlink()
Return values
void

checkUnencodedFilepath()

Check for unencoded newlines, carriage returns or % symbols in a file path.

public static checkUnencodedFilepath(string $filepath) : bool
Parameters
$filepath : string

The file path to check

Tags
see
BagUtils::encodeFilepath()
see
BagUtils::decodeFilepath()
Return values
bool

True if there are un-encoded characters

decodeFilepath()

Decode a file path according to the special rules of the spec.

public static decodeFilepath(string $line) : string

RFC 8943 - sections 2.1.3 & 2.2.3 If filename includes an LF, a CR, a CRLF, or a percent sign (%), those characters (and only those) MUST be percent-encoded as described in [RFC3986].

Parameters
$line : string

The original filepath from the manifest file.

Return values
string

The filepath with the special characters decoded.

encodeFilepath()

Encode a file path according to the special rules of the spec.

public static encodeFilepath(string $line) : string

RFC 8943 - sections 2.1.3 & 2.2.3 If filename includes an LF, a CR, a CRLF, or a percent sign (%), those characters (and only those) MUST be percent-encoded as described in [RFC3986].

Parameters
$line : string

The original file path.

Return values
string

The file path with the special manifest characters encoded.

findAllByPattern()

Return all files that match the pattern, or an empty array.

public static findAllByPattern(string $pattern) : array<string|int, mixed>
Parameters
$pattern : string

The pattern to search for.

Tags
throws
FilesystemException

Error in matching pattern.

Return values
array<string|int, mixed>

Array of matches.

getAbsolute()

There is a method that deal with Sven Arduwie proposal https://www.php.net/manual/en/function.realpath.php#84012 And runeimp at gmail dot com proposal https://www.php.net/manual/en/function.realpath.php#112367

public static getAbsolute(string $path[, bool $add_absolute = false ]) : string
Parameters
$path : string

The path to decode.

$add_absolute : bool = false

Whether to prepend the current working directory if the path is relative.

Tags
author

moreau.marc.web@gmail.com

Return values
string

getAllFiles()

Recursively list all files in a directory, except dot files.

public static getAllFiles(string $directory[, array<string|int, mixed> $exclusions = [] ]) : array<string|int, mixed>
Parameters
$directory : string

The starting full path.

$exclusions : array<string|int, mixed> = []

Array with directory names to skip.

Return values
array<string|int, mixed>

List of files with absolute path.

getValidCharset()

Check the provided lower case name of a character set against our list. If we have it, return the proper MIME name.

public static getValidCharset(string $charset) : string|null
Parameters
$charset : string

The trimmed lowercase version of the character set MIME name.

Return values
string|null

The proper name or null if we don't have it.

invalidPathCharacters()

Paths for new and existing files should not have these conditions.

public static invalidPathCharacters(string $path) : bool
Parameters
$path : string

The relative path from an existing bag file or as a destination for a new file.

Return values
bool

True if invalid characters/character sequences exist.

isDotDir()

Utility to test a filename as . or .

public static isDotDir(string $filename) : bool

.

Parameters
$filename : string

Basename of a file or directory.

Return values
bool

True if it is a dot directory name.

splitFileDataOnLineEndings()

Split the file data on any of the allowed line endings.

public static splitFileDataOnLineEndings(string $data) : array<string|int, mixed>
Parameters
$data : string

The file data as a single string.

Return values
array<string|int, mixed>

Array split on \r\n, \r, and \n

__construct()

BagUtils constructor.

private __construct() : mixed
Return values
mixed

Search results