Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Refactor enums #1214

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"doctrine/annotations": "^1.0",
"doctrine/doctrine-bundle": "^2.3",
"doctrine/doctrine-migrations-bundle": "^3.1",
"doctrine/orm": "^2.8",
"doctrine/orm": "^2.11",
"doctrine/persistence": "^2.0",
"easyrdf/easyrdf": "^0.9.1",
"endroid/qr-code": "^4.1",
Expand Down
1 change: 0 additions & 1 deletion config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ doctrine:
enumGroepStatus: CsrDelft\common\Doctrine\Type\Enum\GroepStatusType
enumHuisStatus: CsrDelft\common\Doctrine\Type\Enum\HuisStatusType
enumCommissieSoort: CsrDelft\common\Doctrine\Type\Enum\CommissieSoortType
enumGeslacht: CsrDelft\common\Doctrine\Type\Enum\GeslachtType
enumGroepVersie: CsrDelft\common\Doctrine\Type\Enum\GroepVersieType
enumOnderverenigingStatus: CsrDelft\common\Doctrine\Type\Enum\OnderverenigingStatusType
enumActiviteitSoort: CsrDelft\common\Doctrine\Type\Enum\ActiviteitSoortType
Expand Down
4 changes: 2 additions & 2 deletions lib/DataFixtures/Util/ProfielFixtureUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public static function maakProfiel(
$profiel->uid = $uid;
$profiel->lidjaar = (int) ('20' . substr((string) $uid, 2));
$profiel->geslacht = [
'male' => Geslacht::Man(),
'female' => Geslacht::Vrouw(),
'male' => Geslacht::Man,
'female' => Geslacht::Vrouw,
][$geslacht];
// TODO $profiel->changelog;
$profiel->changelog = [new ProfielLogTextEntry('Aangemaakt door fixtures')];
Expand Down
18 changes: 0 additions & 18 deletions lib/common/Doctrine/Type/Enum/GeslachtType.php

This file was deleted.

20 changes: 20 additions & 0 deletions lib/common/EnumTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace CsrDelft\common;


trait EnumTrait {
/**
* @return string[]
*/
abstract public static function cases(): array;
abstract public function getValue(): string;
abstract public function getDescription(): string;
public static function getEnumValues(): array {
return array_column(self::cases(), 'value');
}
public static function isValidValue(string $value): bool {
return in_array($value, self::getEnumValues(), strict: true);
}

}
46 changes: 23 additions & 23 deletions lib/entity/Geslacht.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

namespace CsrDelft\entity;

use CsrDelft\common\Enum;
use CsrDelft\common\EnumTrait;

/**
* @author P.W.G. Brussee <[email protected]>
*
* @method static Geslacht Man()
* @method static Geslacht Vrouw()
* @method static boolean isMan($geslacht)
* @method static boolean isVrouw($geslacht)
*/
class Geslacht extends Enum
{
/**
* Geslacht opties.
*/
const Man = 'm';
const Vrouw = 'v';
enum Geslacht: string {
use EnumTrait;
case Man = 'm';
case Vrouw = 'v';

/**
* @var string[]
*/
protected static $mapChoiceToDescription = [
self::Man => 'man',
self::Vrouw => 'vrouw',
];
public function getDescription(): string {
return match($this) {
Geslacht::Man => 'man',
Geslacht::Vrouw => 'vrouw'
};
}

public function getValue(): string {
return $this->value;
}

public function isVrouw(): bool {
return $this === self::Vrouw;
}

public function isMan(): bool {
return $this === self::Man;
}
}
46 changes: 20 additions & 26 deletions lib/entity/profiel/Profiel.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,9 @@ public function __construct()
*/
#[ORM\Column(type: 'string', nullable: true)]
public $duckname;
// fysiek
/**
* @var Geslacht
*/
#[ORM\Column(type: 'enumGeslacht')]
public $geslacht;

#[ORM\Column(type: 'string', enumType: Geslacht::class)]
public Geslacht $geslacht;
/**
* @var DateTimeImmutable
*/
Expand Down Expand Up @@ -250,11 +247,8 @@ public function __construct()
*/
#[ORM\Column(type: 'date', nullable: true)]
public $lidafdatum;
/**
* @var string
*/
#[ORM\Column(type: 'string')]
public $status;
#[ORM\Column(type: 'string', enumType: LidStatus::class)]
public LidStatus $status;
// geld
/**
* @var string|null
Expand Down Expand Up @@ -728,7 +722,7 @@ public function getLink($vorm = 'civitas')
'"' .
$title .
' class="lidLink ' .
htmlspecialchars($this->status) .
htmlspecialchars($this->status->value) .
'" data-lid="' .
$this->uid .
'" data-lid-naam="' .
Expand All @@ -747,7 +741,7 @@ public function getLink($vorm = 'civitas')
'"><a href="/profiel/' .
$this->uid .
'" class="lidLink ' .
htmlspecialchars($this->status) .
htmlspecialchars($this->status->value) .
'">' .
$naam .
'</a></span>';
Expand Down Expand Up @@ -839,9 +833,9 @@ public function getNaam($vorm = 'volledig', $force = false)
} elseif ($this->isLid() || $this->isOudlid()) {
// voor novieten is het Dhr./ Mevr.
if (LoginService::getProfiel()->status === LidStatus::Noviet) {
$naam = Geslacht::isVrouw($this->geslacht) ? 'Mevr. ' : 'Dhr. ';
$naam = $this->geslacht === Geslacht::Vrouw ? 'Mevr. ' : 'Dhr. ';
} else {
$naam = Geslacht::isVrouw($this->geslacht) ? 'Ama. ' : 'Am. ';
$naam = $this->geslacht === Geslacht::Vrouw ? 'Ama. ' : 'Am. ';
}
if (!empty($this->tussenvoegsel)) {
$naam .= ucfirst($this->tussenvoegsel) . ' ';
Expand All @@ -852,7 +846,7 @@ public function getNaam($vorm = 'volledig', $force = false)
}
// status char weergeven bij oudleden en ereleden
if ($this->isOudlid()) {
$naam .= ' ' . LidStatus::from($this->status)->getChar();
$naam .= ' ' . $this->status->getChar();
}
}
// geen lid
Expand All @@ -868,7 +862,7 @@ public function getNaam($vorm = 'volledig', $force = false)
$naam .= $this->achternaam;
// status char weergeven bij kringels
if ($this->status === LidStatus::Kringel) {
$naam .= ' ' . LidStatus::from($this->status)->getChar();
$naam .= ' ' . $this->status->getChar();
}
}

Expand Down Expand Up @@ -1034,12 +1028,12 @@ public function getNageslachtGrootte()

public function isLid()
{
return LidStatus::isLidLike($this->status);
return $this->status->isLidLike();
}

public function isOudlid()
{
return LidStatus::isOudlidLike($this->status);
return $this->status->isOudlidLike();
}

/**
Expand Down Expand Up @@ -1106,7 +1100,7 @@ public function propertyMogelijk(string $name)
return in_array($this->status, Profiel::$properties_lidstatus[$name]);
}

public function getDataTableColumn()
public function getDataTableColumn(): DataTableColumn
{
return new DataTableColumn(
$this->getLink('volledig'),
Expand All @@ -1115,7 +1109,7 @@ public function getDataTableColumn()
);
}

public function getId()
public function getId(): string
{
return $this->uid;
}
Expand All @@ -1125,17 +1119,17 @@ public function getWeergave(): string
return $this->achternaam ? $this->getNaam('volledig') : '';
}

public function getChar()
public function getChar(): string
{
return LidStatus::from($this->status)->getChar();
return $this->status->getChar();
}

public function getLidStatusDescription()
public function getLidStatusDescription(): string
{
return LidStatus::from($this->status)->getDescription();
return $this->status->getDescription();
}

public function getLeeftijd()
public function getLeeftijd(): int
{
return $this->gebdatum->diff(date_create_immutable())->y;
}
Expand Down
27 changes: 0 additions & 27 deletions lib/model/entity/Kringleider.php

This file was deleted.

Loading
Loading