76% of 414OPs |
86% of 66Lines |
64% of 78Branches |
46% of 46Paths |
| # | |
|---|---|
| 1 |
<?php |
| 2 |
|
| 3 |
namespace mageekguy\atoum; |
| 4 |
|
| 5 |
use |
| 6 |
mageekguy\atoum\template, |
| 7 |
mageekguy\atoum\exceptions |
| 8 |
; |
| 9 |
|
| 10 |
class template extends template\data |
| 11 |
{
|
| 12 |
protected $children = array(); |
| 13 |
|
| 14 |
public function __set($tag, $data)100% |
| 15 |
{
|
| 16 |
foreach ($this->getByTag($tag) as $child) |
| 17 |
{
|
| 18 |
$child->setData($data); |
| 19 |
} |
| 20 |
|
| 21 |
return $this; |
| 22 |
} |
| 23 |
|
| 24 |
public function __get($tag)100% |
| 25 |
{
|
| 26 |
return $this->getByTag($tag); |
| 27 |
} |
| 28 |
|
| 29 |
public function __unset($tag)100% |
| 30 |
{
|
| 31 |
foreach ($this->getByTag($tag) as $child) |
| 32 |
{
|
| 33 |
$child->resetData(); |
| 34 |
} |
| 35 |
|
| 36 |
return $this; |
| 37 |
} |
| 38 |
|
| 39 |
public function __isset($tag)100% |
| 40 |
{
|
| 41 |
return (sizeof($this->getByTag($tag)) > 0); |
| 42 |
} |
| 43 |
|
| 44 |
public function getByTag($tag)100% |
| 45 |
{
|
| 46 |
$iterator = new template\iterator(); |
| 47 |
|
| 48 |
return $iterator->addTag($tag, $this); |
| 49 |
} |
| 50 |
|
| 51 |
public function getById($id, $fromRoot = true)100% |
| 52 |
{
|
| 53 |
$root = $fromRoot === false ? $this : $this->getRoot(); |
| 54 |
|
| 55 |
if ($root->getId() === $id) |
| 56 |
{
|
| 57 |
return $root; |
| 58 |
} |
| 59 |
else |
| 60 |
{
|
| 61 |
foreach ($root->children as $child) |
| 62 |
{
|
| 63 |
$tag = $child->getById($id, false); |
| 64 |
|
| 65 |
if ($tag !== null) |
| 66 |
{
|
| 67 |
return $tag; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
return null; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public function getChild($rank)100% |
| 76 |
{
|
| 77 |
return (isset($this->children[$rank]) === false ? null : $this->children[$rank]); |
| 78 |
} |
| 79 |
|
| 80 |
public function getChildren()100% |
| 81 |
{
|
| 82 |
return array_values($this->children); |
| 83 |
} |
| 84 |
|
| 85 |
public function setWith($mixed)75% |
| 86 |
{
|
| 87 |
foreach ($mixed as $tag => $value) |
| 88 |
{
|
| 89 |
$this->{$tag} = $value;
|
| 90 |
} |
| 91 |
|
| 92 |
return $this; |
| 93 |
} |
| 94 |
|
| 95 |
public function resetChildrenData()0% |
| 96 |
{
|
| 97 |
foreach ($this->children as $child) |
| 98 |
{
|
| 99 |
$child->resetData(); |
| 100 |
} |
| 101 |
|
| 102 |
return $this; |
| 103 |
} |
| 104 |
|
| 105 |
public function build($mixed = array())100% |
| 106 |
{
|
| 107 |
foreach ($this->setWith($mixed)->children as $child) |
| 108 |
{
|
| 109 |
$this->addData($child->getData()); |
| 110 |
} |
| 111 |
|
| 112 |
return parent::build(); |
| 113 |
} |
| 114 |
|
| 115 |
public function hasChildren()100% |
| 116 |
{
|
| 117 |
return (sizeof($this->children) > 0); |
| 118 |
} |
| 119 |
|
| 120 |
public function isChild(template\data $child)100% |
| 121 |
{
|
| 122 |
return ($child->parent === $this); |
| 123 |
} |
| 124 |
|
| 125 |
public function addToParent($mixed = array())0% |
| 126 |
{
|
| 127 |
$this->setWith($mixed); |
| 128 |
|
| 129 |
return parent::addToParent(); |
| 130 |
} |
| 131 |
|
| 132 |
public function addChild(template\data $child)100% |
| 133 |
{
|
| 134 |
if ($this->isChild($child) === false) |
| 135 |
{
|
| 136 |
$id = $child->getId(); |
| 137 |
|
| 138 |
if ($id !== null && $this->idExists($id) === true) |
| 139 |
{
|
| 140 |
throw new exceptions\runtime('Id \'' . $id . '\' is already defined');
|
| 141 |
} |
| 142 |
|
| 143 |
if ($child->parentIsSet() === true) |
| 144 |
{
|
| 145 |
$child->unsetParent(); |
| 146 |
} |
| 147 |
|
| 148 |
$child->rank = sizeof($this->children); |
| 149 |
$this->children[$child->rank] = $child; |
| 150 |
$child->parent = $this; |
| 151 |
} |
| 152 |
|
| 153 |
return $this; |
| 154 |
} |
| 155 |
|
| 156 |
public function deleteChild(template\data $child)100% |
| 157 |
{
|
| 158 |
if ($this->isChild($child) === true) |
| 159 |
{
|
| 160 |
unset($this->children[$child->rank]); |
| 161 |
$child->parent = null; |
| 162 |
$child->rank = null; |
| 163 |
} |
| 164 |
|
| 165 |
return $this; |
| 166 |
} |
| 167 |
|
| 168 |
public function idExists($id)100% |
| 169 |
{
|
| 170 |
return ($this->getById($id) !== null); |
| 171 |
} |
| 172 |
|
| 173 |
public function setAttribute($name, $value) {}
|
| 174 |
|
| 175 |
public function unsetAttribute($name) {}
|
| 176 |
} |