Skip to content
This repository has been archived by the owner on Feb 1, 2020. It is now read-only.

Commit

Permalink
Can use placeholders in keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
kherge committed Aug 15, 2013
1 parent 6ea8648 commit 2dd7f24
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 34 deletions.
92 changes: 59 additions & 33 deletions src/lib/Herrera/Wise/Loader/AbstractFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,40 +126,15 @@ public function process($data, $file)

array_walk_recursive(
$data,
function (&$value) use (&$data, $global, $_this) {
preg_match_all(
'/%(?P<reference>[^%]+)%/',
$value,
$matches
);
function (&$value, $key) use (&$data, $global, $_this) {
$value = $_this->doReplace($value, $data, $global);

if (false === empty($matches['reference'])) {
foreach ($matches['reference'] as $reference) {
try {
$ref = $_this->resolveReference($reference, $data);
} catch (InvalidReferenceException $exception) {
if (empty($global)) {
throw $exception;
}

$ref = $_this->resolveReference($reference, $global);
}

if ((false === is_null($ref))
&& (false === is_scalar($ref))
&& (false == preg_match('/^%(?:[^%]+)%$/', $value))) {
throw InvalidReferenceException::format(
'The non-scalar reference "%s" cannot be used inline.',
"%$reference%"
);
}

if ("%$reference%" === $value) {
$value = $ref;
} else {
$value = str_replace("%$reference%", $ref, $value);
}
}
if (false !== strpos($key, '%')) {
unset($data[$key]);

$key = $_this->doReplace($key, $data, $global);

$data[$key] = $value;
}
}
);
Expand Down Expand Up @@ -220,4 +195,55 @@ public function setWise(Wise $wise)
* @return array The parsed data.
*/
abstract protected function doLoad($file);

/**
* Replaced the placeholder value(s).
*
* @param mixed $input The input.
* @param array $data The data the input is from.
* @param array $global The global values.
*
* @return mixed The result.
*
* @throws InvalidReferenceException If an invalid reference is used.
*/
protected function doReplace($input, $data, $global)
{
preg_match_all(
'/%(?P<reference>[^%]+)%/',
$input,
$matches
);

if (false === empty($matches['reference'])) {
foreach ($matches['reference'] as $reference) {
try {
$ref = $this->resolveReference($reference, $data);
} catch (InvalidReferenceException $exception) {
if (empty($global)) {
throw $exception;
}

$ref = $this->resolveReference($reference, $global);
}

if ((false === is_null($ref))
&& (false === is_scalar($ref))
&& (false == preg_match('/^%(?:[^%]+)%$/', $input))) {
throw InvalidReferenceException::format(
'The non-scalar reference "%s" cannot be used inline.',
"%$reference%"
);
}

if ("%$reference%" === $input) {
$input = $ref;
} else {
$input = str_replace("%$reference%", $ref, $input);
}
}
}

return $input;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public function testProcess()
),
'global' => '%global.value%',
'placeholder' => '%imported.list%',
'inline_placeholder' => 'rand: %imported.list.null%%imported.value%'
'inline_placeholder' => 'rand: %imported.list.null%%imported.value%',
'%imported.key%' => 'a value'
),
true
) . ';'
Expand All @@ -135,6 +136,7 @@ public function testProcess()
'<?php return ' . var_export(
array(
'imported' => array(
'key' => 'replaced_key',
'list' => array(
'null' => null,
'value' => 123
Expand All @@ -159,7 +161,9 @@ public function testProcess()
'value' => 123
),
'inline_placeholder' => 'rand: ' . $rand,
'replaced_key' => 'a value',
'imported' => array(
'key' => 'replaced_key',
'list' => array(
'null' => null,
'value' => 123
Expand Down

0 comments on commit 2dd7f24

Please sign in to comment.