https://github.com/mortalflesh/stringify
Simple and tiny class (function) to stringify anything in PHP.
https://github.com/mortalflesh/stringify
stringify
Last synced: 8 months ago
JSON representation
Simple and tiny class (function) to stringify anything in PHP.
- Host: GitHub
- URL: https://github.com/mortalflesh/stringify
- Owner: MortalFlesh
- License: mit
- Created: 2018-11-14T13:00:43.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-12-13T09:53:17.000Z (over 2 years ago)
- Last Synced: 2025-08-19T03:53:17.839Z (10 months ago)
- Topics: stringify
- Language: PHP
- Size: 55.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
Stringify
=========
[](https://packagist.org/packages/mf/stringify)
[](https://github.com/MortalFlesh/stringify/actions/workflows/tests.yaml)
[](https://coveralls.io/github/MortalFlesh/stringify?branch=master)
Simple and tiny class (function) to stringify anything in PHP.
## Installation
```bash
composer require mf/stringify
```
## Usage
### By class and static method
```php
use MF\Stringify\Stringify;
echo Stringify::stringify([1, 2, 3]); // "[1, 2, 3]"
```
### By standalone function
```php
use function MF\Stringify\stringify;
echo stringify([1, 2, 3]); // "[1, 2, 3]"
```
```php
$result = array_map(stringify(...), [1, 'two']); // ['1', '"two"']
```
### Sprintf bonus
> with a new `%A` placeholder for `stringify` function
```php
use function MF\Stringify\sprintf;
echo sprintf('Hello %A!', 'world'); // Hello "world"!
echo sprintf('Hello %A!', ['world']); // Hello ["world"]!
```
## Example
_NOTE: values longer than 100 chars is shrinked to 100 chars with `...` suffix_
For easier examples, let's use a standalone function
| Type | PHP | Result (_string_) |
| --- | --- | --- |
| NULL | `stringify(null);` | `null` |
| bool | `stringify(true);` | `true` |
| bool | `stringify(false);` | `false` |
| string | `stringify('');` | `""` |
| string | `stringify('Some string');` | `"Some string"` |
| int | `stringify(42);` | `42` |
| float | `stringify(3.14);` | `3.14` |
| array | `stringify([1, 2, 3]);` | `[1, 2, 3]` |
| array | `stringify(['foo' => 'bar']);` | `["foo" => "bar"]` |
| array | `stringify(['person' => ['name' => 'Peter Parker'], 'alterego' => 'spider-man']);` | `["person" => ["name" => "Peter Parker"], "alterego" => "spider-man"]` |
| object | `stringify(new \Foo\Bar());` | `Foo\Bar` |
| object | `stringify(new \DateTime());` | `DateTime { 2018-11-15T10:20:30+00:00 }` |
| object | `stringify(Seq::range('1..4'));` | `MF\Collection\Immutable\Seq [1, 2, 3, 4]` |
## Changelog
For latest changes see [CHANGELOG.md](CHANGELOG.md) file. We follow [Semantic Versioning](https://semver.org/).
## Contributing and development
### Install dependencies
```bash
composer install
```
### Run tests
For each pull-request, unit tests as well as static analysis and codestyle checks must pass.
To run all those checks execute:
```bash
composer all
```