Skip to content

Commit

Permalink
New method FileHelper::fwritei() to write with insertion of content…
Browse files Browse the repository at this point in the history
… instead of replacement
  • Loading branch information
ElGigi committed Mar 17, 2022
1 parent 9cb5d1b commit eabef23
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 53 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [1.7.0] - 2022-03-17

### Added

- New method `FileHelper::fwritei()` to write with insertion of content instead of replacement

## [1.6.5] - 2022-03-07

### Fixed
Expand Down
111 changes: 58 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,107 +10,112 @@ Many PHP functions used in the Berlioz framework, which you can use in your deve

## Array

- `b_array_is_list(array $array): bool`
- `b_array_is_list(array $array): bool`

Is sequential array?
Is sequential array?

- `b_array_column(array $array, int|string|\Closure|null $column_key, int|string|\Closure|null $index_key = null): array`
- `b_array_column(array $array, int|string|\Closure|null $column_key, int|string|\Closure|null $index_key = null): array`

Get values from a single column in the input array.

Difference between native array_column() and b_array_column() is
that b_array_column() accept a \Closure in keys arguments.
Get values from a single column in the input array.

- `b_array_merge_recursive(array $arraySrc, array ...$arrays): array`
Difference between native array_column() and b_array_column() is that b_array_column() accept a \Closure in keys
arguments.

Merge two or more arrays recursively.
- `b_array_merge_recursive(array $arraySrc, array ...$arrays): array`

Difference between native array_merge_recursive() is that
b_array_merge_recursive() do not merge strings values
into an array.
Merge two or more arrays recursively.

- `b_array_traverse_exists(&$mixed, string $path): bool`
Difference between native array_merge_recursive() is that b_array_merge_recursive() do not merge strings values into
an array.

Traverse array with path and return if path exists.
- `b_array_traverse_exists(&$mixed, string $path): bool`

- `b_array_traverse_get(&$mixed, string $path, $default = null): mixed|null`
Traverse array with path and return if path exists.

Traverse array with path and get value.
- `b_array_traverse_get(&$mixed, string $path, $default = null): mixed|null`

- `b_array_traverse_set(&$mixed, string $path, $value): bool`
Traverse array with path and get value.

Traverse array with path and set value.
- `b_array_traverse_set(&$mixed, string $path, $value): bool`

Traverse array with path and set value.

## File

- `b_human_file_size($size, int $precision = 2): string`
- `b_human_file_size($size, int $precision = 2): string`

Get a human see file size.
Get a human see file size.

- `b_size_from_ini(string $size): int`
- `b_size_from_ini(string $size): int`

Get size in bytes from ini conf file.
Get size in bytes from ini conf file.

- `b_resolve_absolute_path(string $srcPath, string $dstPath): ?string`
- `b_resolve_absolute_path(string $srcPath, string $dstPath): ?string`

Resolve absolute path from another.
Resolve absolute path from another.

- `b_resolve_relative_path(string $srcPath, string $dstPath): string`
- `b_resolve_relative_path(string $srcPath, string $dstPath): string`

Resolve relative path from another.
Resolve relative path from another.

## File
- `b_fwritei(resource $resource, string $str, ?int $length = null, ?int $offset = null): int|false`

File write in insertion mode.

Use seekable and writeable resource and not mode 'a+'.

## Object

- `b_get_property_value($object, string $property, &$exists = null): mixed`
- `b_get_property_value($object, string $property, &$exists = null): mixed`

Get property value with getter method.
Get property value with getter method.

- `b_set_property_value($object, string $property, $value): bool`
- `b_set_property_value($object, string $property, $value): bool`

Set property value with setter method.
Set property value with setter method.

## String

- `b_str_random(int $length = 12, int $options = B_STR_RANDOM_NUMBER | B_STR_RANDOM_SPECIAL_CHARACTERS | B_STR_RANDOM_NEED_ALL): string`
- `b_str_random(int $length = 12, int $options = B_STR_RANDOM_NUMBER | B_STR_RANDOM_SPECIAL_CHARACTERS | B_STR_RANDOM_NEED_ALL): string`

Generate an random string.
Generate an random string.

- `b_nl2p(string $str): string`
- `b_nl2p(string $str): string`

Surrounds paragraphs with "P" HTML tag and inserts HTML line breaks before all newlines; in a string.
Surrounds paragraphs with "P" HTML tag and inserts HTML line breaks before all newlines; in a string.

- `b_str_remove_accents(string $str): string`
- `b_str_remove_accents(string $str): string`

Remove accents.
Remove accents.

- `b_str_to_uri(string $str): string`
- `b_str_to_uri(string $str): string`

String to URI string.
String to URI string.

- `b_minify_html(string $str): string`
- `b_minify_html(string $str): string`

Minify HTML string.
Minify HTML string.

- `b_str_truncate(string $str, int $nbCharacters = 128, int $where = B_TRUNCATE_RIGHT, string $separator = '...'): string`
- `b_str_truncate(string $str, int $nbCharacters = 128, int $where = B_TRUNCATE_RIGHT, string $separator = '...'): string`

Truncate string.
Truncate string.

- `b_parse_str(string $str, bool $keepDots = true): array`
- `b_parse_str(string $str, bool $keepDots = true): array`

Parses the string into variables.
Parses the string into variables.

- `b_pascal_case(string $str): string`
- `b_pascal_case(string $str): string`

Get pascal case convention of string.
Get pascal case convention of string.

- `b_camel_case(string $str): string`
- `b_camel_case(string $str): string`

Get camel case convention of string.
Get camel case convention of string.

- `b_snake_case(string $str): string`
- `b_snake_case(string $str): string`

Get snake case convention of string.
Get snake case convention of string.

- `b_spinal_case(string $str): string`
- `b_spinal_case(string $str): string`

Get spinal case convention of string.
Get spinal case convention of string.
36 changes: 36 additions & 0 deletions src/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,40 @@ private static function extractFilename(array &$path): ?string

return $filename;
}

/**
* File write in insertion mode.
*
* Use seekable and writeable resource and not mode 'a+'.
*
* @param resource $resource
* @param string $data
* @param int|null $length
* @param int|null $offset
*
* @return int|false
*/
public static function fwritei($resource, string $data, ?int $length = null, ?int $offset = null)
{
if (!is_resource($resource)) {
throw new InvalidArgumentException('Argument #1 must be a valid resource');
}

// Shift content
fseek($resource, $currentPos = $offset ?? ftell($resource));
$dataLength = $length ?? strlen($data);
$i = 0;
$totalWritten = 0;
do {
$shiftData = fread($resource, $dataLength) ?: false;
fseek($resource, $currentPos + ($dataLength * $i++));
if (false === ($written = fwrite($resource, $data, $dataLength))) {
return false;
}
$totalWritten += $written;
$data = $shiftData;
} while (false !== $shiftData);

return $totalWritten;
}
}
17 changes: 17 additions & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ function b_resolve_relative_path(string $srcPath, string $dstPath): string
return FileHelper::resolveRelativePath($srcPath, $dstPath);
}

/**
* File write in insertion mode.
*
* Use seekable and writeable resource and not mode 'a+'.
*
* @param resource $resource
* @param string $data
* @param int|null $length
* @param int|null $offset
*
* @return int|false
*/
function b_fwritei($resource, string $data, ?int $length = null, ?int $offset = null)
{
return FileHelper::fwritei($resource, $data, $length, $offset);
}


/////////////////////
/// OBJECT HELPER ///
Expand Down
55 changes: 55 additions & 0 deletions tests/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,59 @@ public function testResolveRelativePath($src, $dst, $expected)
{
$this->assertEquals($expected, FileHelper::resolveRelativePath($src, $dst));
}

public function testFwritei()
{
$resource = fopen('php://memory', 'w+');
fwrite($resource, 'Populari magistro');

FileHelper::fwritei($resource, ' commeatus');

$this->assertEquals(
'Populari magistro commeatus',
stream_get_contents($resource, -1, 0)
);

fseek($resource, 9);
FileHelper::fwritei($resource, 'quae referente ');

$this->assertEquals(
'Populari quae referente magistro commeatus',
stream_get_contents($resource, -1, 0)
);

fseek($resource, 0);
FileHelper::fwritei($resource, 'BERLIOZ: ');

$this->assertEquals(
'BERLIOZ: Populari quae referente magistro commeatus',
stream_get_contents($resource, -1, 0)
);
}

public function testFwritei_withLength()
{
$resource = fopen('php://memory', 'w+');
fwrite($resource, 'Populari magistro');

FileHelper::fwritei($resource, ' commeatus', 4, 8);

$this->assertEquals(
'Populari com magistro',
stream_get_contents($resource, -1, 0)
);
}

public function testFwritei_withOffset()
{
$resource = fopen('php://memory', 'w+');
fwrite($resource, 'Populari magistro');

FileHelper::fwritei($resource, ' commeatus', null, 8);

$this->assertEquals(
'Populari commeatus magistro',
stream_get_contents($resource, -1, 0)
);
}
}

0 comments on commit eabef23

Please sign in to comment.