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