Replaces an existing leaf with a new one.
AssetInterface $needle The current asset to replace:
AssetInterface $replacement The new asset:
Boolean $graceful Whether the failure should return false or throw an exception:
Boolean Whether the asset has been found
\InvalidArgumentException If the asset cannot be found
Overrides AssetCollectionInterface::replaceLeaf
public function replaceLeaf(AssetInterface $needle, AssetInterface $replacement, $graceful = false) {
foreach ($this->assets as $i => $asset) {
$clone = isset($this->clones[$asset]) ? $this->clones[$asset] : null;
if (in_array($needle, array(
$asset,
$clone,
), true)) {
unset($this->clones[$asset]);
$this->assets[$i] = $replacement;
return true;
}
if ($asset instanceof AssetCollectionInterface && $asset
->replaceLeaf($needle, $replacement, true)) {
return true;
}
}
if ($graceful) {
return false;
}
throw new \InvalidArgumentException('Leaf not found.');
}