Created
July 17, 2014 12:37
-
-
Save Adirelle/16c1662dd4e9e027f2fc to your computer and use it in GitHub Desktop.
Collection-recursive AssetReference for Assetic
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
<?php | |
// Implements \IteratorAggregate and AssetCollectionInterface instead of only AssetInterface | |
class AssetReference implements \IteratorAggregate, AssetCollectionInterface | |
{ | |
/* [snip, snip] */ | |
public function add(AssetInterface $asset) | |
{ | |
$asset = $this->am->get($this->name); | |
if($asset instanceof AssetCollectionInterface) { | |
return $asset->add($asset); | |
} | |
} | |
public function all() | |
{ | |
$this->flushFilters(); | |
$asset = $this->am->get($this->name); | |
if($asset instanceof AssetCollectionInterface) { | |
return $asset->all(); | |
} else { | |
// Simply dereference | |
return [ $asset ]; | |
} | |
} | |
public function removeLeaf(AssetInterface $leaf, $graceful = false) | |
{ | |
$asset = $this->am->get($this->name); | |
if($asset instanceof AssetCollectionInterface) { | |
return $asset->removeLeaf($leaf, $graceful); | |
} | |
} | |
public function replaceLeaf(AssetInterface $needle, AssetInterface $replacement, $graceful = false) | |
{ | |
$asset = $this->am->get($this->name); | |
if($asset instanceof AssetCollectionInterface) { | |
return $asset->replaceLeaf($needle, $replacement, $graceful); | |
} | |
} | |
public function getIterator() | |
{ | |
$asset = $this->am->get($this->name); | |
if($asset instanceof \IteratorAggregate) { | |
return $asset->getIterator(); | |
} else { | |
return new \ArrayIterator([$asset]); | |
} | |
} | |
/* [snip, snip] */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment