Parable GetSet allows for clean and intuitive handling of global variables such as $_GET
and $_SERVER
using value
collections, in addition to resource objects with their own scope.
The major benefit is dot-notation (key.string
) for nested values, such as $getCollection->get('this.is.nested');
, which will
look in the GetCollection
's stored values for ['this']['is']['nested']
.
Collections always return the requested value or null
by default, allowing the predictable use of null coalescing.
Php 8.0+ and composer are required.
$ composer require parable-php/getset
Assuming a query string like: ?id=345&user[name]=username
:
$getCollection = new \Parable\GetSet\GetCollection();
echo $getCollection->get('id');
echo ':';
echo $getCollection->get('user.name');
Would output 345:username
Obviously you can also set values directly:
$getCollection->set('settings.mail.from', 'me@system');
// later
$mail_from = $getCollection->get('settings.mail.from') ?? 'nobody';
These correspond to their $_NAME
global variables, also found in the super-global $GLOBALS
variable.
CookieCollection
, handles$_COOKIE
data.FilesCollection
, handles$_FILES
data.GetCollection
, handles$_GET
data.PostCollection
, handles$_POST
data.ServerCollection
, handles$_SERVER
data.SessionCollection
, handles$_SESSION
data.
DataCollection
, a generic collection using only a local resource, useful for passing data throughout an application. Think configuration.InputStreamCollection
, to read out an input stream. Can import/parsejson
or query string data.
You can easily build your own collections by extending the BaseCollection
class and co-opting the easy
dot-notation get/set logic.
getAll(): array
- returns all values currently storedgetAllAndClear(): array
- returns all values currently stored and then clears themget(string $key, $default = null): mixed
- returns value by key or$default
if not foundgetAndRemove(string $key): mixed
- returns value by key and then removes it, or throw exception if it doesn't existset(string $key, $value): void
- set a value by keysetMany(array $values): void
- set all values passed one-by-one, overwriting pre-existing valuessetAll(array $values): void
- set all values, overwriting any pre-existing onesremove(string $key): void
- remove by key, if already removed, throwsclear(): void
- removes all valueshas(string $key): bool
- returns whether the passed key (orkey.string
) currently existscount(): int
- returns number of root value items (not all nested values)
Any suggestions, bug reports or general feedback is welcome. Use github issues and pull requests, or find me over at devvoh.com.
All Parable components are open-source software, licensed under the MIT license.