Skip to content

Commit 35de7b8

Browse files
committed
Initial commit
0 parents  commit 35de7b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+6538
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
vendor/
3+
infection.log

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 simivar
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.MD

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Reverse print_r
2+
This library provides six different handlers for reversing output of PHP's `print_r` function back to original variables.
3+
If there's no handler available for a type it's returned as `string`.
4+
5+
## Assumptions
6+
- all values should be type-casted, not returned as `string`
7+
- empty string (`""`) is treated as `null` (see `NullHandler`)
8+
- integers are treated as integers (no `boolean` support)
9+
- multi-level `array` MUST be supported with type-casting
10+
- `public`, `protected` and `private` properties of Objects MUST be supported with type-casting
11+
12+
## Known issues
13+
- no support for Object Inheritance
14+
15+
# Installation
16+
Package is available via [Composer](https://getcomposer.org/).
17+
18+
```
19+
composer require simivar/reverse-print-r
20+
```
21+
22+
# Usage
23+
```php
24+
<?php
25+
26+
$print_r_output = print_r([
27+
'string' => 'some text',
28+
'integer' => 1,
29+
'float' => 2.3,
30+
'subArray' => [
31+
'Hello World.',
32+
],
33+
], true);
34+
35+
$reverser = new \ReversePrintR\ReversePrintR($print_r_output);
36+
echo $reverser->reverse()['float'];
37+
// outputs "2.3"
38+
```
39+
40+
## Changing behavior of Handlers
41+
All handlers are defined as `final`, but thanks to Dependency Injection it's easy to change the behavior of library
42+
and it's type-casting. Let's say you want to keep all the empty strings `""` as string, not `null`. All you have to do
43+
is create your own `HandlerRunner` without `NullHandler`.
44+
45+
```php
46+
<?php
47+
48+
$print_r_output = print_r([
49+
'string' => '',
50+
'null' => null,
51+
], true);
52+
53+
$handlerRunner = new \ReversePrintR\HandlerRunner(
54+
new \ReversePrintR\Handler\FloatHandler(),
55+
new \ReversePrintR\Handler\IntegerHandler(),
56+
new \ReversePrintR\Handler\ArrayHandler(),
57+
new \ReversePrintR\Handler\ObjectHandler()
58+
);
59+
60+
$reverser = new \ReversePrintR\ReversePrintR($print_r_output, $handlerRunner);
61+
var_dump($reverser->reverse()['null']);
62+
// outputs ""
63+
```
64+
65+
## Own Handlers
66+
The same way to removed `NullHandler` you can add your own handlers. All you have to do is make sure that it implements
67+
`\ReversePrintR\Handler\HandlerInterface` and you are good to go.
68+
69+
# Versioning
70+
Library is following [Semver](http://semver.org/). All minor and patch updates are backwards compatible.
71+
72+
# License
73+
Please see the [license file](https://github.com/simivar/reverse-print-r/blob/master/LICENSE) for more information.

composer.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "simivar/reverse-print-r",
3+
"description": "Library to reverse print_r output to PHP objects, arrays and scalar values.",
4+
"type": "library",
5+
"require-dev": {
6+
"phpunit/phpunit": "^9.0",
7+
"friendsofphp/php-cs-fixer": "^2.16",
8+
"vimeo/psalm": "^3.9",
9+
"infection/infection": "^0.15.3",
10+
"ergebnis/composer-normalize": "^2.2",
11+
"phpbench/phpbench": "@dev"
12+
},
13+
"autoload": {
14+
"psr-4": {
15+
"ReversePrintR\\": "src/"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Tests\\Unit\\ReversePrintR\\": "tests/Unit",
21+
"Tests\\Integration\\ReversePrintR\\": "tests/Integration"
22+
}
23+
},
24+
"license": "MIT",
25+
"authors": [
26+
{
27+
"name": "simivar",
28+
"email": "simivar@gmail.com"
29+
}
30+
],
31+
"minimum-stability": "stable",
32+
"require": {
33+
"php": "^7.1"
34+
},
35+
"scripts": {
36+
"psalm": "vendor/bin/psalm",
37+
"infection": "vendor/bin/infection",
38+
"test": "vendor/bin/phpunit",
39+
"test-unit": "vendor/bin/phpunit --testsuite Unit",
40+
"test-integration": "vendor/bin/phpunit --testsuite Integration",
41+
"bench": [
42+
"Composer\\Config::disableProcessTimeout",
43+
"vendor/bin/phpbench run --report=my_report --output=my_output"
44+
],
45+
"qa": [
46+
"@psalm",
47+
"@test",
48+
"@infection"
49+
]
50+
}
51+
}

0 commit comments

Comments
 (0)