Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
94.74% covered (success)
94.74%
18 / 19
CRAP
78.26% covered (success)
78.26%
36 / 46
Reference
0.00% covered (danger)
0.00%
0 / 1
94.74% covered (success)
94.74%
18 / 19
31.42
78.26% covered (success)
78.26%
36 / 46
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 fromXMLReference
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 10
 newReferenceMapping
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 update
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
6 / 6
 getConstraintName
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 setConstraintName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getMethodName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getForeignTable
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setForeignTable
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getOnDelete
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 setOnDelete
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getOnUpdate
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 setOnUpdate
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getReferenceMappingList
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getForeignEntity
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 doesCollectionRefersTo
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setCollectionRefersTo
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
<?php
declare(strict_types = 1);
namespace Siesta\Model;
use Siesta\XML\XMLReference;
/**
 * @author Gregor Müller
 */
class Reference
{
    const ON_X_RESTRICT = "restrict";
    const ON_X_CASCADE = "cascade";
    const ON_X_SET_NULL = "set null";
    const ON_X_NONE = "no action";
    /**
     * @var DataModel
     */
    protected $dataModel;
    /**
     * @var Entity
     */
    protected $entity;
    /**
     * @var string
     */
    protected $name;
    /**
     * @var string
     */
    protected $constraintName;
    /**
     * @var string
     */
    protected $foreignTable;
    /**
     * @var string
     */
    protected $onDelete;
    /**
     * @var string
     */
    protected $onUpdate;
    /**
     * @var bool
     */
    protected $collectionRefersTo;
    /**
     * @var ReferenceMapping[]
     */
    protected $referenceMappingList;
    // from here derived
    /**
     * @var Entity
     */
    protected $foreignEntity;
    /**
     * Reference constructor.
     *
     * @param DataModel $dataModel
     * @param Entity $entity
     */
    public function __construct(DataModel $dataModel, Entity $entity)
    {
        $this->dataModel = $dataModel;
        $this->entity = $entity;
        $this->referenceMappingList = [];
    }
    /**
     * @param XMLReference $xmlReference
     */
    public function fromXMLReference(XMLReference $xmlReference)
    {
        $this->setName($xmlReference->getName());
        $this->setConstraintName($xmlReference->getConstraintName());
        $this->setForeignTable($xmlReference->getForeignTable());
        $this->setOnUpdate($xmlReference->getOnUpdate());
        $this->setOnDelete($xmlReference->getOnDelete());
        foreach ($xmlReference->getXmlReferenceMappingList() as $xmlReferenceMapping) {
            $referenceMapping = new ReferenceMapping($this->dataModel, $this->entity);
            $referenceMapping->fromXMLReferenceMaping($xmlReferenceMapping);
            $this->referenceMappingList[] = $referenceMapping;
        }
    }
    /**
     * @return ReferenceMapping
     */
    public function newReferenceMapping() : ReferenceMapping
    {
        $referenceMapping = new ReferenceMapping($this->dataModel, $this->entity);
        $this->referenceMappingList[] = $referenceMapping;
        return $referenceMapping;
    }
    public function update()
    {
        $this->foreignEntity = $this->dataModel->getEntityByTableName($this->getForeignTable());
        if ($this->foreignEntity === null) {
            return;
        }
        foreach ($this->referenceMappingList as $referenceMapping) {
            $referenceMapping->update($this->foreignEntity);
        }
    }
    /**
     * @return string
     */
    public function getConstraintName()
    {
        if ($this->constraintName !== null) {
            return $this->constraintName;
        }
        return $this->entity->getTableName() . "_" . $this->getName();
    }
    /**
     * @param string $constraintName
     */
    public function setConstraintName($constraintName)
    {
        $this->constraintName = $constraintName;
    }
    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }
    /**
     * @return string
     */
    public function getMethodName()
    {
        return ucfirst($this->getName());
    }
    /**
     * @return string
     */
    public function getForeignTable()
    {
        return $this->foreignTable;
    }
    /**
     * @param string $foreignTable
     */
    public function setForeignTable($foreignTable)
    {
        $this->foreignTable = $foreignTable;
    }
    /**
     * @return string
     */
    public function getOnDelete()
    {
        return $this->onDelete !== null ? $this->onDelete : "restrict";
    }
    /**
     * @param string $onDelete
     */
    public function setOnDelete($onDelete)
    {
        $this->onDelete = $onDelete;
    }
    /**
     * @return string
     */
    public function getOnUpdate()
    {
        return $this->onUpdate !== null ? $this->onUpdate : "restrict";
    }
    /**
     * @param string $onUpdate
     */
    public function setOnUpdate($onUpdate)
    {
        $this->onUpdate = $onUpdate;
    }
    /**
     * @return ReferenceMapping[]
     */
    public function getReferenceMappingList()
    {
        return $this->referenceMappingList;
    }
    /**
     * @return Entity
     */
    public function getForeignEntity()
    {
        return $this->foreignEntity;
    }
    /**
     * @return boolean
     */
    public function doesCollectionRefersTo()
    {
        return $this->collectionRefersTo;
    }
    /**
     *
     */
    public function setCollectionRefersTo()
    {
        $this->collectionRefersTo = true;
    }
}