-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial version of property bag collections
- Loading branch information
Showing
19 changed files
with
334 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Magomogo\Persisted; | ||
|
||
interface CollectableModelInterface | ||
{ | ||
/** | ||
* @param PropertyBagCollection $collection | ||
* @param $offset | ||
*/ | ||
public function appendToCollection($collection, $offset); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Magomogo\Persisted; | ||
|
||
interface CollectionOwnerInterface | ||
{ | ||
public function collections(); | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Magomogo\Persisted; | ||
|
||
use Magomogo\Persisted\Container\ContainerInterface; | ||
use Magomogo\Persisted\PropertyBag; | ||
use Magomogo\Persisted\CollectableModelInterface; | ||
|
||
abstract class PropertyBagCollection implements \ArrayAccess, \IteratorAggregate, \Countable | ||
{ | ||
/** | ||
* @var PropertyBag | ||
*/ | ||
protected $owner; | ||
|
||
protected $items = array(); | ||
|
||
abstract protected function constructModel($propertyBag); | ||
|
||
/** | ||
* @param ContainerInterface $container | ||
* @return $this | ||
*/ | ||
public function loadFrom($container) | ||
{ | ||
foreach ($container->listReferences($this, $this->owner) as $properties) { | ||
/** @var PropertyBag $properties */ | ||
$this->items[] = $properties; | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param ContainerInterface $container | ||
* @return $this | ||
*/ | ||
public function putIn($container) | ||
{ | ||
$container->referToMany($this, $this->owner, $this->items); | ||
return $this; | ||
} | ||
|
||
public function appendPropertyBag($propertyBag, $offset) | ||
{ | ||
if (is_null($offset)) { | ||
$this->items[] = $propertyBag; | ||
} else { | ||
$this->items[$offset] = $propertyBag; | ||
} | ||
} | ||
|
||
public function count() | ||
{ | ||
return count($this->items); | ||
} | ||
|
||
public function offsetExists($offset) | ||
{ | ||
return array_key_exists($offset, $this->items); | ||
} | ||
|
||
public function offsetGet($offset) | ||
{ | ||
return $this->constructModel($this->items[$offset]); | ||
} | ||
|
||
/** | ||
* @param mixed $offset | ||
* @param CollectableModelInterface $value | ||
*/ | ||
public function offsetSet($offset, $value) | ||
{ | ||
$value->appendToCollection($this, $offset); | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
unset($this->items[$offset]); | ||
} | ||
|
||
public function getIterator() | ||
{ | ||
return new \ArrayIterator($this->asArray()); | ||
} | ||
|
||
public function asArray() | ||
{ | ||
$models = array(); | ||
foreach ($this->items as $offset => $propertyBag) { | ||
$models[$offset] = $this->constructModel($propertyBag); | ||
} | ||
return $models; | ||
} | ||
} |
Oops, something went wrong.