Tag.php 1.9 KB

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