Skip to content

Commit

Permalink
Merge pull request #13 from MortalFlesh/feature/require-php82
Browse files Browse the repository at this point in the history
Require php8.2
  • Loading branch information
MortalFlesh committed Dec 13, 2023
2 parents 31e5880 + 0e5d570 commit 18cae77
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:

strategy:
matrix:
php-version: ['8.1', '8.2', '8.3']
php-version: ['8.2', '8.3']
dependencies: ['']
include:
- { php-version: '8.1', dependencies: '--prefer-lowest --prefer-stable' }
- { php-version: '8.2', dependencies: '--prefer-lowest --prefer-stable' }

name: Unit tests - PHP ${{ matrix.php-version }} ${{ matrix.dependencies }}

Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.2'
extensions: mbstring, intl
tools: composer:v2

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Changelog rules:
-->

## Unreleased
- [**BC**] Require php 8.2
- Update dependencies
- Add `sprintf` function with a `%A` placeholder for `stringify` function
- Mark `stringify` constant as deprecated in favor of `sprintf(...)` syntax

## 6.0.0 - 2023-12-13
- [**BC**] Require php 8.1
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ use function MF\Stringify\stringify;
echo stringify([1, 2, 3]); // "[1, 2, 3]"
```

**Bonus**: Standalone function may be used through a constant with its FQN
```php
use const MF\Stringify\stringify;
$result = array_map(stringify(...), [1, 'two']); // ['1', '"two"']
```

### Sprintf bonus
> with a new `%A` placeholder for `stringify` function
```php
use function MF\Stringify\sprintf;

$result = array_map(stringify, [1, 'two']); // ['1', '"two"']
echo sprintf('Hello %A!', 'world'); // Hello "world"!
echo sprintf('Hello %A!', ['world']); // Hello ["world"]!
```

## Example
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
}
},
"require": {
"php": "^8.1"
"php": "^8.2"
},
"require-dev": {
"lmc/coding-standard": "^3.3",
"mf/collections-php": "^7.0",
"mf/collections-php": "^8.0",
"phpstan/extension-installer": "^1.3.1",
"phpstan/phpstan": "^1.10.49",
"phpstan/phpstan-phpunit": "^1.3.15",
Expand Down
29 changes: 29 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MF\Stringify;

/** @deprecated It is not that useful now, since the stringify(...) notation */
const stringify = __NAMESPACE__ . '\\stringify';

/**
Expand All @@ -21,3 +22,31 @@ function stringify(mixed $value, bool $shrinkLongOutput = false): string
{
return Stringify::stringify($value, $shrinkLongOutput);
}

/**
* Works the same as a \sprintf() function but with additional %A placeholder.
*
* %A placeholder stringify any value.
* @see stringify()
*
* @phpstan-param mixed $args
*/
function sprintf(string $format, ...$args): string
{
if (str_contains($format, '%A')) {
preg_match_all('/((?<!%)%[a-zA-Z])/', $format, $matches);
$matches = $matches[0] ?? [];
$format = (string) preg_replace('/((?<!%)%A)/', '%s', $format);

$modifiedArgs = [];
foreach ($args as $i => $arg) {
$modifiedArgs[$i] = ($matches[$i] ?? null) === '%A'
? stringify($arg)
: $arg;
}

$args = $modifiedArgs;
}

return \sprintf($format, ...$args);
}
61 changes: 61 additions & 0 deletions tests/SprintfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

namespace MF\Stringify;

/**
* @group unit
*/
class SprintfTest extends AbstractTestCase
{
/** @dataProvider provideValues */
public function testShouldFormatValuesToString(string $format, array $args, string $expected): void
{
$result = sprintf($format, ...$args);

$this->assertSame($expected, $result);
}

public static function provideValues(): iterable
{
return [
'empty' => ['', [], ''],
'string' => ['%s', ['foo'], 'foo'],
'string with multiple args' => ['%s %s', ['foo', 'bar'], 'foo bar'],
'string with numbers' => ['%s %d', ['foo', 42], 'foo 42'],
'string with float' => ['%s %f', ['foo', 42.42], 'foo 42.420000'],
'string with bool' => ['%s %b', ['foo', true], 'foo 1'],
'string with array' => ['%s %A', ['foo', [1, 2, 3]], 'foo [1, 2, 3]'],
'string with multiple arrays' => [
'%s %A %A',
['foo', [1, 2, 3], ['foo' => 'bar']],
'foo [1, 2, 3] ["foo" => "bar"]',
],
'string with object' => ['%s %A', ['foo', new \stdClass()], 'foo stdClass'],
'string with text and values' => [
'Hello %A, your age is %A',
['Jon', 42],
'Hello "Jon", your age is 42',
],
'string with more values than placeholders' => [
'Hello %A, your age is %d',
['Jon', 42, 3, 2, 1],
'Hello "Jon", your age is 42',
],
'string with % in it' => [
'Hello %A, your age is %d%% and that %% is not a placeholder',
['Jon', 42],
'Hello "Jon", your age is 42% and that % is not a placeholder',
],
'string with % followed by a char in it' => [
'Hello %A, your age is %d%% and that %%char is not a placeholder',
['Jon', 42],
'Hello "Jon", your age is 42% and that %char is not a placeholder',
],
'string with % followed by the A char in it' => [
'Hello %A, your age is %d%% and that %%A char is not a placeholder',
['Jon', 42],
'Hello "Jon", your age is 42% and that %A char is not a placeholder',
],
];
}
}

0 comments on commit 18cae77

Please sign in to comment.