class-report-data.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Report Data Model Class - Handles data structure and validation
  4. *
  5. * @package StudiouWCCustomReports
  6. */
  7. if (!defined('ABSPATH')) {
  8. exit;
  9. }
  10. class StudiouWC_Report_Data {
  11. private $view;
  12. private $date_from;
  13. private $date_to;
  14. private $statuses;
  15. private $per_page;
  16. private $paged;
  17. private $orderby;
  18. private $order;
  19. public function __construct($params = array()) {
  20. $this->view = $params['view'] ?? 'product_categories_summary';
  21. $this->date_from = $params['date_from'] ?? '';
  22. $this->date_to = $params['date_to'] ?? date('Y-m-d');
  23. $this->statuses = $params['statuses'] ?? array();
  24. $this->per_page = intval($params['per_page'] ?? 20);
  25. $this->paged = intval($params['paged'] ?? 1);
  26. $this->orderby = $params['orderby'] ?? 'category';
  27. $this->order = $params['order'] ?? 'ASC';
  28. $this->validate_params();
  29. }
  30. private function validate_params() {
  31. // Validate date format
  32. if (!empty($this->date_from) && !$this->is_valid_date($this->date_from)) {
  33. $this->date_from = date('Y-m-d', strtotime('-1 month'));
  34. }
  35. if (!empty($this->date_to) && !$this->is_valid_date($this->date_to)) {
  36. $this->date_to = date('Y-m-d');
  37. }
  38. // Validate order direction
  39. if (!in_array(strtoupper($this->order), array('ASC', 'DESC'))) {
  40. $this->order = 'ASC';
  41. }
  42. // Validate orderby column
  43. $valid_orderby = array('category', 'orders_count', 'total_price');
  44. if (!in_array($this->orderby, $valid_orderby)) {
  45. $this->orderby = 'category';
  46. }
  47. // Validate per_page limits
  48. if ($this->per_page < 1 || $this->per_page > 1000) {
  49. $this->per_page = 20;
  50. }
  51. // Validate paged
  52. if ($this->paged < 1) {
  53. $this->paged = 1;
  54. }
  55. }
  56. private function is_valid_date($date) {
  57. $d = DateTime::createFromFormat('Y-m-d', $date);
  58. return $d && $d->format('Y-m-d') === $date;
  59. }
  60. // Getters
  61. public function get_view() {
  62. return $this->view;
  63. }
  64. public function get_date_from() {
  65. return $this->date_from;
  66. }
  67. public function get_date_to() {
  68. return $this->date_to;
  69. }
  70. public function get_statuses() {
  71. return $this->statuses;
  72. }
  73. public function get_per_page() {
  74. return $this->per_page;
  75. }
  76. public function get_paged() {
  77. return $this->paged;
  78. }
  79. public function get_orderby() {
  80. return $this->orderby;
  81. }
  82. public function get_order() {
  83. return $this->order;
  84. }
  85. public function to_array() {
  86. return array(
  87. 'view' => $this->view,
  88. 'date_from' => $this->date_from,
  89. 'date_to' => $this->date_to,
  90. 'statuses' => $this->statuses,
  91. 'per_page' => $this->per_page,
  92. 'paged' => $this->paged,
  93. 'orderby' => $this->orderby,
  94. 'order' => $this->order
  95. );
  96. }
  97. }