| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* This source file is subject to the new BSD license that is bundled |
| 6 |
|
* with this package in the file LICENSE.txt. |
| 7 |
|
* It is also available through the world-wide-web at this URL: |
| 8 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 9 |
|
* |
| 10 |
|
* @category Xyster |
| 11 |
|
* @package Xyster_Data |
| 12 |
|
* @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net) |
| 13 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 14 |
|
* @version $Id: Clause.php 219 2008-02-09 18:02:48Z doublecompile $ |
| 15 |
|
*/ |
| 16 |
|
/** |
| 17 |
|
* @see Xyster_Data_Clause_Interface |
| 18 |
|
*/ |
| 19 |
1 |
require_once 'Xyster/Data/Clause/Interface.php'; |
| 20 |
|
/** |
| 21 |
|
* @see Xyster_Type |
| 22 |
|
*/ |
| 23 |
1 |
require_once 'Xyster/Type.php'; |
| 24 |
|
/** |
| 25 |
|
* Abstract clause of symbols |
| 26 |
|
* |
| 27 |
|
* @category Xyster |
| 28 |
|
* @package Xyster_Data |
| 29 |
|
* @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net) |
| 30 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 31 |
|
*/ |
| 32 |
|
abstract class Xyster_Data_Clause implements Xyster_Data_Clause_Interface |
| 33 |
|
{ |
| 34 |
|
/** |
| 35 |
|
* @var array |
| 36 |
|
*/ |
| 37 |
|
protected $_items = array(); |
| 38 |
|
|
| 39 |
|
/** |
| 40 |
|
* @var Xyster_Type |
| 41 |
|
*/ |
| 42 |
|
private $_type; |
| 43 |
|
|
| 44 |
|
/** |
| 45 |
|
* Creates a new data clause |
| 46 |
|
* |
| 47 |
|
* Extending classes MUST pass the type to this constructor for the class to |
| 48 |
|
* work as expected. |
| 49 |
|
* |
| 50 |
|
* @param Xyster_Type $type The class type allowed in this clause |
| 51 |
|
* @param Xyster_Data_Symbol $symbol A clause or symbol to add |
| 52 |
|
*/ |
| 53 |
|
public function __construct( Xyster_Type $type, Xyster_Data_Symbol $symbol = null ) |
| 54 |
|
{ |
| 55 |
82 |
$this->_type = $type; |
| 56 |
82 |
if ( $symbol instanceof Xyster_Data_Clause ) { |
| 57 |
8 |
$this->merge($symbol); |
| 58 |
82 |
} else if ( $symbol instanceof Xyster_Data_Clause_Interface ) { |
| 59 |
|
foreach( $symbol as $v ) { |
| 60 |
|
$this->add($v); |
| 61 |
|
} |
| 62 |
82 |
} else if ( $symbol !== null ) { |
| 63 |
7 |
$this->add($symbol); |
| 64 |
7 |
} |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
/** |
| 68 |
|
* Adds an item to this clause |
| 69 |
|
* |
| 70 |
|
* @param Xyster_Data_Symbol $symbol |
| 71 |
|
* @return Xyster_Data_Clause provides a fluent interface |
| 72 |
|
*/ |
| 73 |
|
public function add( Xyster_Data_Symbol $symbol ) |
| 74 |
|
{ |
| 75 |
62 |
if ( !$this->_type->isInstance($symbol) ) { |
| 76 |
2 |
require_once 'Xyster/Data/Clause/Exception.php'; |
| 77 |
2 |
throw new Xyster_Data_Clause_Exception("This clause only supports " . $this->_type); |
| 78 |
0 |
} |
| 79 |
62 |
$this->_items[] = $symbol; |
| 80 |
62 |
return $this; |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
/** |
| 84 |
|
* Gets the number of entries in the clause |
| 85 |
|
* |
| 86 |
|
* @return int |
| 87 |
|
*/ |
| 88 |
|
public function count() |
| 89 |
|
{ |
| 90 |
30 |
return count($this->_items); |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
/** |
| 94 |
|
* Gets the iterator for this clause |
| 95 |
|
* |
| 96 |
|
* @return Iterator |
| 97 |
|
*/ |
| 98 |
|
public function getIterator() |
| 99 |
|
{ |
| 100 |
31 |
$iterator = null; |
| 101 |
31 |
if ( count($this->_items) ) { |
| 102 |
31 |
require_once 'Xyster/Collection/Iterator.php'; |
| 103 |
31 |
$iterator = new Xyster_Collection_Iterator($this->_items); |
| 104 |
31 |
} else { |
| 105 |
11 |
$iterator = new EmptyIterator; |
| 106 |
|
} |
| 107 |
31 |
return $iterator; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
/** |
| 111 |
|
* Adds the items from one clause to the end of this one |
| 112 |
|
* |
| 113 |
|
* @param Xyster_Data_Clause $clause |
| 114 |
|
* @return Xyster_Data_Clause provides a fluent interface |
| 115 |
|
*/ |
| 116 |
|
public function merge( Xyster_Data_Clause $clause ) |
| 117 |
|
{ |
| 118 |
12 |
if ( !$this->_type->equals($clause->_type) ) { |
| 119 |
2 |
require_once 'Xyster/Data/Clause/Exception.php'; |
| 120 |
2 |
throw new Xyster_Data_Clause_Exception("This clause only supports " . $this->_type); |
| 121 |
0 |
} |
| 122 |
10 |
$this->_items = array_merge($this->_items, $clause->_items); |
| 123 |
10 |
return $this; |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
/** |
| 127 |
|
* Removes an entry in the clause |
| 128 |
|
* |
| 129 |
|
* @param Xyster_Data_Symbol $symbol |
| 130 |
|
* @return boolean Whether the clause was changed |
| 131 |
|
*/ |
| 132 |
|
public function remove( Xyster_Data_Symbol $symbol ) |
| 133 |
|
{ |
| 134 |
4 |
foreach( $this->_items as $k => $v ) { |
| 135 |
4 |
if ( Xyster_Type::areDeeplyEqual($v, $symbol) ) { |
| 136 |
2 |
unset($this->_items[$k]); |
| 137 |
2 |
return true; |
| 138 |
0 |
} |
| 139 |
2 |
} |
| 140 |
2 |
return false; |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
/** |
| 144 |
|
* Converts the clause into an array of its symbols |
| 145 |
|
* |
| 146 |
|
* @return array |
| 147 |
|
*/ |
| 148 |
|
public function toArray() |
| 149 |
|
{ |
| 150 |
11 |
return array_values($this->_items); |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
/** |
| 154 |
|
* Gets the string representation of this object |
| 155 |
|
* |
| 156 |
|
* @magic |
| 157 |
|
* @return string |
| 158 |
|
*/ |
| 159 |
|
public function __toString() |
| 160 |
|
{ |
| 161 |
2 |
return implode(', ', $this->_items); |
| 162 |
|
} |
| 163 |
|
} |