Code Coverage for /home/user/workspace/all/Pyrus/src/Pyrus/DirectedGraph/Vertex.php

Coverage: 55%

Aggregate Code Coverage for all tests

       1           : <?php
       2           : /**
       3           :  * PEAR2_Pyrus_DirectedGraph_Vertex
       4           :  *
       5           :  * PHP version 5
       6           :  *
       7           :  * @category  PEAR2
       8           :  * @package   PEAR2_Pyrus
       9           :  * @author    Greg Beaver <cellog@php.net>
      10           :  * @copyright 2008 The PEAR Group
      11           :  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
      12           :  * @version   SVN: $Id$
      13           :  * @link      http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/
      14           :  */
      15           : 
      16           : /**
      17           :  * Class to represent vertices within the dependency directed graph.
      18           :  *
      19           :  * @category  PEAR2
      20           :  * @package   PEAR2_Pyrus
      21           :  * @author    Greg Beaver <cellog@php.net>
      22           :  * @copyright 2008 The PEAR Group
      23           :  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
      24           :  * @link      http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/
      25           :  */
      26         3 : class PEAR2_Pyrus_DirectedGraph_Vertex implements ArrayAccess, Countable, Iterator
      27           : {
      28           :     const WHITE = PEAR2_Pyrus_DirectedGraph::WHITE;
      29           :     const GRAY = PEAR2_Pyrus_DirectedGraph::GRAY;
      30           :     const BLACK = PEAR2_Pyrus_DirectedGraph::BLACK;
      31           :     protected $color = self::WHITE;
      32           :     public $data;
      33           :     protected $connections = array();
      34           : 
      35           :     /**
      36           :      * Encapsulate data within a directed graph vertex
      37           :      *
      38           :      * @param object $data
      39           :      */
      40           :     function __construct($data)
      41           :     {
      42         3 :         if (!is_object($data)) {
      43           :             throw new PEAR2_Pyrus_DirectedGraph_Exception('data must be an object, was ' .
      44           :                 gettype($data));
      45           :         }
      46           : 
      47         3 :         $this->data = $data;
      48         3 :     }
      49           : 
      50           :     /**
      51           :      * Connect to another vertex
      52           :      *
      53           :      * @param PEAR2_Pyrus_DirectedGraph_Vertex $to
      54           :      */
      55           :     function connect(PEAR2_Pyrus_DirectedGraph_Vertex $to)
      56           :     {
      57         2 :         $this->connections[spl_object_hash($to)] = $to;
      58         2 :     }
      59           : 
      60           :     /**
      61           :      * Set the color of a visited node
      62           :      *
      63           :      * WHITE = unvisited, GRAY = visited, BLACK = finished
      64           :      *
      65           :      * @param self::WHITE|self::GRAY|self::BLACK|null $color if null, return the current color
      66           :      * @return int
      67           :      */
      68           :     function color($color = null)
      69           :     {
      70         3 :         if ($color === null) {
      71         3 :             return $this->color;
      72           :         }
      73           : 
      74         3 :         $this->color = $color;
      75         3 :     }
      76           : 
      77           :     function count()
      78           :     {
      79         3 :         $count = count($this->connections);
      80         3 :         foreach ($this->connections as $node) {
      81         2 :             if ($node->color() != self::WHITE) {
      82         2 :                 --$count;
      83         2 :             }
      84         3 :         }
      85           : 
      86         3 :         return $count;
      87           :     }
      88           : 
      89           :     function offsetGet($var)
      90           :     {
      91           :         return $this->connections[$var];
      92           :     }
      93           : 
      94           :     function offsetSet($var, $value)
      95           :     {
      96           :         if ($value instanceof PEAR2_Pyrus_DirectedGraph_Vertex) {
      97           :             $this->connect($value);
      98           :         }
      99           :     }
     100           : 
     101           :     function offsetExists($var)
     102           :     {
     103           :         return isset($this->connections[$var]);
     104           :     }
     105           : 
     106           :     function offsetUnset($var)
     107           :     {
     108           :         unset($this->connections[$var]);
     109           :     }
     110           : 
     111           :     function current()
     112           :     {
     113         2 :         return current($this->connections);
     114           :     }
     115           : 
     116           :     function next()
     117           :     {
     118         2 :         return next($this->connections);
     119           :     }
     120           : 
     121           :     function key()
     122           :     {
     123           :         return key($this->connections);
     124           :     }
     125           : 
     126           :     function valid()
     127           :     {
     128         2 :         return current($this->connections);
     129           :     }
     130           : 
     131           :     function rewind()
     132           :     {
     133         2 :         reset($this->connections);
     134         2 :     }
     135         3 : }