1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Icybee package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Icybee\Modules\Files\Facets; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\ActiveRecord\Query; |
15
|
|
|
use ICanBoogie\Facets\Criterion\BasicCriterion; |
16
|
|
|
|
17
|
|
|
class SizeCriterion extends BasicCriterion |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Adds support for the `size` filter. |
21
|
|
|
* |
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
24
|
|
|
public function alter_conditions(array &$filters, array $modifiers) |
25
|
|
|
{ |
26
|
|
|
parent::alter_conditions($filters, $modifiers); |
27
|
|
|
|
28
|
|
|
if (isset($modifiers['size'])) |
29
|
|
|
{ |
30
|
|
|
$value = $modifiers['size']; |
31
|
|
|
|
32
|
|
|
if (in_array($value, [ 'l', 'm', 's' ])) |
33
|
|
|
{ |
34
|
|
|
$filters['size'] = $value; |
35
|
|
|
} |
36
|
|
|
else |
37
|
|
|
{ |
38
|
|
|
unset($filters['size']); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Adds support for the `size` filter. |
45
|
|
|
* |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function alter_query_with_value(Query $query, $value) |
49
|
|
|
{ |
50
|
|
|
if ($value) |
51
|
|
|
{ |
52
|
|
|
list($avg, $max, $min) = $query->model |
53
|
|
|
->similar_site |
54
|
|
|
->select('AVG(size), MAX(size), MIN(size)') |
55
|
|
|
->one(\PDO::FETCH_NUM); |
56
|
|
|
|
57
|
|
|
$bounds = [ |
58
|
|
|
|
59
|
|
|
$min, |
60
|
|
|
round($avg - ($avg - $min) / 3), |
61
|
|
|
round($avg), |
62
|
|
|
round($avg + ($max - $avg) / 3), |
63
|
|
|
$max |
64
|
|
|
|
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
switch ($value) |
68
|
|
|
{ |
69
|
|
|
case 'l': $query->and('size >= ?', $bounds[3]); break; |
|
|
|
|
70
|
|
|
case 'm': $query->and('size >= ? AND size < ?', $bounds[2], $bounds[3]); break; |
|
|
|
|
71
|
|
|
case 's': $query->and('size < ?', $bounds[2]); break; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $query; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.