Skip to content

Commit 0bee727

Browse files
author
Andrey Helldar
committed
Added the ability to save simple arrays (values only)
1 parent d105904 commit 0bee727

File tree

7 files changed

+123
-33
lines changed

7 files changed

+123
-33
lines changed

README.md

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,6 @@ Simple conversion of an array to a pretty view.
1717
</p>
1818

1919

20-
## Content
21-
22-
* [Installation](#installation)
23-
* [Introduction](#introduction)
24-
* [Using](#using)
25-
* [Saving numeric keys without alignment](#saving-numeric-keys-without-alignment)
26-
* [Saving string keys without alignment](#saving-string-keys-without-alignment)
27-
* [Saving numeric keys with alignment](#saving-numeric-keys-with-alignment)
28-
* [Saving string keys with alignment](#saving-string-keys-with-alignment)
29-
* [Change key case](#change-key-case)
30-
* [Storing file](#storing-file)
31-
* [Copyright and License](#copyright-and-license)
32-
33-
3420
## Installation
3521

3622
To get the latest version of `Pretty Array` package, simply require the project using [Composer](https://getcomposer.org):
@@ -44,19 +30,19 @@ Instead, you may of course manually update your `require-dev` block and run `com
4430
```json
4531
{
4632
"require-dev": {
47-
"andrey-helldar/pretty-array": "^2.0"
33+
"andrey-helldar/pretty-array": "^2.2"
4834
}
4935
}
5036
```
5137

52-
5338
## Introduction
5439

5540
> Q: Why did you create this package when there is a cooler [symfony/var-exporter](https://github.com/symfony/var-exporter)?
5641
5742
The big minus of package [symfony/var-exporter](https://github.com/symfony/var-exporter) is that it works differently with numeric keys.
5843

5944
For example, we have an array:
45+
6046
```php
6147
$array = [
6248
100 => 'foo',
@@ -69,6 +55,7 @@ $array = [
6955
```
7056

7157
When exporting through it, the file will contain the following content:
58+
7259
```php
7360
$array = [
7461
100 => 'foo',
@@ -82,13 +69,16 @@ $array = [
8269

8370
> Q: Why do you think this is bad?
8471
85-
This package has a framework-independent base. However, it was originally developed as an assistant for package [lang-translations](https://github.com/andrey-helldar/lang-translations).
72+
This package has a framework-independent base. However, it was originally developed as an assistant for
73+
package [lang-translations](https://github.com/andrey-helldar/lang-translations).
8674

8775
This package allows you to publish language translations for the Laravel framework.
8876

89-
A feature of the framework is that IDEs that help with development do not know how to read the numeric keys of arrays of translation files, so it was necessary to translate them into a text equivalent.
77+
A feature of the framework is that IDEs that help with development do not know how to read the numeric keys of arrays of translation files, so it was necessary to translate them
78+
into a text equivalent.
9079

9180
This behavior includes [errors.php](https://github.com/andrey-helldar/lang-translations/blob/master/src/lang/en/errors.php) file:
81+
9282
```php
9383
<?php
9484

@@ -114,7 +104,9 @@ return [
114104
// ...
115105
```
116106

117-
The peculiarity of the package is that it takes the values of the source file and combines it with what is already in the application. Thus, the output is a file with numeric keys that IDE helpers cannot read:
107+
The peculiarity of the package is that it takes the values of the source file and combines it with what is already in the application. Thus, the output is a file with numeric keys
108+
that IDE helpers cannot read:
109+
118110
```php
119111
<?php
120112

@@ -140,10 +132,10 @@ return [
140132
// ...
141133
```
142134

143-
144135
## Using
145136

146137
Source array for all examples:
138+
147139
```php
148140
$array = array (
149141
'foo' => 1,
@@ -175,6 +167,7 @@ return $service->raw($array);
175167
```
176168

177169
Result:
170+
178171
```text
179172
[
180173
'foo' => 1,
@@ -195,7 +188,6 @@ Result:
195188
]
196189
```
197190

198-
199191
### Saving string keys without alignment
200192

201193
```php
@@ -208,6 +200,7 @@ return $service->raw($array);
208200
```
209201

210202
Result:
203+
211204
```text
212205
[
213206
'foo' => 1,
@@ -228,7 +221,6 @@ Result:
228221
]
229222
```
230223

231-
232224
### Saving numeric keys with alignment
233225

234226
```php
@@ -241,6 +233,7 @@ return $service->raw($array);
241233
```
242234

243235
Result:
236+
244237
```text
245238
[
246239
'foo' => 1,
@@ -261,7 +254,6 @@ Result:
261254
]
262255
```
263256

264-
265257
### Saving string keys with alignment
266258

267259
```php
@@ -275,6 +267,7 @@ return $service->raw($array);
275267
```
276268

277269
Result:
270+
278271
```text
279272
[
280273
'foo' => 1,
@@ -295,6 +288,38 @@ Result:
295288
]
296289
```
297290

291+
### Saving simple array
292+
293+
```php
294+
use Helldar\PrettyArray\Services\Formatter;
295+
296+
$service = Formatter::make();
297+
$service->setSimple();
298+
299+
return $service->raw($array);
300+
```
301+
302+
Result:
303+
304+
```text
305+
[
306+
1,
307+
2,
308+
3,
309+
'qaz',
310+
[
311+
'qwe',
312+
'rty',
313+
'zxc',
314+
],
315+
[
316+
'qwe',
317+
'rty',
318+
'zxc',
319+
],
320+
'iop',
321+
]
322+
```
298323

299324
### Change key case
300325

@@ -309,6 +334,7 @@ return $service->raw($array);
309334
```
310335

311336
Result:
337+
312338
```text
313339
[
314340
'Foo' => 1,
@@ -341,6 +367,7 @@ The following options are available:
341367

342368

343369
### Storing file
370+
344371
```php
345372
use Helldar\PrettyArray\Services\File;
346373
use Helldar\PrettyArray\Services\Formatter;
@@ -354,6 +381,7 @@ File::make($formatted)
354381
```
355382

356383
Result in stored file `foo.php`:
384+
357385
```php
358386
<?php
359387

@@ -375,8 +403,3 @@ return [
375403
2 => 'iop',
376404
];
377405
```
378-
379-
380-
## Copyright and License
381-
382-
`Pretty Array` was written by Andrey Helldar, and is released under the MIT License. See the [LICENSE](LICENSE) file for details.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"andrey-helldar/support": "^2.0|^3.0"
2222
},
2323
"require-dev": {
24-
"phpunit/phpunit": "^8.0|^9.0"
24+
"phpunit/phpunit": "^9.0"
2525
},
2626
"suggest": {
2727
"symfony/thanks": "Give thanks (in the form of a GitHub) to your fellow PHP package maintainers"

src/Services/Formatter.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ final class Formatter implements Caseable
1616

1717
protected $equals_align = false;
1818

19+
protected $simple = false;
20+
1921
protected $pad_length = 4;
2022

2123
protected $line_break = PHP_EOL;
@@ -35,6 +37,11 @@ public function setEqualsAlign(): void
3537
$this->equals_align = true;
3638
}
3739

40+
public function setSimple(): void
41+
{
42+
$this->simple = true;
43+
}
44+
3845
public function raw(array $array, int $pad = 1): string
3946
{
4047
$array = $this->convertKeysCase($array);
@@ -47,7 +54,9 @@ public function raw(array $array, int $pad = 1): string
4754
$key = $this->key($key, $keys_size);
4855
$value = $this->value($value, $pad + 1);
4956

50-
$row = "{$key} => {$value}," . $this->line_break;
57+
$row = $this->simple
58+
? "$value," . $this->line_break
59+
: "$key => $value," . $this->line_break;
5160

5261
$formatted .= $this->pad($row, $pad_length);
5362
}

tests/FormatterSimple.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
class FormatterSimple extends TestCase
6+
{
7+
public function testAsSimple()
8+
{
9+
$service = $this->service();
10+
$service->setSimple();
11+
12+
$array = $this->requireSource('source-simple.php');
13+
$formatted = $service->raw($array);
14+
15+
$this->assertSame(
16+
$this->getFile('simple.txt'),
17+
$formatted . PHP_EOL
18+
);
19+
}
20+
}

tests/TestCase.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function getFile(string $filename): string
2626
}
2727

2828
/**
29-
* @param string $filename
29+
* @param string $filename
3030
*
3131
* @throws \Helldar\PrettyArray\Exceptions\FileDoesntExistsException
3232
*
@@ -40,12 +40,14 @@ protected function requireFile(string $filename): array
4040
}
4141

4242
/**
43+
* @param string $filename
44+
*
4345
* @throws \Helldar\PrettyArray\Exceptions\FileDoesntExistsException
4446
*
4547
* @return array
4648
*/
47-
protected function requireSource(): array
49+
protected function requireSource(string $filename = 'source.php'): array
4850
{
49-
return $this->requireFile('source.php');
51+
return $this->requireFile($filename);
5052
}
5153
}

tests/stubs/simple.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
'foo',
3+
'bar',
4+
'baz',
5+
'qwe rty',
6+
[
7+
'qwe',
8+
'rty',
9+
'zxc',
10+
],
11+
[
12+
'foo bar baz',
13+
'rty',
14+
'qaw sed',
15+
],
16+
'iop',
17+
]

tests/stubs/source-simple.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
return array (
4+
1 => 'foo',
5+
'foo' => 'bar',
6+
'baz',
7+
'qwe rty',
8+
'baq' => array (
9+
'qwe',
10+
'rty',
11+
'zxc',
12+
),
13+
'asd fgh' => array (
14+
'foo bar baz',
15+
'rty',
16+
'qaw sed',
17+
),
18+
'iop',
19+
);

0 commit comments

Comments
 (0)