Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tech/laravelize #2

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,56 @@
name: Tests

on: [push, pull_request]
on:
push:
branches:
- main
pull_request: ~

jobs:
test:

runs-on: ubuntu-latest
build:
runs-on: ubuntu-20.04
strategy:
fail-fast: true
matrix:
php: [8.0, 7.4, 7.3]
dependency-version: [prefer-lowest, prefer-stable]
php: [ 8.2, 8.1, 8.0, 7.4 ]
dependency-version: [ prefer-lowest, prefer-stable ]

name: P${{ matrix.php }} - ${{ matrix.dependency-version }}

steps:
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/checkout@v2

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
- uses: symfonycorp/security-checker-action@v4

- name: Setup PHP
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring, zip, pcntl
coverage: none

- name: Install dependencies
- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache composer dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: composer-${{ hashFiles('composer.lock') }}
restore-keys: composer-

- name: Install Composer dependencies
run: composer install -n

- name: Validate composer.lock
run: composer validate --strict

- name: Run cs fixer
run: vendor/bin/php-cs-fixer fix --dry-run

- name: Run Phpstan
run: vendor/bin/phpstan --no-progress --debug --memory-limit=1G

- name: Run tests
run: vendor/bin/phpunit tests
run: vendor/bin/phpunit tests
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
build
composer.lock
vendor
.phpunit.result.cache
.php-cs-fixer.cache
Expand Down
160 changes: 160 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$rules = [
'array_syntax' => ['syntax' => 'short'],

'no_unused_imports' => true,
'blank_line_after_namespace' => true,
'blank_line_after_opening_tag' => true,
'braces' => true,
'cast_spaces' => true,
'concat_space' => [
'spacing' => 'none',
],
'declare_equal_normalize' => true,
'elseif' => true,
'encoding' => true,
'full_opening_tag' => true,
'fully_qualified_strict_types' => true, // added by Shift
'function_declaration' => true,
'function_typehint_space' => true,
'heredoc_to_nowdoc' => true,
'include' => true,
'increment_style' => ['style' => 'post'],
'indentation_type' => true,
'linebreak_after_opening_tag' => true,
'line_ending' => true,
'lowercase_cast' => true,
'lowercase_keywords' => true,
'lowercase_static_reference' => true, // added from Symfony
'magic_method_casing' => true, // added from Symfony
'magic_constant_casing' => true,
'method_argument_space' => true,
'native_function_casing' => true,
'no_extra_blank_lines' => [
'tokens' => [
'extra',
'throw',
'use',
'use_trait',
],
],
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_closing_tag' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_mixed_echo_print' => [
'use' => 'echo',
],
'no_multiline_whitespace_around_double_arrow' => true,
'multiline_whitespace_before_semicolons' => [
'strategy' => 'no_multi_line',
],
'no_short_bool_cast' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_after_function_name' => true,
'no_spaces_inside_parenthesis' => true,
'no_trailing_comma_in_list_call' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
'normalize_index_brace' => true,
'not_operator_with_successor_space' => true,
'object_operator_without_whitespace' => true,
'phpdoc_indent' => true,
'phpdoc_no_access' => true,
'phpdoc_no_package' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_scalar' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_summary' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_types' => true,
'phpdoc_var_without_name' => true,
'short_scalar_cast' => true,
'simplified_null_return' => false, // disabled by Shift
'single_blank_line_at_eof' => true,
'single_blank_line_before_namespace' => true,
'single_import_per_statement' => true,
'single_line_after_imports' => true,
'single_line_comment_style' => [
'comment_types' => ['hash'],
],
'single_quote' => true,
'space_after_semicolon' => true,
'standardize_not_equals' => true,
'switch_case_semicolon_to_colon' => true,
'switch_case_space' => true,
'ternary_operator_spaces' => true,
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'whitespace_after_comma_in_array' => true,

// php-cs-fixer 3: Renamed rules
'constant_case' => ['case' => 'lower'],
'general_phpdoc_tag_rename' => true,
'phpdoc_inline_tag_normalizer' => true,
'phpdoc_tag_type' => true,
'trailing_comma_in_multiline' => ['elements' => ['arrays']],

// php-cs-fixer 3: Changed options
'binary_operator_spaces' => [
'default' => 'single_space',
'operators' => ['=>' => null],
],
'blank_line_before_statement' => [
'statements' => ['return'],
],
'class_attributes_separation' => [
'elements' => [
'const' => 'one',
'method' => 'one',
'property' => 'one',
],
],
'class_definition' => [
'multi_line_extends_each_single_line' => true,
'single_item_single_line' => true,
'single_line' => true,
],
'ordered_imports' => [
'sort_algorithm' => 'alpha',
],

// php-cs-fixer 3: Removed rootless options (*)
'no_unneeded_control_parentheses' => [
'statements' => ['break', 'clone', 'continue', 'echo_print', 'return', 'switch_case', 'yield'],
],
'no_spaces_around_offset' => [
'positions' => ['inside', 'outside'],
],
'visibility_required' => [
'elements' => ['property', 'method', 'const'],
],

];

return (new Config())
->setRules($rules)
->setFinder(
Finder::create()
->in(__DIR__)
->path([
'src',
'tests',
'config',
])
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true)
);
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ Filter out data from an array of a given conditions

## Installation
```sh
composer require cdinopol/data-guard
composer require acdphp/data-guard
```

## Usage
```
DataGuard::protect(array $data, string $resource, array $conditions, $mask (optional));
```php
$data = (new DataGuard(':', '|', '[]', '###'));

# Hide
$data->hide(array $data, string $resource, string $search, string $operator, mixed $value);

# Mask
$data->mask(array $data, string $resource, string $search, string $operator, mixed $value);
```

### Data
Expand All @@ -25,27 +31,26 @@ DataGuard::protect(array $data, string $resource, array $conditions, $mask (opti
- `:` - key separator, hierarchy of keys to match from root to child.
- `[]` - array indicator, DataGuard will look inside each of the values instead of directly looking for the next key.

### Conditions
- Conditions can be formatted into 3 types:
1. `"*"` - means it will match all from the given resource.
2. `[[operator, value]]` - this will match the given resource directly to the search value.
3. `[[search_resource, operator, value]]` - instead of matching the given resource directly, you can pass another resource (same formatting as resource) as the first index of condition to match against the operator+value. search_resource will be searched through and matched, but the process point will still be on the given resource.
- All conditions in an array must match to be considered positive. Tip: Running OR condition means running DataGuard with another set conditions
## Search (optional)
- instead of matching the given resource directly, you can pass another resource (same formatting as resource) as the first index of condition to match against the operator+value. search_resource will be searched through and matched, but the process point will still be on the given resource.
- if not provided, last node of resource will be matched.

### Condition Operators
## Operators (optional)
```
1. = : equals
2. != : not equals
3. in : in array
4. !in : not in array
5. > : greater than
6. < : less than
7. regex : Regular Expression; condition value must be a proper expression
7. <= : less than or equal
8. >= : greater than or equal
9. regex : Regular Expression; condition value must be a proper expression
```
- if not provided, `=` (equals) will be used

### Mask
- any value, optional.
- will replace the resource value to this instead of removing it.
## Value
- matches the search or resource with the given operator.

## Usage
```php
Expand Down Expand Up @@ -93,7 +98,7 @@ $data = [
[
'name' => 'Carl',
'profile' => [
'addresses' => [
'address' => [
[
'city' => 'Chicago',
'country' => 'USA',
Expand All @@ -109,9 +114,8 @@ $data = [
];

// Hides profile if city = Asgard
$resource = 'heroes[]|hero|villain|others[]:profile';
$conditions = [['address|addresses[]:city', '=', 'Asgard']];
$protectedData = DataGuard::protect($data, $resource, $conditions);
$protectedData = (new DataGuard())
->hide($data, 'heroes[]|hero|villain|others[]:profile', 'address|address[]:city', '=', 'Asgard');

print_r($protectedData);
# Result:
Expand Down
31 changes: 26 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "cdinopol/data-guard",
"name": "acdphp/data-guard",
"description": "Filter out data from an array of a given conditions",
"type": "library",
"license": "MIT",
Expand All @@ -11,15 +11,36 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.3 || ^8.0"
"php": "^7.4 || ^8.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^3.0"
"phpunit/phpunit": "^9.6",
"friendsofphp/php-cs-fixer": "^3.34",
"phpstan/phpstan": "^1.10",
"ekino/phpstan-banned-code": "^1.0",
"orchestra/testbench": "^6.36",
"nunomaduro/larastan": "^1.0"
},
"autoload": {
"psr-4": {
"Cdinopol\\DataGuard\\": "src/"
"Acdphp\\DataGuard\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Acdphp\\DataGuard\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"Acdphp\\DataGuard\\DataGuardServiceProvider"
]
}
},
"config": {
"platform": {
"php": "7.4"
}
}
}
Loading