1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage model
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8 // Protect from unauthorized access
9 defined('FOF_INCLUDED') or die;
10
11 /**
12 * FrameworkOnFramework model behavior class
13 *
14 * @package FrameworkOnFramework
15 * @since 2.1
16 */
17 class FOFModelFieldNumber extends FOFModelField
18 {
19 /**
20 * The partial match is mapped to an exact match
21 *
22 * @param mixed $value The value to compare to
23 *
24 * @return string The SQL where clause for this search
25 */
26 public function partial($value)
27 {
28 return $this->exact($value);
29 }
30
31 /**
32 * Perform a between limits match. When $include is true
33 * the condition tested is:
34 * $from <= VALUE <= $to
35 * When $include is false the condition tested is:
36 * $from < VALUE < $to
37 *
38 * @param mixed $from The lowest value to compare to
39 * @param mixed $to The higherst value to compare to
40 * @param boolean $include Should we include the boundaries in the search?
41 *
42 * @return string The SQL where clause for this search
43 */
44 public function between($from, $to, $include = true)
45 {
46 if ($this->isEmpty($from) || $this->isEmpty($to))
47 {
48 return '';
49 }
50
51 $extra = '';
52
53 if ($include)
54 {
55 $extra = '=';
56 }
57
58 $sql = '((' . $this->getFieldName() . ' >' . $extra . ' ' . $from . ') AND ';
59 $sql .= '(' . $this->getFieldName() . ' <' . $extra . ' ' . $to . '))';
60
61 return $sql;
62 }
63
64 /**
65 * Perform an outside limits match. When $include is true
66 * the condition tested is:
67 * (VALUE <= $from) || (VALUE >= $to)
68 * When $include is false the condition tested is:
69 * (VALUE < $from) || (VALUE > $to)
70 *
71 * @param mixed $from The lowest value of the excluded range
72 * @param mixed $to The higherst value of the excluded range
73 * @param boolean $include Should we include the boundaries in the search?
74 *
75 * @return string The SQL where clause for this search
76 */
77 public function outside($from, $to, $include = false)
78 {
79 if ($this->isEmpty($from) || $this->isEmpty($to))
80 {
81 return '';
82 }
83
84 $extra = '';
85
86 if ($include)
87 {
88 $extra = '=';
89 }
90
91 $sql = '((' . $this->getFieldName() . ' <' . $extra . ' ' . $from . ') OR ';
92 $sql .= '(' . $this->getFieldName() . ' >' . $extra . ' ' . $to . '))';
93
94 return $sql;
95 }
96
97 /**
98 * Perform an interval match. It's similar to a 'between' match, but the
99 * from and to values are calculated based on $value and $interval:
100 * $value - $interval < VALUE < $value + $interval
101 *
102 * @param integer|float $value The center value of the search space
103 * @param integer|float $interval The width of the search space
104 * @param boolean $include Should I include the boundaries in the search?
105 *
106 * @return string The SQL where clause
107 */
108 public function interval($value, $interval, $include = true)
109 {
110 if ($this->isEmpty($value))
111 {
112 return '';
113 }
114
115 $from = $value - $interval;
116 $to = $value + $interval;
117
118 $extra = '';
119
120 if ($include)
121 {
122 $extra = '=';
123 }
124
125 $sql = '((' . $this->getFieldName() . ' >' . $extra . ' ' . $from . ') AND ';
126 $sql .= '(' . $this->getFieldName() . ' <' . $extra . ' ' . $to . '))';
127
128 return $sql;
129 }
130
131 /**
132 * Perform a range limits match. When $include is true
133 * the condition tested is:
134 * $from <= VALUE <= $to
135 * When $include is false the condition tested is:
136 * $from < VALUE < $to
137 *
138 * @param mixed $from The lowest value to compare to
139 * @param mixed $to The higherst value to compare to
140 * @param boolean $include Should we include the boundaries in the search?
141 *
142 * @return string The SQL where clause for this search
143 */
144 public function range($from, $to, $include = true)
145 {
146 if ($this->isEmpty($from) && $this->isEmpty($to))
147 {
148 return '';
149 }
150
151 $extra = '';
152
153 if ($include)
154 {
155 $extra = '=';
156 }
157
158 if ($from)
159 $sql[] = '(' . $this->getFieldName() . ' >' . $extra . ' ' . $from . ')';
160 if ($to)
161 $sql[] = '(' . $this->getFieldName() . ' <' . $extra . ' ' . $to . ')';
162
163 $sql = '(' . implode(' AND ', $sql) . ')';
164
165 return $sql;
166 }
167
168 /**
169 * Perform an interval match. It's similar to a 'between' match, but the
170 * from and to values are calculated based on $value and $interval:
171 * $value - $interval < VALUE < $value + $interval
172 *
173 * @param integer|float $value The starting value of the search space
174 * @param integer|float $interval The interval period of the search space
175 * @param boolean $include Should I include the boundaries in the search?
176 *
177 * @return string The SQL where clause
178 */
179 public function modulo($value, $interval, $include = true)
180 {
181 if ($this->isEmpty($value) || $this->isEmpty($interval))
182 {
183 return '';
184 }
185
186 $extra = '';
187
188 if ($include)
189 {
190 $extra = '=';
191 }
192
193 $sql = '(' . $this->getFieldName() . ' >' . $extra . ' ' . $value . ' AND ';
194 $sql .= '(' . $this->getFieldName() . ' - ' . $value . ') % ' . $interval . ' = 0)';
195
196 return $sql;
197 }
198 }
199