-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
380c8f7
commit a97cb88
Showing
6 changed files
with
228 additions
and
7 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,19 @@ | ||
Copyright (c) 2012 Lewis Dyer | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,193 @@ | ||
<?php | ||
namespace ReverseRegex\Generator; | ||
|
||
use \ArrayObject; | ||
use \SplObjectStorage; | ||
use \ArrayAccess; | ||
use \Countable; | ||
use \Iterator; | ||
|
||
/** | ||
* Base to all Generator Scopes | ||
* | ||
* @author Lewis Dyer <getintouch@icomefromthenet.com> | ||
* @since 0.0.1 | ||
*/ | ||
class Node implements ArrayAccess, Countable, Iterator | ||
{ | ||
/** | ||
* @var string name of the node | ||
*/ | ||
protected $label; | ||
|
||
/** | ||
* @var ArrayObject container for node metadata | ||
*/ | ||
protected $attrs; | ||
|
||
/** | ||
* @var SplObjectStorage container for node relationships | ||
*/ | ||
protected $links; | ||
|
||
/** | ||
* Class Constructor | ||
* | ||
* @access public | ||
* @param string $label | ||
*/ | ||
public function __construct($label = 'node') | ||
{ | ||
$this->attrs = new ArrayObject(); | ||
$this->links = new SplObjectStorage(); | ||
|
||
$this->setLabel($label); | ||
} | ||
|
||
/** | ||
* Fetch the nodes label | ||
* | ||
* @access public | ||
* @return string the nodes label | ||
*/ | ||
public function getLabel() | ||
{ | ||
return $this->label; | ||
} | ||
|
||
/** | ||
* Sets the node label | ||
* | ||
* @access public | ||
* @param string $label the nodes label | ||
*/ | ||
public function setLabel($label) | ||
{ | ||
if (!(is_scalar($label) || is_null($label))) { | ||
return false; | ||
} | ||
|
||
$this->label = $label; | ||
} | ||
|
||
|
||
/** | ||
* Attach a node | ||
* | ||
* @access public | ||
* @param Node $node the node to attach | ||
* @return Node | ||
*/ | ||
public function &attach(Node $node) | ||
{ | ||
$this->links->attach($node); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Detach a node | ||
* | ||
* @access public | ||
* @return Node | ||
* @param Node $node the node to remove | ||
*/ | ||
public function &detach(Node $node) | ||
{ | ||
foreach ($this->links as $linked_node) { | ||
if ($linked_node == $node) { | ||
$this->links->detach($node); | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Search for node in its relations | ||
* | ||
* @access public | ||
* @return boolean true if found | ||
* @param Node $node the node to search for | ||
*/ | ||
public function contains(Node $node) | ||
{ | ||
foreach ($this->links as $linked_node) { | ||
if ($linked_node == $node) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Apply a closure to all relations | ||
* | ||
* @access public | ||
* @param Closer the function to apply | ||
*/ | ||
public function map(Closure $function) | ||
{ | ||
foreach ($this->links as $node) { | ||
$function($node); | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------ | ||
# Countable | ||
|
||
public function count() | ||
{ | ||
return count($this->links); | ||
} | ||
|
||
//------------------------------------------------------------------ | ||
# Iterator | ||
|
||
public function current() | ||
{ | ||
return $this->links->current(); | ||
} | ||
public function key() | ||
{ | ||
return $this->links->key(); | ||
} | ||
public function next() | ||
{ | ||
return $this->links->next(); | ||
} | ||
public function rewind() | ||
{ | ||
return $this->links->rewind(); | ||
} | ||
public function valid() | ||
{ | ||
return $this->links->valid(); | ||
} | ||
|
||
//------------------------------------------------------------------ | ||
# ArrayAccess Implementation | ||
|
||
public function offsetGet($key) | ||
{ | ||
return $this->attrs->offsetGet($key); | ||
} | ||
|
||
public function offsetSet($key, $value) | ||
{ | ||
$this->attrs->offsetSet($key, $value); | ||
} | ||
|
||
public function offsetExists($key) | ||
{ | ||
return $this->attrs->offsetExists($key); | ||
} | ||
|
||
public function offsetUnset($key) | ||
{ | ||
return $this->attrs->offsetUnset($key); | ||
} | ||
} | ||
|
||
/* End of Class */ |
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