Skip to content

Commit

Permalink
Retrieve adherent last membership year from tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Remg committed Oct 30, 2024
1 parent dc781e4 commit 3653daa
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/Entity/Adherent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2277,12 +2277,14 @@ public function getMissingMembershipYears(): array
return [];
}

if (!$this->lastMembershipDonation) {
$lastYear = $this->getLastAdherentYearTag();

if (!$lastYear) {
return [date('Y')];
}

return range(
((int) $this->lastMembershipDonation->format('Y')) + 1,
((int) $lastYear) + 1,
(int) date('Y')
);
}
Expand Down Expand Up @@ -2504,6 +2506,38 @@ public function hasTag(string $tag): bool
return TagEnum::includesTag($tag, $this->tags ?? []);
}

public function getLastAdherentYearTag(): ?string
{
$allTags = array_map(
fn (int $year) => TagEnum::getAdherentYearTag($year),
array_reverse(range(2022, date('Y')))
);

foreach ($this->tags as $tag) {
if (\in_array($tag, $allTags, true)) {
return $tag;
}
}

return null;
}

public function getLastMembershipYearFromTags(): ?string
{
$lastAdherentYearTag = $this->getLastAdherentYearTag();

if (!$lastAdherentYearTag) {
return null;
}

$matches = [];
if (preg_match('/^adherent:a_jour_([\d]{4})$/', $lastAdherentYearTag, $matches)) {
return $matches[1];
}

return null;
}

public function updateFromMembershipRequest(MembershipRequest $membershipRequest): void
{
if (!$this->isCertified()) {
Expand Down

0 comments on commit 3653daa

Please sign in to comment.