2016-08-04 05:59:04 +02:00
|
|
|
<?php
|
|
|
|
require_once 'models/DataStore.php';
|
|
|
|
|
2016-09-22 23:23:53 +02:00
|
|
|
class DataStoreList implements IteratorAggregate {
|
2016-08-04 05:59:04 +02:00
|
|
|
private $list = [];
|
|
|
|
|
|
|
|
public static function getList($type, $beanList) {
|
|
|
|
$dataStoreList = new DataStoreList();
|
|
|
|
|
|
|
|
foreach ($beanList as $bean) {
|
|
|
|
$dataStoreList->add(new $type($bean));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dataStoreList;
|
|
|
|
}
|
|
|
|
|
2016-09-09 05:38:58 +02:00
|
|
|
public function getIterator() {
|
|
|
|
return new ArrayIterator($this->list);
|
|
|
|
}
|
|
|
|
|
2016-08-04 05:59:04 +02:00
|
|
|
public function add(DataStore $dataStore) {
|
|
|
|
$this->list[] = $dataStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove(DataStore $dataStore) {
|
|
|
|
$dataStoreIndexInList = $this->getIndexInListOf($dataStore);
|
|
|
|
|
2016-11-23 02:27:05 +01:00
|
|
|
if ($dataStoreIndexInList == -1) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
unset($this->list[$dataStoreIndexInList]);
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-04 05:59:04 +02:00
|
|
|
}
|
|
|
|
|
2016-12-14 01:10:49 +01:00
|
|
|
public function includesId($id) {
|
|
|
|
$includes = false;
|
|
|
|
|
|
|
|
foreach($this->list as $item) {
|
|
|
|
if($item->id == $id) {
|
|
|
|
$includes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $includes;
|
|
|
|
}
|
|
|
|
|
2016-08-04 05:59:04 +02:00
|
|
|
public function toBeanList() {
|
|
|
|
$beanList = [];
|
|
|
|
|
|
|
|
foreach($this->list as $item) {
|
2016-08-04 06:37:23 +02:00
|
|
|
$item->updateBeanProperties();
|
2016-08-04 05:59:04 +02:00
|
|
|
$beanList[] = $item->getBeanInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $beanList;
|
|
|
|
}
|
2016-10-03 21:44:41 +02:00
|
|
|
|
|
|
|
public function toArray() {
|
|
|
|
$array = [];
|
|
|
|
|
|
|
|
foreach($this->list as $item) {
|
|
|
|
$item->updateBeanProperties();
|
|
|
|
$array[] = $item->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
2016-08-04 05:59:04 +02:00
|
|
|
|
|
|
|
private function getIndexInListOf($dataStore) {
|
|
|
|
foreach ($this->list as $itemIdInList => $item) {
|
|
|
|
if ($item->id === $dataStore->id) {
|
|
|
|
return $itemIdInList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|