|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2017 RhubarbPHP |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +namespace Rhubarb\Stem\Repositories\MySql\Filters; |
| 18 | + |
| 19 | +use Rhubarb\Stem\Collections\Collection; |
| 20 | +use Rhubarb\Stem\Filters\Filter; |
| 21 | +use Rhubarb\Stem\Filters\ListContains; |
| 22 | +use Rhubarb\Stem\Repositories\Repository; |
| 23 | +use Rhubarb\Stem\Sql\WhereExpressionCollector; |
| 24 | + |
| 25 | +class MySqlListContains extends ListContains |
| 26 | +{ |
| 27 | + use MySqlFilterTrait; |
| 28 | + |
| 29 | + public static function fromGenericFilter(Filter $filter) |
| 30 | + { |
| 31 | + /** |
| 32 | + * @var ListContains $filter |
| 33 | + */ |
| 34 | + return new static($filter->columnName, $filter->contains); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns the SQL fragment needed to filter where a column equals a given value. |
| 39 | + * |
| 40 | + * @param Collection $collection |
| 41 | + * @param Repository $repository |
| 42 | + * @param WhereExpressionCollector $whereExpressionCollector |
| 43 | + * @param array $params |
| 44 | + * @return bool |
| 45 | + */ |
| 46 | + protected function doFilterWithRepository( |
| 47 | + Collection $collection, |
| 48 | + Repository $repository, |
| 49 | + WhereExpressionCollector $whereExpressionCollector, |
| 50 | + &$params |
| 51 | + ) { |
| 52 | + $filtered = true; |
| 53 | + |
| 54 | + foreach ($this->contains as $contain) { |
| 55 | + $currentFiltered = $this->createColumnWhereClauseExpression( |
| 56 | + "LIKE", |
| 57 | + [$contain], |
| 58 | + $collection, |
| 59 | + $repository, |
| 60 | + $whereExpressionCollector, |
| 61 | + $params); |
| 62 | + |
| 63 | + $keys = array_keys($params); |
| 64 | + |
| 65 | + $end = end($keys); |
| 66 | + |
| 67 | + $params[$end] = "%{$params[$end]}%"; |
| 68 | + |
| 69 | + if ($filtered && !$currentFiltered) { |
| 70 | + $filtered = false; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + return $filtered; |
| 76 | + } |
| 77 | +} |
0 commit comments