Maslosoft Manganel API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/manganel
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
* @link https://maslosoft.com/manganel/
*/
namespace Maslosoft\Manganel\Traits;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
/**
* Use this trait to provide mutli-model feature to classes.
*
* NOTE: It will not check for uniqueness of models
* Use this if uniqueness is ensured already
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
trait ModelsAwareTrait
{
/**
* Annotated model
* @var AnnotatedInterface[]
*/
private $models = [];
/**
* Annotated models
* @var AnnotatedInterface[]
*/
public function getModels()
{
return $this->models;
}
/**
*
* @param AnnotatedInterface[] $models
* @return $this
*/
public function setModels($models)
{
$this->models = $models;
return $this;
}
/**
* Add model to set but only if it's instance is not present in set.
*
* @param AnnotatedInterface $model
* @return boolean Whether model was added
*/
public function addModel(AnnotatedInterface $model)
{
$this->models[] = $model;
}
/**
* Remove model from set.
*
* @param AnnotatedInterface $model
* @return boolean Whether model was removed
*/
public function removeModel(AnnotatedInterface $model)
{
if (!$this->hasModel($model))
{
return false;
}
foreach ($this->models as $index => $existing)
{
if (get_class($existing) === get_class($model))
{
unset($this->models[$index]);
return true;
}
}
// Should not happen, model not found even if should exists
return false;
}
public function hasModel(AnnotatedInterface $model)
{
foreach ($this->models as $existing)
{
if (get_class($existing) === get_class($model))
{
return true;
}
}
return false;
}
}
API documentation generated by ApiGen