Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
borkweb committed May 22, 2024
1 parent 7f7de78 commit 2f61ea6
Show file tree
Hide file tree
Showing 7 changed files with 387 additions and 3 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = true

[*.{neon,neon.dist}]
indent_style = tab

[**.{jshintrc,json,scss-lint,yml}]
indent_style = space
indent_size = 2
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
composer.lock
files/
repo/
vendor/
tests/_support/_generated
bin/*.phar

# Dev tools
.buildpath
*.iml
.project
.idea/
.vscode/

# Third party dependencies
vendor/

# Tests
codeception.yml
tests/_output/*
tests/*.suite.yml
!tests/_data/dump.sql
!tests/_data/pue.sql
!tests/_data/pue-files
!tests/_pue-files/*.zip
tests/*.pem
tests/*.cert
tests/*.key
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 StellarWP
Copyright (c) 2023 StellarWP

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
110 changes: 108 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,108 @@
# superglobals
A small library that handles access to superglobals
# StellarWP SuperGlobals

A library that handles access to superglobals.

## Table of Contents

* [Installation](#installation)
* [Usage](#usage)
* [`Request::get_get_var( $var, $default = null )`](#get_get_var-var-default-null)
* [`Request::get_post_var( $var, $default = null )`](#get_post_var-var-default-null)
* [`Request::get_server_var( $var, $default = null )`](#get_server_var-var-default-null)
* [`Request::get_var( $var, $default = null )`](#get_var-var-default-null)
* [`Request::sanitize_deep( &$value )`](#sanitize_deep-value)

## Installation

It's recommended that you install SuperGlobals as a project dependency via [Composer](https://getcomposer.org/):

```bash
composer require stellarwp/superglobals
```

> We _actually_ recommend that this library gets included in your project using [Strauss](https://github.com/BrianHenryIE/strauss).
>
> Luckily, adding Strauss to your `composer.json` is only slightly more complicated than adding a typical dependency, so checkout our [strauss docs](https://github.com/stellarwp/global-docs/blob/main/docs/strauss-setup.md).
**An important note on namespaces:**

> The docs will in this repo all use `StellarWP\SuperGlobals` as the base namespace, however, if you are using [Strauss](#strauss)
> to prefix namespaces in your project, you will need to adapt the namespaces accordingly. (Example: `Boom\Shakalaka\StellarWP\SuperGlobals`)
## Usage

### `Request::get_get_var( $var, $default = null )`

Get a `$_GET` value and recursively sanitize it using `Request::sanitize_deep()`.

#### Example

```php
use StellarWP\SuperGlobals\Request;

// Get $_GET['post_id']
$var = Request::get_get_var( 'post_id' );

// Provide a default value if the variable is not set.
$var = Request::get_get_var( 'post_id', 12 );
```

### `Request::get_post_var( $var, $default = null )`

Get a `$_POST` value and recursively sanitize it using `Request::sanitize_deep()`.

#### Example

```php
use StellarWP\SuperGlobals\Request;

// Get $_POST['post_id']
$var = Request::get_post_var( 'post_id' );

// Provide a default value if the variable is not set.
$var = Request::get_post_var( 'post_id', 12 );
```

### `Request::get_server_var( $var, $default = null )`

Get a `$_SERVER` value and recursively sanitize it using `Request::sanitize_deep()`.

#### Example

```php
use StellarWP\SuperGlobals\Request;

// Get $_SERVER['REQUEST_URI']
$var = Request::get_server_var( 'REQUEST_URI' );

// Provide a default value if the variable is not set.
$var = Request::get_server_var( 'REQUEST_URI', 'http://example.com' );
```

### `Request::get_var( $var, $default = null )`

Gets a value from `$_REQUEST`, `$_POST`, or `$_GET` and recursively sanitizes it using `Request::sanitize_deep()`.

#### Example

```php
use StellarWP\SuperGlobals\Request;

// Get $_REQUEST['post_id'] or $_POST['post_id'] or $_GET['post_id'], wherever it lives
$var = Request::get_var( 'post_id' );

// Provide a default value if the variable is not set.
$var = Request::get_var( 'post_id', 12 );
```

### `Request::sanitize_deep( &$value )`

Sanitizes a value recursively using appropriate sanitization functions depending on the type of the value.

#### Example

```php
use StellarWP\SuperGlobals\Request;

$var = Request::sanitize_deep( $some_var );
```
56 changes: 56 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "stellarwp/superglobals",
"description": "A library that handles access to superglobals.",
"type": "library",
"license": "MIT",
"platform": {
"php": "7.4"
},
"autoload": {
"psr-4": {
"StellarWP\\SuperGlobals\\": "src/SuperGlobals/"
}
},
"autoload-dev": {
"psr-4": {
"StellarWP\\SuperGlobals\\Tests\\": "tests/_support/Helper/"
}
},
"authors": [
{
"name": "StellarWP",
"email": "[email protected]"
},
{
"name": "Matthew Batchelder",
"email": "[email protected]"
}
],
"minimum-stability": "stable",
"require": {
"stellarwp/arrays": "^1.2"
},
"require-dev": {
"codeception/module-asserts": "^1.0",
"codeception/module-cli": "^1.0",
"codeception/module-db": "^1.0",
"codeception/module-filesystem": "^1.0",
"codeception/module-phpbrowser": "^1.0",
"codeception/module-rest": "^1.0",
"codeception/module-webdriver": "^1.0",
"codeception/util-universalframework": "^1.0",
"lucatume/wp-browser": "^3.0.14",
"phpunit/phpunit": "~6.0",
"szepeviktor/phpstan-wordpress": "^1.1",
"symfony/event-dispatcher-contracts": "^2.5.1",
"symfony/string": "^5.4"
},
"scripts": {
"test:analysis": [
"phpstan analyse -c phpstan.neon.dist --memory-limit=512M"
]
},
"scripts-descriptions": {
"test:analysis": "Run static code analysis."
}
}
29 changes: 29 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Configuration for PHPStan
# https://phpstan.org/config-reference

includes:
# @see https://github.com/phpstan/phpstan-src/blob/master/conf/bleedingEdge.neon
- phar://phpstan.phar/conf/bleedingEdge.neon
# Include this extension
- vendor/szepeviktor/phpstan-wordpress/extension.neon

parameters:
parallel:
jobSize: 10
maximumNumberOfProcesses: 32
minimumNumberOfJobsPerProcess: 2
level: 5
inferPrivatePropertyTypeFromConstructor: true
reportUnmatchedIgnoredErrors: false
checkGenericClassInNonGenericObjectType: false
treatPhpDocTypesAsCertain: false

# Paths to be analyzed.
paths:
- %currentWorkingDirectory%/src

ignoreErrors:
# Uses func_get_args()
- '#^Function add_query_arg invoked with [123] parameters?, 0 required\.$#'
# Uses func_get_args()
- '#^Function apply_filters(_ref_array)? invoked with [34567] parameters, 2 required\.$#'
Loading

0 comments on commit 2f61ea6

Please sign in to comment.