Skip to content

Commit

Permalink
Merge pull request #13 from mediact/feature/APD-1542
Browse files Browse the repository at this point in the history
Add support for keys that contain the separator
  • Loading branch information
ashokadewit authored Jun 23, 2021
2 parents acad83a + 91e7bed commit 92e6157
Show file tree
Hide file tree
Showing 32 changed files with 319 additions and 42 deletions.
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@
"/phpcs.xml",
"/phpstan.neon",
"/tests",
"/example"
"/example",
"/grumphp.yml",
"/pdepend.xml"
]
},
"extra": {
"grumphp": {
"config-default-path": "vendor/mediact/testing-suite/config/default/grumphp.yml"
}
},
"config": {
"sort-packages": true
}
Expand Down
3 changes: 2 additions & 1 deletion example/branching.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand All @@ -20,5 +21,5 @@
);

foreach ($container->branch('categories.*') as $category) {
var_dump($category->get('name'));
print_r($category->get('name'));
}
2 changes: 2 additions & 0 deletions grumphp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- resource: 'vendor/mediact/testing-suite/config/default/grumphp.yml'
11 changes: 11 additions & 0 deletions pdepend.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<symfony:container xmlns:symfony="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://pdepend.org/schema/dic/pdepend"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<config>
<cache>
<driver>memory</driver>
</cache>
</config>
</symfony:container>
38 changes: 11 additions & 27 deletions src/DataContainer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand All @@ -15,6 +16,8 @@
class DataContainer implements IterableDataContainerInterface
{
use ReplaceByPatternTrait;
use PathParserTrait;
use KeyQuoterTrait;

/** @var array */
private $data;
Expand All @@ -24,7 +27,7 @@ class DataContainer implements IterableDataContainerInterface
*
* @param array $data
*/
public function __construct(iterable $data = [])
final public function __construct(iterable $data = [])
{
$this->data = $data instanceof Traversable
? iterator_to_array($data)
Expand Down Expand Up @@ -126,7 +129,7 @@ public function glob(string $pattern): array
? [$pattern]
: $this->findArrayPathsByPatterns(
$this->data,
explode(static::SEPARATOR, $pattern),
$this->parsePath($pattern),
''
);
}
Expand Down Expand Up @@ -169,11 +172,11 @@ function ($match) use ($pattern, $replacement) {
public function branch(string $pattern): array
{
return array_map(
function (array $data) : DataContainerInterface {
function (array $data): DataContainerInterface {
return new static($data);
},
array_map(
function (string $path) : array {
function (string $path): array {
return (array) $this->get($path, []);
},
$this->glob($pattern)
Expand Down Expand Up @@ -235,25 +238,6 @@ public function move(string $pattern, string $replacement)
}
}

/**
* Parse a path into an array.
*
* @param string $path
*
* @return array
*/
private function parsePath(string $path): array
{
return array_map(
function (string $key) {
return ctype_digit($key)
? intval($key)
: $key;
},
array_filter(explode(static::SEPARATOR, $path), 'strlen')
);
}

/**
* Get reference to a data node, create it if it does not exist.
*
Expand All @@ -265,9 +249,10 @@ private function &getNodeReference(array $keys): array
{
$current =& $this->data;

while (count($keys)) {
while (!empty($keys)) {
$key = array_shift($keys);
if (!array_key_exists($key, $current)
if (
!array_key_exists($key, $current)
|| !is_array($current[$key])
) {
$current[$key] = [];
Expand Down Expand Up @@ -303,8 +288,7 @@ function ($key) use ($pattern) {

$paths = [];
foreach ($matchingKeys as $key) {
$path = $prefix . $key;

$path = $prefix . $this->quoteKey($key);
if (count($patterns) === 0) {
$paths[] = $path;
continue;
Expand Down
1 change: 1 addition & 0 deletions src/DataContainerDecoratorTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions src/DataContainerFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions src/DataContainerFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
3 changes: 2 additions & 1 deletion src/DataContainerFilterChain.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down Expand Up @@ -37,7 +38,7 @@ function (
DataContainerFilterInterface $filter
) use (
$container
) : bool {
): bool {
return $carry && $filter($container);
},
true
Expand Down
1 change: 1 addition & 0 deletions src/DataContainerFilterInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
5 changes: 4 additions & 1 deletion src/DataContainerInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand All @@ -16,7 +17,9 @@ interface DataContainerInterface extends IteratorAggregate
/**
* The separator used in paths.
*/
const SEPARATOR = '.';
public const SEPARATOR = '.';
public const ENCLOSURE = '"';
public const ESCAPE = '\\';

/**
* Check whether a path exists.
Expand Down
1 change: 1 addition & 0 deletions src/DataContainerIterator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions src/DataContainerIteratorAggregateTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions src/DataContainerIteratorInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions src/IterableDataContainerInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
34 changes: 34 additions & 0 deletions src/KeyQuoterTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
*/

namespace Mediact\DataContainer;

trait KeyQuoterTrait
{
/**
* Quote a key.
*
* @param string $key
*
* @return string
*/
private function quoteKey(string $key): string
{
return strpos($key, DataContainerInterface::SEPARATOR) !== false
? sprintf(
'%s%s%s',
DataContainerInterface::ENCLOSURE,
str_replace(
DataContainerInterface::ENCLOSURE,
DataContainerInterface::ENCLOSURE . DataContainerInterface::ENCLOSURE,
$key
),
DataContainerInterface::ENCLOSURE
)
: $key;
}
}
40 changes: 40 additions & 0 deletions src/PathParserTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
*/

namespace Mediact\DataContainer;

trait PathParserTrait
{
/**
* Parse a path.
*
* @param string $path
*
* @return array
*/
private function parsePath(string $path): array
{
return array_map(
function (string $key) {
return ctype_digit($key)
? intval($key)
: $key;
},
array_values(
array_filter(
str_getcsv(
$path,
DataContainerInterface::SEPARATOR,
DataContainerInterface::ENCLOSURE,
DataContainerInterface::ESCAPE
),
'strlen'
)
)
);
}
}
1 change: 1 addition & 0 deletions src/PatternReplacer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions src/PatternReplacerInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions src/ReplaceByPatternTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions tests/DataContainerDecoratorTraitTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions tests/DataContainerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions tests/DataContainerFilterChainTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions tests/DataContainerIteratorAggregateTraitTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
1 change: 1 addition & 0 deletions tests/DataContainerIteratorTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
Expand Down
Loading

0 comments on commit 92e6157

Please sign in to comment.