Skip to content

Commit

Permalink
basic code
Browse files Browse the repository at this point in the history
  • Loading branch information
leNEKO committed Apr 28, 2024
1 parent ce50cd5 commit 23a04b1
Show file tree
Hide file tree
Showing 27 changed files with 3,045 additions and 6 deletions.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.PHONY: test static coverage profile-clear

test:
vendor/bin/phpunit --color

static:
psalm --show-info=true


coverage:
XDEBUG_MODE=coverage vendor/bin/phpunit \
--coverage-clover $(PHPUNIT_COVERAGE_PATH)/cov.xml \
--coverage-filter src \
--coverage-html $(PHPUNIT_COVERAGE_PATH);
hash xdg-open && xdg-open $(PHPUNIT_COVERAGE_PATH)/index.html;

profile-clear:
rm $(XDEBUG_OUTPUT_DIR)/cachegrind.out.*
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Flattener

A lib for (un)flatten php data structure and a small cli for apply this on Json input.

## Usage

### Lib

Basically, `Flattener::flatten(...)` turn this

```php
<?php

[
'hello' => [
'world' => [
'!', '?'
],
'people' => [
1, 2.3
],
],
'bye' => null
]
```

into

```php
<?php

[
'.hello.world[0]' => '!',
'.hello.world[1]' => '?',
'.hello.people[0]' => 1,
'.hello.people[1]' => 2.3,
'.bye' => null,
]
```

(And `Flattener::unflatten(...)` will play this backward)

### CLI


Get help

```shell
bin/json-unflatten --help
```

## Dev

### Coverage

```shell
make coverage
````
38 changes: 38 additions & 0 deletions bin/json-flatten
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env php
<?php

namespace Leneko\Flattener;

require dirname(__DIR__, 1) . '/vendor/autoload.php';

use Silly\Application;

$app = new Application('Json (un)flatten');

$app->command(
'flatten [path]',
function (string $path = 'php://stdin') {
echo JsonFlatten::flatten(
\file_get_contents($path)
);
}
)
->descriptions(
'flatten a json structure',
['path' => 'path to read or STDIN if not set']
);

$app->command(
'unflatten [path]',
function (string $path = 'php://stdin') {
echo JsonFlatten::unflatten(
\file_get_contents($path)
);
}
)
->descriptions(
'unflatten a json structure',
['path' => 'path to read or STDIN if not set']
);;

$app->run();
14 changes: 9 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "leneko/flattener",
"type": "library",
"license": "GPL-3.0",
"type": "lib",
"description": "(un)flatten php/json data structure",
"license": "GPL-3.0-or-later",
"autoload": {
"psr-4": {
"Leneko\\Flattener\\": "src/"
Expand All @@ -15,9 +16,12 @@
],
"minimum-stability": "stable",
"require": {
"php": "^8.3"
"php": "^8.3",
"mnapoli/silly": "^1.9"
},
"require-dev": {
"ext-xdebug": "*"
}
"ext-xdebug": "*",
"phpunit/phpunit": "^11.1"
},
"bin": ["bin/json-flatten"]
}
Loading

0 comments on commit 23a04b1

Please sign in to comment.