forked from agavi/agavi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support arbitrary "provides" and "depends" validator options with acc…
…ess to argument base parts, closes agavi#1199
- Loading branch information
Showing
4 changed files
with
69 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,15 +59,15 @@ public function clear() | |
*/ | ||
public function checkDependencies(array $tokens, AgaviVirtualArrayPath $base) | ||
{ | ||
$root = new AgaviVirtualArrayPath(''); | ||
$currentParts = $base->getParts(); | ||
foreach($tokens as $token) { | ||
$path = $root; | ||
if(substr($token, 0, 1) == '[') { | ||
// the dependency we need to check is relative | ||
$path = $base; | ||
if($currentParts && strpos($token, '%') !== false) { | ||
// the depends attribute contains sprintf syntax | ||
$token = vsprintf($token, $currentParts); | ||
} | ||
|
||
if(!$path->getValueByChildPath($token, $this->depData)) { | ||
|
||
$path = new AgaviVirtualArrayPath($token); | ||
if(!$path->getValue($this->depData)) { | ||
return false; | ||
} | ||
} | ||
|
@@ -87,9 +87,43 @@ public function checkDependencies(array $tokens, AgaviVirtualArrayPath $base) | |
*/ | ||
public function addDependTokens(array $tokens, AgaviVirtualArrayPath $base) | ||
{ | ||
$currentParts = $base->getParts(); | ||
foreach($tokens as $token) { | ||
$base->setValueByChildPath($token, $this->depData, true); | ||
if($currentParts && strpos($token, '%') !== false) { | ||
// the depends attribute contains sprintf syntax | ||
$token = vsprintf($token, $currentParts); | ||
} | ||
|
||
$path = new AgaviVirtualArrayPath($token); | ||
$path->setValue($this->depData, true); | ||
} | ||
} | ||
|
||
/** | ||
* Populate key references in an argument base string if necessary. | ||
* Fills only empty bracket positions with an sprintf() offset placeholder. | ||
* Example: foo[][bar][] as input will return foo[%2$s][bar][%4$s] as output. | ||
* This is used in validate.xsl to convert pre-1.1 provides/depends behavior. | ||
* | ||
* @param string The argument base string. | ||
* | ||
* @return string The argument base string with empty brackets filled with | ||
* correct sprintf() position specifiers. | ||
* | ||
* @author David Zülke <[email protected]> | ||
* @since 1.1.0 | ||
*/ | ||
public static function populateArgumentBaseKeyRefs($string) | ||
{ | ||
$index = 1; | ||
return preg_replace_callback( | ||
'#\[([^\]]*)\]#', | ||
function($matches) use(&$index) { | ||
$index++; // always increment so static key parts are "skipped" properly | ||
return $matches[1] !== '' ? $matches[0] : '[%'.$index.'$s]'; // leave parts other than "[]" intact, else inject numeric accessor | ||
}, | ||
$string | ||
); | ||
} | ||
} | ||
?> |