Part-DB.Part-DB-server/src/Entity/Parts/PartTraits/ProjectTrait.php

87 lines
2.4 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Entity\Parts\PartTraits;
use App\Entity\ProjectSystem\Project;
use App\Entity\ProjectSystem\ProjectBOMEntry;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
trait ProjectTrait
{
2023-06-11 14:55:06 +02:00
/**
* @var Collection<ProjectBOMEntry> $project_bom_entries
*/
2024-03-03 19:57:31 +01:00
#[ORM\OneToMany(mappedBy: 'part', targetEntity: ProjectBOMEntry::class, cascade: ['remove'], orphanRemoval: true)]
2023-06-11 14:55:06 +02:00
protected Collection $project_bom_entries;
/**
* @var Project|null If a project is set here, then this part is special and represents the builds of a project.
*/
2024-03-03 19:57:31 +01:00
#[ORM\OneToOne(inversedBy: 'build_part', targetEntity: Project::class)]
#[ORM\JoinColumn]
protected ?Project $built_project = null;
/**
* Returns all ProjectBOMEntries that use this part.
*
* @phpstan-return Collection<int, ProjectBOMEntry>
*/
public function getProjectBomEntries(): Collection
{
return $this->project_bom_entries;
}
/**
* Checks whether this part represents the builds of a project
* @return bool True if it represents the builds, false if not
*/
#[Groups(['part:read'])]
public function isProjectBuildPart(): bool
{
return $this->built_project !== null;
}
/**
2023-04-15 23:14:53 +02:00
* Returns the project that this part represents the builds of, or null if it doesn't
*/
public function getBuiltProject(): ?Project
{
return $this->built_project;
}
/**
* Sets the project that this part represents the builds of
* @param Project|null $built_project The project that this part represents the builds of, or null if it is not a build part
*/
public function setBuiltProject(?Project $built_project): self
{
$this->built_project = $built_project;
return $this;
}
/**
* Get all projects which uses this part.
*
* @return Project[] * all devices which uses this part as a one-dimensional array of Device objects
* (empty array if there are no ones)
* * the array is sorted by the devices names
*/
public function getProjects(): array
{
$projects = [];
foreach($this->project_bom_entries as $entry) {
$projects[] = $entry->getProject();
}
return $projects;
}
}