| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Network;
- use App\Entity\Organization\Organization;
- use App\Enum\Organization\PrincipalTypeEnum;
- use App\Repository\Network\NetworkOrganizationRepository;
- use App\Tests\Service\Network\TreeTest;
- /**
- * Class Tree : service rassemblant des fonction pour répondre aux poblématique d'arbre du réseau
- * @package App\Service\Network
- */
- class Tree
- {
- private NetworkOrganizationRepository $networkOrganizationRepository;
- public function __construct(NetworkOrganizationRepository $networkOrganizationRepository)
- {
- $this->networkOrganizationRepository = $networkOrganizationRepository;
- }
- /**
- * Retrouve tous les parents d'une structure et les tries selon leur type principal
- * @param Organization $organization
- * @return array
- */
- public function findAllParentsAndSortByType(Organization $organization): array {
- return $this->sortByType($this->networkOrganizationRepository->findAllParents($organization));
- }
- /**
- * Trie les organisations par rapport à leur type principal :
- * DELEGATION, GROUPMENT, LOCAL_FEDERATION, DEPARTEMENTAL_FEDERATION, REGIONAL_FEDERATION, NATIONAL_FEDERATION
- * @param array $organizations
- * @return array
- * @see TreeTest::testSortByType()
- */
- public function sortByType(array $organizations): array {
- $typeOrder = [
- PrincipalTypeEnum::DELEGATION(),
- PrincipalTypeEnum::GROUPMENT(),
- PrincipalTypeEnum::LOCAL_FEDERATION(),
- PrincipalTypeEnum::DEPARTEMENTAL_FEDERATION(),
- PrincipalTypeEnum::REGIONAL_FEDERATION(),
- PrincipalTypeEnum::NATIONAL_FEDERATION()
- ];
- usort($organizations, function(Organization $organization1, Organization $organization2) use($typeOrder){
- $orderOrganization1 = array_keys($typeOrder, $organization1->getPrincipalType());
- $orderOrganization2 = array_keys($typeOrder, $organization2->getPrincipalType());
- if ($orderOrganization1 == $orderOrganization2) {
- return 0;
- }
- return ($orderOrganization1 < $orderOrganization2) ? -1 : 1;
- });
- return $organizations;
- }
- }
|