Group.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Knuckles\Scribe\Attributes;
  3. use Attribute;
  4. #[Attribute(Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
  5. class Group
  6. {
  7. public function __construct(
  8. public mixed $name,
  9. public ?string $description = '',
  10. /** You can use the separate #[Authenticated] attribute, or pass authenticated: false to this. */
  11. public ?bool $authenticated = null,
  12. ){
  13. }
  14. protected function getName(): string
  15. {
  16. if (is_string($this->name)) {
  17. return $this->name;
  18. }
  19. if (interface_exists('BackedEnum') && is_a($this->name, 'BackedEnum')) {
  20. return $this->name->value;
  21. }
  22. throw new \InvalidArgumentException(
  23. 'The name property of a group must be either a PHP Backed Enum or a string'
  24. );
  25. }
  26. public function toArray()
  27. {
  28. $data = [
  29. "groupName" => $this->getName(),
  30. "groupDescription" => $this->description,
  31. ];
  32. if (!is_null($this->authenticated)) {
  33. $data["authenticated"] = $this->authenticated;
  34. }
  35. return $data;
  36. }
  37. }