Tag.php 2.0 KB

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