Tag.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_Tag extends Minz_Model {
  4. private int $id = 0;
  5. private string $name;
  6. /**
  7. * @var array<string,mixed>
  8. */
  9. private array $attributes = [];
  10. private int $nbEntries = -1;
  11. private int $nbUnread = -1;
  12. public function __construct(string $name = '') {
  13. $this->_name($name);
  14. }
  15. public function id(): int {
  16. return $this->id;
  17. }
  18. /**
  19. * @param int|string $value
  20. */
  21. public function _id($value): void {
  22. $this->id = (int)$value;
  23. }
  24. public function name(): string {
  25. return $this->name;
  26. }
  27. public function _name(string $value): void {
  28. $this->name = trim($value);
  29. }
  30. /**
  31. * @phpstan-return ($key is non-empty-string ? mixed : array<string,mixed>)
  32. * @return array<string,mixed>|mixed|null
  33. */
  34. public function attributes(string $key = '') {
  35. if ($key === '') {
  36. return $this->attributes;
  37. } else {
  38. return $this->attributes[$key] ?? null;
  39. }
  40. }
  41. /** @param string|array<mixed>|bool|int|null $value Value, not HTML-encoded */
  42. public function _attributes(string $key, $value = null): void {
  43. if ($key == '') {
  44. if (is_string($value)) {
  45. $value = json_decode($value, true);
  46. }
  47. if (is_array($value)) {
  48. $this->attributes = $value;
  49. }
  50. } elseif ($value === null) {
  51. unset($this->attributes[$key]);
  52. } else {
  53. $this->attributes[$key] = $value;
  54. }
  55. }
  56. public function nbEntries(): int {
  57. if ($this->nbEntries < 0) {
  58. $tagDAO = FreshRSS_Factory::createTagDao();
  59. $this->nbEntries = $tagDAO->countEntries($this->id()) ?: 0;
  60. }
  61. return $this->nbEntries;
  62. }
  63. /**
  64. * @param string|int $value
  65. */
  66. public function _nbEntries($value): void {
  67. $this->nbEntries = (int)$value;
  68. }
  69. public function nbUnread(): int {
  70. if ($this->nbUnread < 0) {
  71. $tagDAO = FreshRSS_Factory::createTagDao();
  72. $this->nbUnread = $tagDAO->countNotRead($this->id()) ?: 0;
  73. }
  74. return $this->nbUnread;
  75. }
  76. /**
  77. * @param string|int $value
  78. */
  79. public function _nbUnread($value): void {
  80. $this->nbUnread = (int)$value;
  81. }
  82. }