Tag.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * @return mixed|string|array<string,mixed>|null
  43. */
  44. public function attributes(string $key = '') {
  45. if ($key == '') {
  46. return $this->attributes;
  47. } else {
  48. return $this->attributes[$key] ?? null;
  49. }
  50. }
  51. /**
  52. * @param mixed|string|array<string,mixed>|null $value
  53. */
  54. public function _attributes(string $key, $value = null): void {
  55. if ($key == '') {
  56. if (is_string($value)) {
  57. $value = json_decode($value, true);
  58. }
  59. if (is_array($value)) {
  60. $this->attributes = $value;
  61. }
  62. } elseif ($value === null) {
  63. unset($this->attributes[$key]);
  64. } else {
  65. $this->attributes[$key] = $value;
  66. }
  67. }
  68. public function nbEntries(): int {
  69. if ($this->nbEntries < 0) {
  70. $tagDAO = FreshRSS_Factory::createTagDao();
  71. $this->nbEntries = $tagDAO->countEntries($this->id()) ?: 0;
  72. }
  73. return $this->nbEntries;
  74. }
  75. /**
  76. * @param string|int $value
  77. */
  78. public function _nbEntries($value): void {
  79. $this->nbEntries = (int)$value;
  80. }
  81. public function nbUnread(): int {
  82. if ($this->nbUnread < 0) {
  83. $tagDAO = FreshRSS_Factory::createTagDao();
  84. $this->nbUnread = $tagDAO->countNotRead($this->id()) ?: 0;
  85. }
  86. return $this->nbUnread;
  87. }
  88. /**
  89. * @param string|int$value
  90. */
  91. public function _nbUnread($value): void {
  92. $this->nbUnread = (int)$value;
  93. }
  94. }