Skip to content

Commit

Permalink
Removed usage of ArrayObject, opting for ArrayAccess instead
Browse files Browse the repository at this point in the history
  • Loading branch information
jgswift committed Dec 10, 2014
1 parent d80fd09 commit 79c9572
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 18 deletions.
59 changes: 57 additions & 2 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use observr\Subject\SubjectTrait as SubjectTrait;
use observr\Subject\FixtureTrait as FixtureTrait;

class Event extends \ArrayObject implements EventInterface, EventAwareInterface, SubjectInterface {
class Event implements \ArrayAccess, \IteratorAggregate, EventInterface, EventAwareInterface, SubjectInterface {
use EventTrait, EventAwareTrait, SubjectTrait, FixtureTrait;

const FAILURE = 'fail';
Expand All @@ -18,6 +18,12 @@ class Event extends \ArrayObject implements EventInterface, EventAwareInterface,
const CANCEL = 'cancel';

const SUCCESS = 'success';

/**
* Locally stores event data
* @var array
*/
private $data;

function __construct($sender, array $args = null) {
if(is_array($sender)) {
Expand All @@ -30,7 +36,56 @@ function __construct($sender, array $args = null) {
}

if(is_array($args)) {
$this->exchangeArray($args);
$this->data = $args;
} else {
$this->data = [$args];
}
}

/**
* Allows native iteration over event data
* @return \ArrayIterator
*/
public function getIterator() {
return new \ArrayIterator($this->data);
}

/**
* Checks if event data exists at offset
* @param mixed $offset
* @return boolean
*/
public function offsetExists($offset) {
return isset($this->data[$offset]);
}

/**
* Retrieve event data from offset
* @param $offset
* @return mixed
*/
public function offsetGet($offset) {
if(isset($this->data[$offset])) {
return $this->data[$offset];
}
}

/**
* Modify event data at offset
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value) {
$this->data[$offset] = $value;
}

/**
* Remove event data at offset
* @param mixed $offset
*/
public function offsetUnset($offset) {
if(isset($this->data[$offset])) {
unset($this->data[$offset]);
}
}
}
Expand Down
82 changes: 67 additions & 15 deletions src/State/Listener/AggregateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,69 @@
use observr\State\Notifier\AggregateNotifier as AggregateNotifier;
use observr\State\Listener\ListenerInterface as ListenerInterface;

class AggregateListener extends \ArrayObject implements ListenerInterface, NotifierAwareInterface {
class AggregateListener implements \ArrayAccess, \IteratorAggregate, ListenerInterface, NotifierAwareInterface {
/**
* Locally stores listener/notifiers
* @var array
*/
private $listeners = [];

/**
* Default constructor for AggregateListener
* @param array $listeners
*/
public function __construct(array $listeners = []) {
$this->listeners = $listeners;
}

/**
* Check if listener is at index
* @param mixed $offset
* @return boolean
*/
public function offsetExists($offset) {
return isset($this->listeners[$offset]);
}

/**
* Retrieve listener from index
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset) {
if(isset($this->listeners[$offset])) {
return $this->listeners[$offset];
}
}

/**
* Remove listener from index
* @param mixed $offset
*/
public function offsetUnset($offset) {
if(isset($this->listeners[$offset])) {
unset($this->listeners[$offset]);
}
}

/**
* Allows native iteration over AggregateListener
* @return \ArrayIterator
*/
public function getIterator() {
return new \ArrayIterator($this->listeners);
}

/**
* Ensure new value is Listener
* @param mixed $index
* @param mixed $offset
* @param ListenerInterface $newval
*/
public function offsetSet($index, $newval) {
public function offsetSet($offset, $newval) {
if($newval instanceof ListenerInterface &&
$newval instanceof NotifierInterface) {
parent::offsetSet($index, $newval);
($newval instanceof NotifierInterface ||
$newval instanceof NotifierAwareInterface)) {
$this->listeners[$offset] = $newval;
}
}

Expand All @@ -25,11 +77,11 @@ public function offsetSet($index, $newval) {
* @return boolean
*/
public function isValid() {
if(count($this) === 0) {
if(count($this->listeners) === 0) {
return false;
}

foreach($this as $listener) {
foreach($this->listeners as $listener) {
if(!$listener->isValid()) {
return false;
}
Expand All @@ -43,7 +95,7 @@ public function isValid() {
* @param callable $callable
*/
public function watch($name, callable $callable) {
foreach($this as $listener) {
foreach($this->listeners as $listener) {
$listener->watch($name, $callable);
}
}
Expand All @@ -53,7 +105,7 @@ public function watch($name, callable $callable) {
* @param string|callable $observer
*/
public function unwatch($name = null) {
foreach($this as $listener) {
foreach($this->listeners as $listener) {
$listener->unwatch($name);
}
}
Expand All @@ -67,7 +119,7 @@ public function isWatched() {
return false;
}

foreach($this as $listener) {
foreach($this->listeners as $listener) {
if(!$listener->isWatched()) {
return false;
}
Expand All @@ -83,7 +135,7 @@ public function isWatched() {
*/
public function notify(EventAwareInterface $event) {
$results = [];
foreach($this as $listener) {
foreach($this->listeners as $listener) {
$results = array_merge($results,$listener->notify($event));
}

Expand All @@ -97,7 +149,7 @@ public function notify(EventAwareInterface $event) {
public function getStates() {
$states = [];

foreach($this as $listener) {
foreach($this->listeners as $listener) {
$states = array_merge($states,$listener->getStates());
}
return $states;
Expand All @@ -110,7 +162,7 @@ public function getStates() {
public function getName() {
$names = [];

foreach($this as $listener) {
foreach($this->listeners as $listener) {
$names[] = $listener->getName();
}

Expand All @@ -123,7 +175,7 @@ public function getName() {
*/
public function getNotifier() {
$notifiers = [];
foreach($this as $listener) {
foreach($this->listeners as $listener) {
if($listener instanceof NotifierAwareInterface) {
$notifiers[] = $listener->getNotifier();
}
Expand All @@ -138,7 +190,7 @@ public function getNotifier() {
*/
public function getRunCount() {
$count = 0;
foreach($this as $listener) {
foreach($this->listeners as $listener) {
$count += $listener->getRunCount();
}

Expand Down
54 changes: 53 additions & 1 deletion src/State/Notifier/AggregateNotifier.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,58 @@
<?php
namespace observr\State\Notifier {
class AggregateNotifier extends \ArrayObject implements NotifierInterface {
class AggregateNotifier implements \ArrayAccess, \IteratorAggregate, NotifierInterface {
/**
* Locally stores listener/notifiers
* @var array
*/
private $notifiers = [];

/**
* Default constructor for AggregateListener
* @param array $notifiers
*/
public function __construct(array $notifiers = []) {
$this->notifiers = $notifiers;
}

/**
* Check if listener is at index
* @param mixed $offset
* @return boolean
*/
public function offsetExists($offset) {
return isset($this->notifiers[$offset]);
}

/**
* Retrieve listener from index
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset) {
if(isset($this->notifiers[$offset])) {
return $this->notifiers[$offset];
}
}

/**
* Remove listener from index
* @param mixed $offset
*/
public function offsetUnset($offset) {
if(isset($this->notifiers[$offset])) {
unset($this->notifiers[$offset]);
}
}

/**
* Allows native iteration over AggregateListener
* @return \ArrayIterator
*/
public function getIterator() {
return new \ArrayIterator($this->notifiers);
}

/**
* Ensure new value is Notifier
* @param mixed $index
Expand Down

0 comments on commit 79c9572

Please sign in to comment.