Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## PENDING

- Add `Parser::reverseParse()` doing the opposite of `Parser::parse()` [#10](https://github.com/mapado/request-fields-parser/pull/10) by [@nickinthebox](https://github.com/nickinthebox)

## 3.0.3

- Test array access + avoid warning [#9](https://github.com/mapado/request-fields-parser/pull/9) by [@jdeniau](https://github.com/jdeniau)
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Convert string like `id,firstname,lastname,jobs{startDate,position,company{id,re

You can think of it like an [explode](https://php.net/explode) on steroids.

Also implement a `reverseParse` function for the opposite transformation.

## Installation

```sh
Expand All @@ -34,6 +36,8 @@ use Mapado\RequestFieldsParser\Parser;
$parser = new Parser();

$outArray = $parser->parse($string);

$outString = $parser->reverseParse($array);
```

## Extensibility
Expand All @@ -55,7 +59,7 @@ class ExtendedParser implements ParserInterface
$this->decoratedParser = $decoratedParser;
}

public function parse(string $sttring): array
public function parse(string $string): array
{
// do stuff and return an array
}
Expand Down
22 changes: 22 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ public function parse(string $string): Fields
return $out;
}

/**
* @param iterable<int|string, mixed>|string $fields
*/
public function reverseParse($fields): string
{
if (is_string($fields)) {
return $fields;
}

$fieldsStr = '';
foreach ($fields as $fieldName => $fieldValue) {
if (is_array($fieldValue) || $fieldValue instanceof \Traversable) {
$fieldsStr .= $fieldName . '{' . $this->reverseParse($fieldValue) . '},';
} else {
$fieldsStr .= (true === $fieldValue ? $fieldName : $fieldValue) . ',';
}
}
$fieldsStr = rtrim($fieldsStr, ',');

return $fieldsStr;
}

private function treatCurrent(bool $isFirst): Fields
{
if ($isFirst) {
Expand Down
72 changes: 72 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testEmpty(): void
$this->assertSame([], $parsed->toArray());
}


public function testOneLevelParser(): void
{
$testedInstance = new Parser();
Expand Down Expand Up @@ -83,4 +84,75 @@ public function testMultiLevelParser(): void
iterator_to_array($parsed)['eventDate'],
);
}


public function testReverseEmpty(): void
{
$testedInstance = new Parser();

$reverseParsed = $testedInstance->reverseParse([]);

$this->assertIsString($reverseParsed);
$this->assertSame('', $reverseParsed);
}

public function testOneLevelReverseParser(): void
{
$testedInstance = new Parser();
$reverseParsed = $testedInstance->reverseParse(['@id', 'title', 'eventDate']);
$this->assertIsString($reverseParsed);
$this->assertSame('@id,title,eventDate', $reverseParsed);
}

public function testMultiLevelReverseParser(): void
{
$source = [
'@id',
'title',
'eventDate' => [
'@id',
'startDate',
'ticketing' => [
'@id',
],
],
];

$testedInstance = new Parser();
$reverseParsed = $testedInstance->reverseParse($source);
$this->assertIsString($reverseParsed);
$this->assertSame('@id,title,eventDate{@id,startDate,ticketing{@id}}', $reverseParsed);

$sourceWithBooleans = [
'@id' => true,
'title' => true,
'eventDate' => [
'@id' => true,
'startDate' => true,
'ticketing' => [
'@id' => true,
],
],
];

$reverseParsedWithBooleans = $testedInstance->reverseParse($sourceWithBooleans);
$this->assertIsString($reverseParsedWithBooleans);
$this->assertSame('@id,title,eventDate{@id,startDate,ticketing{@id}}', $reverseParsedWithBooleans);
}


public function testReversability(): void
{
$testedInstance = new Parser();

$sample = '@id,title,eventDate';
$parsedSample = $testedInstance->parse($sample);
$reverseParsedSample = $testedInstance->reverseParse($parsedSample);
$this->assertSame($sample, $reverseParsedSample);

$sample = '@id,title,eventDate{@id,startDate,ticketing{@id}}';
$parsedSample = $testedInstance->parse($sample);
$reverseParsedSample = $testedInstance->reverseParse($parsedSample);
$this->assertSame($sample, $reverseParsedSample);
}
}