├── .gitignore ├── fm-demo.png ├── fm-demo2.png ├── src └── FilterManager │ ├── FilterManagerException.php │ ├── Facades │ └── FilterManager.php │ ├── FilterManagerServiceProvider.php │ └── FilterManager.php ├── .styleci.yml ├── composer.json ├── LICENSE ├── README_CN.md ├── README.md └── demo.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | vendor/ 3 | -------------------------------------------------------------------------------- /fm-demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toplan/filter-manager/HEAD/fm-demo.png -------------------------------------------------------------------------------- /fm-demo2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toplan/filter-manager/HEAD/fm-demo2.png -------------------------------------------------------------------------------- /src/FilterManager/FilterManagerException.php: -------------------------------------------------------------------------------- 1 | =5.4.0" 14 | }, 15 | "autoload": { 16 | "classmap": [ 17 | "src/" 18 | ], 19 | "psr-0": { 20 | "Toplan\\FilterManager": "src/" 21 | } 22 | }, 23 | "minimum-stability": "stable" 24 | } 25 | -------------------------------------------------------------------------------- /src/FilterManager/FilterManagerServiceProvider.php: -------------------------------------------------------------------------------- 1 | app['FilterManager'] = $this->app->share(function () { 25 | return FilterManager::create(Input::all())->setBlackList(['page']); 26 | }); 27 | } 28 | 29 | /** 30 | * Get the services provided by the provider. 31 | * 32 | * @return array 33 | */ 34 | public function provides() 35 | { 36 | return array('FilterManager'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 lan tian peng 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README_CN.md: -------------------------------------------------------------------------------- 1 | # 简介 2 | 3 | 该包主要用于资源列表页面筛选器,可以让我们优雅灵活的生成筛选链接。 4 | 5 | ![demo image](fm-demo.png) 6 | 7 | ![demo image](fm-demo2.png) 8 | 9 | # 安装 10 | 11 | ```php 12 | composer require 'toplan/filter-manager:~1.0.4' 13 | ``` 14 | 15 | # 打开姿势 16 | 17 | ### 1. 准备工作 18 | 19 | ```php 20 | //假设这是在你的控制器中 21 | use Toplan\FilterManager\FilterManager as FilterManager; 22 | 23 | //获取参数 24 | $paramsArray = [ 25 | 'paramName' => 'value', 26 | ... 27 | ] 28 | 29 | //生成管理器 30 | $fm = FilterManager::create($paramsArray)->setBlackList(['page']); 31 | 32 | //然后将变量`$fm`渲染到你的模板! 33 | ``` 34 | 35 | **或者直接在laravel中这样使用:** 36 | 37 | 在config/app.php文件中找到名为`providers`的key,然后为FilterManager的服务提供器。 38 | ```php 39 | 'providers' => array( 40 | Toplan\FilterManager\FilterManagerServiceProvider::class, 41 | ) 42 | ``` 43 | 44 | 在 config/app.php文件中找到名为`aliases`的key, 然后为FilterManger添加别名。 45 | ```php 46 | 'aliases' => array( 47 | 'FilterManager' => Toplan\FilterManager\Facades\FilterManager::class, 48 | ) 49 | ``` 50 | 51 | ### 2. Just enjoy it! 52 | 53 | 在模板中使用`$fm`: 54 | ```html 55 | 56 |
  • 57 | All 58 |
  • 59 |
  • 60 | Male 61 |
  • 62 |
  • 63 | Female 64 |
  • 65 | ``` 66 | 67 | 在模板中使laravel facade `FilterManger`: 68 | ```html 69 | 70 |
  • 71 | All 72 |
  • 73 |
  • 74 | Male 75 |
  • 76 |
  • 77 | Female 78 |
  • 79 | ``` 80 | 81 | 更多的详细用法参见: demo.md 82 | 83 | # API 84 | 85 | 基本上所有常用用法都在该文件中: demo_temp_for_laravel.blade.php 86 | 87 | ### create($filters, $baseUrl, $blackList) 88 | 89 | 获得FilterManager对象。 90 | 91 | - `$filters`: 参数数组,例:['gender'=>'male', 'city'=>'beijing'] 92 | - `$baseUrl`: 可以根据自己情况进行设置,默认为空,例:'www.example.com' 93 | - `$blackList`: 筛选条件/参数黑名单,例:['pageindex'] 94 | 95 | ### setBlackList(array $list) 96 | 97 | 设置筛选条件黑名单,可以在每次生成url的时候过滤掉你不想要的参数(比如分页参数等) 98 | 99 | 示例: 100 | ```php 101 | $fm->setBlackList(['page', 'pageindex']); 102 | 103 | //在laravel中 104 | FilterManager::setBlackList(['page', 'pageindex']); 105 | ``` 106 | 107 | ### has($filterName) 108 | 109 | 是否有指定筛选条件,如果有则返回值,如果没有则返回false。 110 | 111 | 示例: 112 | ```php 113 | $value = $fm->has('gender'); 114 | 115 | //或在laravel中: 116 | $value = FilterManager::has('gender'); 117 | ``` 118 | 119 | ### isActive($filterName, $filterValue, $trueReturn, $falseReturn) 120 | 121 | 指定的筛选条件是否包含指点值。 122 | 123 | 示例: 124 | ```php 125 | $fm->isActive('gender','male'); 126 | 127 | //或在laravel中 128 | FilterManager::isActive('gender', 'male');//将会返回true 或 false; 129 | 130 | FilterManager::isActive('gender', 'male', 'active', '');//将会返回 'active' 或 ''; 131 | ``` 132 | 133 | ### url($filterName, $filterValue, $multi, $linkageRemoveFilters, $blackList) 134 | 135 | 生成url。 136 | 参数介绍: 137 | - `$filterName`: 筛选条件/参数。 138 | - `$filterValue`: 筛选条件/参数的值,默认值为:`FM_SELECT_ALL`,表示为所有。 139 | - `$multi`: 是否支持多个参数值?true 为支持,默认为false。 140 | - `$linkageRemoveFilters`: 需要联动删除的筛选条件/参数。 141 | - `$blackList`: 临时黑名单,可以临时覆盖默认的黑名单。 142 | 143 | 示例: 144 | ```php 145 | FilterManager::url('gender', FM_SELECT_ALL);//将会删除gender参数 146 | 147 | FilterManager::url('gender', 'male', false);//gender只能有一个值 148 | 149 | FilterManager::url('cities', '成都', true);# 150 | FilterManager::url('cities', '绵阳', true);#支持cities有多个值 151 | 152 | //假设我们要对城镇区域进行筛选,首先我们知道一个省有多个城市,城市又有多个区、镇... 153 | //如果我们要选择‘全部’,或者选择任一一个其他省, 154 | //那么我们还需要通过第四个参数设置联动取消‘市’,‘区’,‘镇’等等你想取消的筛选条件。 155 | // 156 | //选择全部 157 | FilterManager::url('province', FM_SELECT_ALL, false, ['cities', 'counties', ...]);//联动删除cities等条件 158 | //选择一个省 159 | FilterManager::url('province', '四川', false, ['cities', 'counties', ...]);//联动删除cities等条件 160 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Intro 2 | 3 | A filter manager package for the product list filter, help you elegant generate filter url. 4 | 5 | **[中文文档](https://github.com/toplan/FilterManager/blob/master/README_CN.md)** 6 | 7 | ![demo image](fm-demo2.png) 8 | 9 | # Install 10 | 11 | ```php 12 | composer require 'toplan/filter-manager:~1.0.4' 13 | ``` 14 | 15 | # Usage 16 | 17 | ### 1. The preparatory work 18 | 19 | ```php 20 | require 'path/to/vendor/autoload.php'; 21 | use Toplan\FilterManager\FilterManager as FilterManager; 22 | 23 | // params 24 | $paramsArray = [ 25 | 'paramName' => 'value', 26 | ... 27 | ] 28 | 29 | // create instance by yourself. 30 | $fm = FilterManager::create($paramsArray)->setBlackList(['page']); 31 | 32 | //then, render `$fm` value to your template! 33 | ``` 34 | 35 | **Or used in laravel just like this:** 36 | 37 | Find the providers key in config/app.php and register the FilterManger Service Provider. 38 | ```php 39 | 'providers' => array( 40 | Toplan\FilterManager\FilterManagerServiceProvider::class, 41 | ) 42 | ``` 43 | Find the aliases key in config/app.php. 44 | ```php 45 | 'aliases' => array( 46 | 'FilterManager' => Toplan\FilterManager\Facades\FilterManager::class, 47 | ) 48 | ``` 49 | 50 | ### 2. Just enjoy it 51 | 52 | use value `$fm` in template: 53 | ```html 54 | 55 |
  • 56 | All 57 |
  • 58 |
  • 59 | Male 60 |
  • 61 |
  • 62 | Female 63 |
  • 64 | ``` 65 | 66 | or use laravel facade value `FilterManager` in template: 67 | ```html 68 | 69 |
  • 70 | All 71 |
  • 72 |
  • 73 | Male 74 |
  • 75 |
  • 76 | Female 77 |
  • 78 | ``` 79 | 80 | # API 81 | 82 | ### create(array $filters, $baseUrl, $blackList) 83 | 84 | create a instance. 85 | 86 | - `$filters`: this is filters data, required, exp:['gender'=>'male', 'city'=>'beijing'] 87 | 88 | - `$baseUrl`: default value is empty string. 89 | 90 | - `$blackList`: this is blacklist for filters, default value is `[]`, exp:['pageindex']. 91 | 92 | ### setBlackList(array $list) 93 | 94 | set black list for filter. 95 | 96 | example: 97 | ```php 98 | $fm->setBlackList(['page', 'pageindex']); 99 | //or in laravel 100 | FilterManager::setBlackList(['page', 'pageindex']); 101 | ``` 102 | 103 | ### has($filterName) 104 | 105 | whether has the character filter, if true will return the value, if don`t return false. 106 | 107 | example: 108 | ```php 109 | $value = $fm->has('gender'); 110 | 111 | //or in laravel 112 | $value = FilterManager::has('gender'); 113 | ``` 114 | 115 | ### isActive($filterName, $filterValue, $trueReturn, $falseReturn) 116 | 117 | example: 118 | ```php 119 | //in laravel 120 | FilterManager::isActive('gender', 'male');//this will return true or false; 121 | 122 | FilterManager::isActive('gender', 'male', 'active', '');//this will return 'active' or ''; 123 | ``` 124 | 125 | ### url($filterName, $filterValue, $multi, $linkageRemoveFilters, $blackList) 126 | 127 | One filter has some values, and every value has a url, this method return a full url string. 128 | 129 | - `$filterName`: param name, required. 130 | 131 | - `$filterValue`: param value, default value is `FM_SELECT_ALL`. 132 | 133 | - `$multi`: whether to support multiple values? `false` or `true`, default value is `false`. 134 | 135 | - `$linkageRemoveFilters`:linkage remove the other filter, default value is `[]`. 136 | 137 | - `$blackList`: temporary blacklist, default value is ``[]``. 138 | 139 | example: 140 | ```php 141 | //in laravel 142 | FilterManager::url('gender', FM_SELECT_ALL);//without gender param 143 | 144 | FilterManager::url('gender', 'male', false);//single value 145 | 146 | FilterManager::url('cities', 'shanghai', true); 147 | FilterManager::url('cities', 'beijing', true);//multiple values 148 | 149 | // One province has many cities, one city has many counties ..., 150 | // If you select 'all province' or one of provinces, 151 | // you should linkage remove the selected cities and counties ... 152 | // 153 | // like this: 154 | // select all province 155 | FilterManager::url('province', FM_SELECT_ALL, false, ['cities', 'counties', ...]);//linkage remove selected cities 156 | // select one province 157 | FilterManager::url('province', 'sichuan', false, ['cities', 'counties', ...]);//linkage remove selected cities 158 | ``` 159 | -------------------------------------------------------------------------------- /src/FilterManager/FilterManager.php: -------------------------------------------------------------------------------- 1 | 'male','city'=>'beijing',...] 15 | * 16 | * @var array 17 | */ 18 | protected $filters = []; 19 | 20 | /** 21 | * blacklist for filter 22 | * 23 | * @var array 24 | */ 25 | protected $blackList = []; 26 | 27 | /** 28 | * base url(without params) 29 | * exp:'www.xxx.com/goods' 30 | * 31 | * @var string 32 | */ 33 | protected $baseUrl = ''; 34 | 35 | /** 36 | * @param array $filters 37 | * @param $baseUrl 38 | * @param array $blackList 39 | */ 40 | public function __construct(array $filters, $baseUrl = '', array $blackList = []) 41 | { 42 | if (!defined('FM_SELECT_ALL')) { 43 | define('FM_SELECT_ALL', self::ALL); 44 | } 45 | foreach ($filters as $name => $value) { 46 | $this->addFilter($name, $value); 47 | } 48 | $this->baseUrl = explode('?', $baseUrl)[0]; 49 | $this->blackList = $blackList; 50 | } 51 | 52 | /**create a instance of FilterManger 53 | * @param array $filters 54 | * @param string $baseUrl 55 | * @param array $blackList 56 | * 57 | * @return FilterManager 58 | */ 59 | 60 | public static function create(array $filters, $baseUrl = '', array $blackList = []) 61 | { 62 | return new self($filters, $baseUrl, $blackList); 63 | } 64 | 65 | /**set base url 66 | * @param $baseUrl 67 | * 68 | * @return $this 69 | */ 70 | 71 | public function setBaseUrl($baseUrl) 72 | { 73 | $this->baseUrl->$baseUrl; 74 | 75 | return $this; 76 | } 77 | 78 | /**set black list for filters 79 | * @param array $blackList 80 | * 81 | * @return $this 82 | */ 83 | 84 | public function setBlackList(array $blackList) 85 | { 86 | $this->blackList = $blackList; 87 | 88 | return $this; 89 | } 90 | 91 | /** 92 | * add filter 93 | * 94 | * @param string $name 95 | * @param string $value 96 | * 97 | * @throws FilterManagerException 98 | * 99 | * @return $this 100 | */ 101 | public function addFilter($name, $value) 102 | { 103 | $name = "$name"; 104 | if (empty($name)) { 105 | throw new FilterManagerException('Filter name can`t be empty'); 106 | } 107 | $this->filters[$name] = "$value"; 108 | 109 | return $this; 110 | } 111 | 112 | /**remove filter 113 | * @param $name 114 | * 115 | * @return $this 116 | */ 117 | 118 | public function removeFilter($name) 119 | { 120 | if ($name && isset($this->filters["$name"])) { 121 | unset($this->filters["$name"]); 122 | } 123 | 124 | return $this; 125 | } 126 | 127 | /** 128 | * whether has character filter 129 | * 130 | * @param $name 131 | * 132 | * @return mixed 133 | */ 134 | public function has($name) 135 | { 136 | if (isset($this->filters["$name"])) { 137 | return $this->filters["$name"]; 138 | } 139 | 140 | return false; 141 | } 142 | 143 | /** 144 | * is active 145 | * 146 | * @param string $name 147 | * filter name 148 | * @param string $value 149 | * filter value 150 | * @param mixed $trueReturn 151 | * @param mixed $falseReturn 152 | * 153 | * @return bool 154 | */ 155 | public function isActive($name = '', $value = self::ALL, $trueReturn = true, $falseReturn = false) 156 | { 157 | $currentFilters = $this->filters; 158 | if (!$name) { 159 | return $falseReturn; 160 | } 161 | if (!$currentFilters || !isset($currentFilters["$name"])) { 162 | if ($value === self::ALL) { 163 | return $trueReturn; 164 | } else { 165 | return $falseReturn; 166 | } 167 | } 168 | $valueArray = explode(',', $currentFilters["$name"]); 169 | if (in_array($value, $valueArray)) { 170 | return $trueReturn; 171 | } else { 172 | return $falseReturn; 173 | } 174 | } 175 | 176 | /**get full url(with params) 177 | * 178 | * @param string $name 179 | * filter name 180 | * @param string $value 181 | * filter value 182 | * @param bool $multi 183 | * Whether to support more value filtering, 184 | * if $value == FilterManager::ALL, this parameter does`t work 185 | * @param array $linkageRemoveFilters 186 | * Linkage to filter the filter 187 | * @param array $blackList 188 | * 189 | * @return string 190 | */ 191 | 192 | public function url($name = '', $value = self::ALL, $multi = false, array $linkageRemoveFilters = [], array $blackList = []) 193 | { 194 | $filters = []; 195 | $currentFilters = $this->filters; 196 | 197 | if (!$name) { 198 | return $this->baseUrl; 199 | } 200 | 201 | if (!$currentFilters || !count($currentFilters)) { 202 | return $value !== self::ALL ? "$this->baseUrl?$name=$value" : $this->baseUrl; 203 | } 204 | 205 | if (!isset($currentFilters["$name"]) && $value !== self::ALL) { 206 | if ($this->isPass($name, $linkageRemoveFilters, $blackList)) { 207 | $filters["$name"] = $value; 208 | } 209 | } 210 | 211 | foreach ($currentFilters as $filterName => $filterValue) { 212 | if ($this->isPass($filterName, $linkageRemoveFilters, $blackList)) { 213 | if ($name === "$filterName") { 214 | if ($value !== self::ALL) { 215 | if ($multi) { 216 | $valueArray = explode(',', $filterValue); 217 | if (in_array($value, $valueArray)) { 218 | $newValueArray = array_diff($valueArray, [$value]); 219 | $filters["$filterName"] = implode(',', $newValueArray); 220 | } else { 221 | array_push($valueArray, $value); 222 | $filters["$filterName"] = implode(',', $valueArray); 223 | } 224 | } else { 225 | $filters["$filterName"] = $value; 226 | } 227 | } 228 | } else { 229 | $filters["$filterName"] = $filterValue; 230 | } 231 | } 232 | } 233 | $params = []; 234 | foreach ($filters as $key => $filter) { 235 | if ($filter = urlencode(trim($filter))) { 236 | $params[] = "$key=$filter"; 237 | } 238 | } 239 | 240 | return "$this->baseUrl?" . implode('&', $params); 241 | } 242 | 243 | /**filter filters 244 | * @param $filterName 245 | * @param array $linkageRemoveFilters 246 | * @param array $blackList 247 | * 248 | * @return bool 249 | */ 250 | 251 | protected function isPass($filterName, array $linkageRemoveFilters = [], array $blackList = []) 252 | { 253 | if (count($linkageRemoveFilters) > 0) { 254 | if (in_array($filterName, $linkageRemoveFilters)) { 255 | return false; 256 | } 257 | } 258 | if (count($blackList) === 0) { 259 | $blackList = $this->blackList; 260 | } 261 | if (in_array($filterName, $blackList)) { 262 | return false; 263 | } 264 | 265 | return true; 266 | } 267 | } 268 | -------------------------------------------------------------------------------- /demo.md: -------------------------------------------------------------------------------- 1 | ```php 2 | 3 | 4 | FilterManger 5 | 6 | 31 | 32 | 33 |
    34 |
    35 |
    36 |
      37 |
    • 所有分类 >
    • 38 | @if(FilterManager::has('gender') !== false) 39 |
    • 性别:{{FilterManager::has('gender')}}  40 | × 41 |
    • 42 | @endif 43 | @if(FilterManager::has('types') !== false) 44 |
    • 方式:{{FilterManager::has('types')}}  45 | × 46 |
    • 47 | @endif 48 | @if(FilterManager::has('city') !== false) 49 |
    • 城市:{{FilterManager::has('city')}}  50 | × 51 |
    • 52 | @endif 53 |
    54 |
    55 |
    56 |
      57 |
    • 教师性别:
    • 58 |
    • 59 | 全部 60 |
    • 61 |
    • 62 | 63 |
    • 64 |
    • 65 | 66 |
    • 67 |
    68 |
    69 |
    70 | 98 |
    99 |
    100 | 134 |
    135 |
    136 | 147 |
    148 |
    149 |
    150 | 151 | 152 | 157 | 158 | ``` 159 | --------------------------------------------------------------------------------