2022-12-29 13:27:33 +01:00
|
|
|
<?php
|
|
|
|
|
2023-06-11 18:59:07 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-12-29 13:27:33 +01:00
|
|
|
namespace App\Entity\Parts\PartTraits;
|
|
|
|
|
|
|
|
use App\Entity\ProjectSystem\Project;
|
|
|
|
use App\Entity\ProjectSystem\ProjectBOMEntry;
|
2023-05-28 01:51:13 +02:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2022-12-29 13:27:33 +01:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2023-09-03 23:58:09 +02:00
|
|
|
use Symfony\Component\Serializer\Annotation\Groups;
|
2022-12-29 13:27:33 +01:00
|
|
|
|
|
|
|
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;
|
2022-12-29 13:27:33 +01:00
|
|
|
|
2022-12-29 15:22:34 +01:00
|
|
|
/**
|
|
|
|
* @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)]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\JoinColumn]
|
2022-12-29 15:22:34 +01:00
|
|
|
protected ?Project $built_project = null;
|
|
|
|
|
2022-12-29 13:27:33 +01:00
|
|
|
/**
|
2023-06-18 15:37:42 +02:00
|
|
|
* Returns all ProjectBOMEntries that use this part.
|
|
|
|
*
|
|
|
|
* @phpstan-return Collection<int, ProjectBOMEntry>
|
2022-12-29 13:27:33 +01:00
|
|
|
*/
|
|
|
|
public function getProjectBomEntries(): Collection
|
|
|
|
{
|
|
|
|
return $this->project_bom_entries;
|
|
|
|
}
|
|
|
|
|
2022-12-29 15:22:34 +01:00
|
|
|
/**
|
|
|
|
* Checks whether this part represents the builds of a project
|
|
|
|
* @return bool True if it represents the builds, false if not
|
|
|
|
*/
|
2023-09-03 23:58:09 +02:00
|
|
|
#[Groups(['part:read'])]
|
2022-12-29 15:22:34 +01:00
|
|
|
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
|
2022-12-29 15:22:34 +01:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-29 13:27:33 +01:00
|
|
|
/**
|
2023-01-08 20:10:58 +01:00
|
|
|
* Get all projects which uses this part.
|
2022-12-29 13:27:33 +01:00
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
2023-06-11 18:59:07 +02:00
|
|
|
}
|