This commit is contained in:
2020-10-06 14:27:47 +07:00
commit 586be80cf6
16613 changed files with 3274099 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
;
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(array(
'@PSR2' => true,
'array_syntax' => array('syntax' => 'short'),
'combine_consecutive_unsets' => true,
// 'header_comment' => array('header' => $header),
'no_extra_consecutive_blank_lines' => array(
'break',
'continue',
'extra',
'return',
'throw',
'use',
'parenthesis_brace_block',
'square_brace_block',
'curly_brace_block'
),
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_class_elements' => true,
'ordered_imports' => true,
'phpdoc_add_missing_param_annotation' => true,
'psr4' => true,
'strict_comparison' => true,
))
->setFinder($finder)
;

View File

@@ -0,0 +1,16 @@
# Changelog
## 1.0.1 - 2015-03-04
## Changed
- Added CONTRIBUTING.md file
## 1.0.0 - 2015-03-04
## Changed
- Refactored extension to follow new 2amigOS! extension standards based on
[yii2-extension-skeleton](https://github.com/2amigos/yii2-extension-skeleton)
- Updated tests
## Fixed
- Fixed bug when checking "or" conditions

View File

@@ -0,0 +1,32 @@
# Contributing
Contributions are **welcome** and will be fully **credited**.
We accept contributions via Pull Requests on [Github](https://github.com/2amigos/yii2-arrayquery-component).
## Pull Requests
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
- **Create feature branches** - Don't ask us to pull from your master branch.
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.
## Running Tests
``` bash
$ phpunit
```
**Happy coding**!

View File

@@ -0,0 +1,28 @@
# The BSD License (BSD)
Copyright (c) 2013-2015, 2amigOS! Consulting Group LLC.
> Redistribution and use in source and binary forms, with or without modification,
> are permitted provided that the following conditions are met:
>
> Redistributions of source code must retain the above copyright notice, this
> list of conditions and the following disclaimer.
>
> Redistributions in binary form must reproduce the above copyright notice, this
> list of conditions and the following disclaimer in the documentation and/or
> other materials provided with the distribution.
>
> Neither the name of 2amigOS! Consulting Group, LLC. nor the names of its
> contributors may be used to endorse or promote products derived from
> this software without specific prior written permission.
>
>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
>ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
>WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
>DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
>ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
>(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
>LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
>ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
>(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
>SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@@ -0,0 +1,79 @@
ArrayQuery Component for Yii2
=============================
[![Latest Version](https://img.shields.io/github/tag/2amigos/yii2-arrayquery-component.svg?style=flat-square&label=release)](https://github.com/2amigos/yii2-arrayquery-component/tags)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/2amigos/yii2-arrayquery-component/master.svg?style=flat-square)](https://travis-ci.org/2amigos/yii2-arrayquery-component)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/2amigos/yii2-arrayquery-component.svg?style=flat-square)](https://scrutinizer-ci.com/g/2amigos/yii2-arrayquery-component/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/g/2amigos/yii2-arrayquery-component.svg?style=flat-square)](https://scrutinizer-ci.com/g/2amigos/yii2-arrayquery-component)
[![Total Downloads](https://img.shields.io/packagist/dt/2amigos/yii2-arrayquery-component.svg?style=flat-square)](https://packagist.org/packages/2amigos/yii2-arrayquery-component)
Allows searching/filtering of an array. This component is very useful when displaying array data in GridViews with an
ArrayDataProvider.
Installation
------------
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require "2amigos/yii2-arrayquery-component" "*"
```
or add
```
"2amigos/yii2-arrayquery-component" : "*"
```
to the require section of your application's `composer.json` file.
Usage
-----
```
\\ $models is the array elements to used with ArrayDataProvider
$query = new ArrayQuery($models);
$models = $query
->addCondition('name', '~2amigos')
->addCondition('name', 'cebe/yii2-gravatar', 'or')
->find();
$dataProvider = new ArrayDataProvider([
'allModels' => $models,
'pagination' => [
'pageSize' => 50,
],
'sort' => [
'attributes' => [], // to be specified
],
]);
```
## Testing
``` bash
$ phpunit
```
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## Credits
- [Antonio Ramirez](https://github.com/tonydspaniard)
- [All Contributors](../../contributors)
## License
The BSD License (BSD). Please see [License File](LICENSE.md) for more information.
> [![2amigOS!](http://www.gravatar.com/avatar/55363394d72945ff7ed312556ec041e0.png)](http://www.2amigos.us)
<i>Custom Software | Web & Mobile Software Development</i>
[www.2amigos.us](http://www.2amigos.us)

View File

@@ -0,0 +1,46 @@
{
"name": "2amigos/yii2-arrayquery-component",
"description": "Yii2 component that allows for searching/filtering the elements of an array.",
"type": "yii2-extension",
"keywords": [
"2amigos",
"yii",
"yii2",
"yii 2",
"extension",
"component",
"arrayquery"
],
"homepage": "http://yiiwheels.com/extension/arrayquery-component",
"license": "BSD-3-Clause",
"authors": [
{
"name": "2amigOS! Consulting Group",
"email": "hola@2amigos.us",
"homepage": "http://2amigos.us",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/dosamigos/yii2-arrayquery-component/issues",
"source": "https://github.com/dosamigos/yii2-arrayquery-component"
},
"require": {
"yiisoft/yii2": "~2.0.11"
},
"require-dev": {
"phpunit/phpunit": "~5.6",
"friendsofphp/php-cs-fixer": "^2.0",
"scrutinizer/ocular": "~1.1"
},
"autoload": {
"psr-4": {
"dosamigos\\arrayquery\\": "src"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}

View File

@@ -0,0 +1,208 @@
<?php
/**
* @copyright Copyright (c) 2013-2017 2amigos! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\arrayquery;
use dosamigos\arrayquery\conditions\Equal;
use dosamigos\arrayquery\conditions\GreaterThan;
use dosamigos\arrayquery\conditions\GreaterThanOrEqual;
use dosamigos\arrayquery\conditions\LessThan;
use dosamigos\arrayquery\conditions\LessThanOrEqual;
use dosamigos\arrayquery\conditions\Like;
use dosamigos\arrayquery\conditions\NotLike;
/**
* ArrayQuery allows to filter an array by apply conditions.
*
* @author Antonio Ramirez <hola@2amigos.us>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\arrayquery
*/
class ArrayQuery
{
/**
* @var array the data to search, filter.
*/
private $data;
/**
* @var array the array tokenized so user can search multidimensional array by key paths -ie `parentkey.child`
*/
private $tokens;
/**
* @var array the conditions to apply
*/
private $conditions = [];
/**
* @param array $array
*/
public function __construct(array $array)
{
$this->data = $array;
foreach ($array as $k => $item) {
$this->tokens[$k] = $this->tokenize($item, '', false);
}
}
/**
* Adds a condition to apply the array
*
* @param string $key the key to search in the array
* @param mixed $value the value to search. It supports SQL like operator plus some custom ones:
* - `~` or `like` : like `%value%` in SQL
* - `n~` or `nlike` : like `NOT LIKE` in SQL
* @param string $operator the operator. It can be `and` or `or`. If any of `or` matches it will be added to the
* successful results.
*
* @return static
*/
public function addCondition($key, $value, $operator = 'and')
{
if ($value != null) { // not accepting null values
$operation = null;
$operator = strcasecmp($operator, 'or') !== 0 ? 'and' : 'or';
if (preg_match('/^(?:\s*(<>|<=|>=|<|>|=|~|n~|like|nlike))?(.*)$/i', $value, $matches)) {
$operation = $matches[1];
$value = trim($matches[2]);
}
if (empty($operation) || strlen($operation) > 5) {
$operation = '=';
}
switch ($operation) {
case '<':
$condition = new LessThan($value);
break;
case '>':
$condition = new GreaterThan($value);
break;
case '<>':
$condition = new Equal($value);
$condition->reverse();
break;
case '<=':
$condition = new LessThanOrEqual($value);
break;
case '>=':
$condition = new GreaterThanOrEqual($value);
break;
case '~':
case 'like':
$condition = new Like($value);
break;
case 'n~':
case 'nlike':
$condition = new NotLike($value);
break;
case '=':
default:
$condition = new Equal($value);
}
$this->conditions[$operator][] = ['condition' => $condition, 'key' => $key];
}
return $this;
}
/**
* Returns the first matched result.
* @return array the first matched result, empty array if none found.
*/
public function one()
{
foreach ($this->tokens as $key => $token) {
if (!$this->matches($token)) {
continue;
}
return $this->data[$key];
}
return [];
}
/**
* Returns array of matched results.
* @return array the matched results.
*/
public function find()
{
if (empty($this->conditions)) {
return $this->data;
}
$results = [];
foreach ($this->tokens as $key => $token) {
if (!$this->matches($token)) {
continue;
}
$results[$key] = $this->data[$key];
}
return $results;
}
/**
* Tokenizes the array to ease the search in multidimensional arrays.
*
* @param array $array the array to tokenize
* @param string $prefix the key prefix
* @param bool $addParent whether to add parent value anyway. False
*
* @return array
*/
public function tokenize($array, $prefix = '', $addParent = true)
{
$paths = [];
$px = empty($prefix) ? null : $prefix . ".";
foreach ($array as $key => $items) {
if (is_array($items)) {
$addParent && $paths[$px . $key] = $items;
foreach ($this->tokenize($items, $px . $key) as $k => $path) {
$paths[$k] = $path;
}
} elseif (is_object($items)) {
$addParent && $paths[$px . $key] = $items;
foreach ($this->tokenize(get_object_vars($items), $px . $key) as $k => $path) {
$paths[$k] = $path;
}
} else {
$paths[$px . $key] = $items;
}
}
return $paths;
}
/**
* Checks data against conditions
*
* @param mixed $data the data to match against.
*
* @return bool true if matches condition
*/
private function matches($data)
{
$matches = true;
$conditions = isset($this->conditions['and']) ? $this->conditions['and'] : [];
foreach ($conditions as $condition) {
$key = $condition['key'];
$condition = $condition['condition'];
if (!array_key_exists($key, $data) || !$condition->matches($data[$key])) {
$matches = false;
break;
}
}
$conditions = isset($this->conditions['or']) ? $this->conditions['or'] : [];
foreach ($conditions as $condition) {
$key = $condition['key'];
$condition = $condition['condition'];
if (array_key_exists($key, $data) && $condition->matches($data[$key])) {
$matches = true;
break;
}
}
return $matches;
}
}

View File

@@ -0,0 +1,77 @@
<?php
/**
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\arrayquery\conditions;
/**
* Condition abstract base class where all conditions extend from
*
* @author Antonio Ramirez <hola@2amigos.us>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\arrayquery\conditions
*/
abstract class Condition
{
/**
* @var mixed the value to match against
*/
protected $value;
/**
* @var bool whether to reverse or not
*/
protected $negate = false;
/**
* @param mixed $value the value to match against
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* Reverses the condition
* @return $this
*/
public function reverse()
{
$this->negate = !$this->negate;
return $this;
}
/**
* Checks whether the value passes condition
*
* @param mixed $data the data to match
*
* @return mixed
*/
abstract public function matches($data);
/**
* Checks whether the value and the data are of same type
*
* @param mixed $value
* @param mixed $against
*
* @return bool true if both are of same type
*/
protected function checkType($value, $against)
{
if (is_numeric($value) && is_numeric($against)) {
$value = filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT);
$against = filter_var($against, FILTER_SANITIZE_NUMBER_FLOAT);
}
if (gettype($value) != gettype($against)) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\arrayquery\conditions;
/**
* Equal checks if value is equal to the data searched.
*
* @author Antonio Ramirez <hola@2amigos.us>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\arrayquery\conditions
*/
class Equal extends Condition
{
/**
* @inheritdoc
*/
public function matches($data)
{
return (($this->checkType($data, $this->value) && strcmp($data, $this->value) === 0) xor $this->negate);
}
}

View File

@@ -0,0 +1,38 @@
<?php
/**
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\arrayquery\conditions;
/**
* GreaterThan checks if value is greater than the data searched.
*
* @author Antonio Ramirez <hola@2amigos.us>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\arrayquery\conditions
*/
class GreaterThan extends Condition
{
/**
* Returns [[LessThan]] condition
* @return LessThan
*/
public function reverse()
{
return new LessThan($this->value);
}
/**
* @inheritdoc
*/
public function matches($data)
{
return ($this->checkType($data, $this->value) && $data > $this->value);
}
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\arrayquery\conditions;
/**
* GreaterThanOrEqual checks if value is greater or equal to the data searched.
*
* @author Antonio Ramirez <hola@2amigos.us>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\arrayquery\conditions
*/
class GreaterThanOrEqual extends Condition
{
/**
* Returns [[LessThanOrEqual]] condition
* @return LessThanOrEqual
*/
public function reverse()
{
return new LessThanOrEqual($this->value);
}
/**
* @inheritdoc
*/
public function matches($data)
{
$gt = new GreaterThan($this->value);
$e = new Equal($this->value);
return $gt->matches($data) || $e->matches($data);
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\arrayquery\conditions;
/**
* LessThan checks if value is less than to the data searched.
*
* @author Antonio Ramirez <hola@2amigos.us>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\arrayquery\conditions
*/
class LessThan extends Condition
{
/**
* Returns [[GreaterThan]] condition
* @return GreaterThan
*/
public function reverse()
{
return new GreaterThan($this->value);
}
/**
* @inheritdoc
*/
public function matches($data)
{
return ($this->checkType($data, $this->value) && $data < $this->value);
}
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\arrayquery\conditions;
/**
* LessThanOrEqual checks if value is less than or equal to the data searched.
*
* @author Antonio Ramirez <hola@2amigos.us>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\arrayquery\conditions
*/
class LessThanOrEqual extends Condition
{
/**
* Returns [[GreaterThanOrEqual]] condition
* @return GreaterThanOrEqual
*/
public function reverse()
{
return new GreaterThanOrEqual($this->value);
}
/**
* @inheritdoc
*/
public function matches($data)
{
$gt = new LessThan($this->value);
$e = new Equal($this->value);
return $gt->matches($data) || $e->matches($data);
}
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\arrayquery\conditions;
/**
* Like checks if value is matches any words in theå data searched.
*
* @author Antonio Ramirez <hola@2amigos.us>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\arrayquery\conditions
*/
class Like extends Condition
{
/**
* @inheritdoc
*/
public function matches($data)
{
return is_string($data) && mb_stripos($data, $this->value) !== false;
}
}

View File

@@ -0,0 +1,27 @@
<?php
/**
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\arrayquery\conditions;
/**
* NotLike checks if value is not within the data searched.
*
* @author Antonio Ramirez <hola@2amigos.us>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\arrayquery\conditions
*/
class NotLike extends Like
{
/**
* @inheritdoc
*/
public function matches($data)
{
return !parent::matches($data);
}
}