Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
filter.php
1<?php
9
12
13class Filter
14{
15 CONST PERIOD_WEEK = 7;
16 CONST PERIOD_MONTH = 30;
17 CONST PERIOD_MONTH_3 = 90;
18 CONST PERIOD_MONTH_6 = 180;
19 CONST PERIOD_MONTH_12 = 365;
20
21 protected $values = array(
22 'authorId' => null,
23 'chainId' => null,
24 'mailingId' => null,
25 'postingId' => null,
26 'periodFrom' => null,
27 'periodTo' => null,
28 'period' => null
29 );
30
31 public function __construct(array $values = array())
32 {
33 foreach ($values as $name => $value)
34 {
35 $this->set($name, $value);
36 }
37 }
38
39 public function set($name, $value = null)
40 {
41 if (!array_key_exists($name, $this->values))
42 {
43 throw new ArgumentException("Unknown filter \"$name\"");
44 }
45
46 if ($value === 'all')
47 {
48 $value = null;
49 }
50
51 if ($name == 'period')
52 {
53 $this->setPeriod($value);
54 }
55
56 $this->values[$name] = $value;
57 }
58
59 public function get($name)
60 {
61 if (!array_key_exists($name, $this->values))
62 {
63 throw new ArgumentException("Unknown filter \"$name\"");
64 }
65
66 return $this->values[$name];
67 }
68
69 public function getNames()
70 {
71 return array_keys($this->values);
72 }
73
74 public function getMappedArray(array $map, array $filter = array())
75 {
76 foreach ($map as $name => $mappedName)
77 {
78 if (!array_key_exists($name, $this->values))
79 {
80 throw new ArgumentException("Unknown filter \"$name\"");
81 }
82
83 if (!$this->values[$name])
84 {
85 if (isset($filter[$mappedName]))
86 {
87 unset($filter[$mappedName]);
88 }
89
90 continue;
91 }
92
93 $filter[$mappedName] = $this->values[$name];
94 }
95
96 return $filter;
97 }
98
99 public function clear()
100 {
101 foreach ($this->values as $name => $value)
102 {
103 $this->values[$name] = null;
104 }
105 }
106
107 protected function setPeriod($period = self::PERIOD_MONTH)
108 {
109 $date = new DateTime();
110 $date->add('-' . $period . ' DAY');
111 $this->set('periodFrom', $date);
112 $this->set('periodTo');
113 }
114}
115
__construct(array $values=array())
Definition filter.php:31
setPeriod($period=self::PERIOD_MONTH)
Definition filter.php:107
getMappedArray(array $map, array $filter=array())
Definition filter.php:74