Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasReker committed Mar 26, 2023
1 parent 6537e87 commit 4b40fad
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 188 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ jobs:
run: composer install --optimize-autoloader --prefer-dist

- name: Run PHPUnit tests
run: composer run test
run: composer test

This comment has been minimized.

Copy link
@szepeviktor

szepeviktor Mar 26, 2023

@MathiasReker This may seem like an improvement as you need to type less.
But now you don't know whether test is a Composer command or a Composer script name.

Explicit is better than implicit.

This comment has been minimized.

Copy link
@MathiasReker

MathiasReker Mar 26, 2023

Author Owner

@MathiasReker This may seem like an improvement as you need to type less. But now you don't know whether test is a Composer command or a Composer script name.

Explicit is better than implicit.

I agree. Thanks for pointing it out. It's super cool with this kind of feedback! I will fix it.


- name: Run PHP CS fixer
run: composer run cs-check
run: composer cs-check

- name: Run phpstan
run: composer run phpstan
run: composer phpstan

- name: Run ergebnis/composer-normalize
run: composer normalize --dry-run --no-check-lock
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ functions.
### Requirements

- `PHP` >= 8.0
- `ext-mbstring`
- php-extension `ext-mbstring`

### Installation

Expand All @@ -39,17 +39,17 @@ functions.

## ✅ mb_count_chars

mb_count_chars — Return information about characters used in a string
mb_count_chars — Return information about characters used in a string.

```
```php
mb_count_chars(string $string, int $mode = 0, string $encoding = 'UTF-8'): array|string
```

## ✅ mb_ucwords

mb_ucwords — Uppercase the first character of each word in a string.

```
```php
mb_ucwords(
string $string,
string $separators = " \t\r\n\f\v",
Expand All @@ -61,15 +61,15 @@ mb_ucwords(

mb_strrev — Reverse a string.

```
```php
mb_strrev(string $string, string $encoding = 'UTF-8'): string
```

## ✅ mb_str_pad

mb_str_pad — Pad a string to a certain length with another string.

```
```php
mb_str_pad(
string $string,
int $length,
Expand All @@ -83,7 +83,7 @@ mb_str_pad(

mb_ucfirst — Make a string's first character uppercase.

```
```php
mb_ucfirst(string $string, string $encoding = 'UTF-8'): string
```

Expand Down Expand Up @@ -117,37 +117,37 @@ docker exec -it php-mbstring-extension bash
PHP Coding Standards Fixer:

```bash
composer run cs-fix
composer cs-fix
```

PHP Coding Standards Checker:

```bash
composer run cs-check
composer cs-check
```

Rector Fixer:

```bash
composer run rector-fix
composer rector-fix
```

Rector Checker:

```bash
composer run rector-check
composer rector-check
```

PHP Stan:

```bash
composer run phpstan
composer phpstan
```

Unit tests:

```bash
composer run test
composer test
```

### License
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"autoload": {
"files": [
"src/Helper/multibyteString.php"
"src/Helper/MultibyteString.php"
]
},
"autoload-dev": {
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3"
services:
app:
build: .
image: php-mbstring-extension:1.0.0
image: php-mbstring-extension:1.0.1
container_name: "php-mbstring-extension"
volumes:
- ./:/app
167 changes: 167 additions & 0 deletions src/Helper/MultibyteString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php
/**
* This file is part of the php-mbstring-extension package.
* (c) Mathias Reker <github@reker.dk>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

if (!function_exists('mb_ucwords')) {
/**
* Uppercase the first character of each word in a string.
*
* @see https://github.com/MathiasReker/php-mbstring-extension
*
* @param string $string The input string to modify
* @param string $encoding [optional] The character encoding. Defaults to 'UTF-8'.
*
* @return string The modified string
*/
function mb_ucwords(string $string, string $encoding = 'UTF-8'): string
{
$result = '';
$previous_character = ' ';

$length = mb_strlen($string, $encoding);
for ($i = 0; $i < $length; ++$i) {
$current_character = mb_substr($string, $i, 1, $encoding);

if (' ' === $previous_character) {
$current_character = mb_strtoupper($current_character, $encoding);
}

$result .= $current_character;
$previous_character = $current_character;
}

return $result;
}
}

if (!function_exists('mb_ucfirst')) {
/**
* Make the first character of a string uppercase.
*
* @see https://github.com/MathiasReker/php-mbstring-extension
*
* @param string $string The input string
* @param string $encoding [optional] The character encoding. Defaults to 'UTF-8'.
*
* @return string The resulting string
*/
function mb_ucfirst(string $string, string $encoding = 'UTF-8'): string
{
$first_char = mb_substr($string, 0, 1, $encoding);
$rest = mb_substr($string, 1, null, $encoding);

$lower_first_char = mb_strtolower($first_char, $encoding);
if ($first_char === $lower_first_char) {
$first_char = mb_strtoupper($first_char, $encoding);
}

return $first_char . $rest;
}
}

if (!function_exists('mb_strrev')) {
/**
* Reverse a string.
*
* @see https://github.com/MathiasReker/php-mbstring-extension
*
* @param string $string The string to be reversed
* @param string $encoding [optional] The character encoding. Defaults to 'UTF-8'.
*
* @return string the reversed string
*/
function mb_strrev(string $string, string $encoding = 'UTF-8'): string
{
$length = mb_strlen($string, $encoding);
$reversed = '';

while ($length-- > 0) {
$reversed .= mb_substr($string, $length, 1, $encoding);
}

return $reversed;
}
}

if (!function_exists('mb_str_pad')) {
/**
* Pad a string to a certain length with another string.
*
* @see https://github.com/MathiasReker/php-mbstring-extension
*
* @param string $input The string to pad
* @param int $pad_length The length of the resulting padded string
* @param string $pad_string [optional] The string to use for padding, defaults to ' '
* The pad_string may be truncated if the required number of padding
* characters can't be evenly divided by the pad_string's length
* @param int $pad_type [optional] The type of padding to apply, defaults to STR_PAD_RIGHT
* Can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH
* @param string $encoding [optional] The character encoding. Defaults to 'UTF-8'
*
* @return string The padded string
*
* @throws ValueError If $pad_type is not STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH
*/
function mb_str_pad(string $input, int $pad_length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, string $encoding = 'UTF-8'): string
{
if (!in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], true)) {
throw new ValueError('Argument #4 ($pad_type) must be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH');
}

return str_pad($input, strlen($input) - mb_strlen($input, $encoding) + $pad_length, $pad_string, $pad_type);
}
}

if (!function_exists('mb_count_chars')) {
/**
* Returns information about characters used in a string.
*
* @see https://github.com/MathiasReker/php-mbstring-extension
*
* @param string $string The string to be examined
* @param int $mode Specifies what information to return.
* - 0: Returns an array with the byte-value as key and the frequency of
* every byte as value.
* - 1: Same as 0 but only byte-values with a frequency greater than zero are listed.
* - 2: Same as 0 but only byte-values with a frequency equal to zero are listed.
* - 3: Returns a string containing all unique characters in the string.
* - 4: Returns a string containing all characters in the string that are not used.
* @param string $encoding [optional] The character encoding. Defaults to 'UTF-8'.
*
* @return int[]|string Returns the information requested based on the mode parameter:
* - Mode 0, 1, or 2: returns an array with byte-values as keys and frequencies as values.
* - Mode 3 or 4: returns a string with unique characters or unused characters.
*
* @throws ValueError if the mode parameter is not between 0 and 4 (inclusive)
*/
function mb_count_chars(string $string, int $mode, string $encoding = 'UTF-8'): array|string
{
$length = mb_strlen($string, $encoding);
$char_counts = [];

for ($i = 0; $i < $length; ++$i) {
$char = mb_substr($string, $i, 1, $encoding);

if (!array_key_exists($char, $char_counts)) {
$char_counts[$char] = 0;
}

++$char_counts[$char];
}

return match ($mode) {
0 => $char_counts,
1 => array_filter($char_counts, static fn ($count): bool => $count > 0),
2 => array_filter($char_counts, static fn ($count): bool => 0 === $count),
3 => implode('', array_unique(mb_str_split($string, 1, $encoding))),
4 => implode('', array_filter(array_unique(mb_str_split($string, 1, $encoding)), static fn ($char): bool => 0 === $char_counts[$char])),
default => throw new ValueError('Argument #2 ($mode) must be between 0 and 4 (inclusive)'),
};
}
}

0 comments on commit 4b40fad

Please sign in to comment.