PHPackages                             gowork/safe - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. gowork/safe

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

gowork/safe
===========

Type Safety Tools

v0.2.2(3y ago)532.4k↓22.2%2[2 PRs](https://github.com/gowork/safe/pulls)1MITPHPPHP ^7.4 || ^8.0

Since Mar 23Pushed 3y ago2 watchersCompare

[ Source](https://github.com/gowork/safe)[ Packagist](https://packagist.org/packages/gowork/safe)[ RSS](/packages/gowork-safe/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (8)Dependencies (5)Versions (8)Used By (1)

Gowork Safe - Type Safety Tools
===============================

[](#gowork-safe---type-safety-tools)

Safe accessors wraps unsafe or uncertain associative data structures and provides methods of safe type casting. Mainly for Symfony.

SafeAccessorTrait methods
-------------------------

[](#safeaccessortrait-methods)

#### `bool(string $key, bool $default = false): bool`

[](#boolstring-key-bool-default--false-bool)

- casts value to `bool` (true, false, 0, 1) if possible
- or throws `InvalidArgumentException` when value set but cannot be casted
- or returns default when value not set

#### `boolOrDefault(string $key, bool $default): bool`

[](#boolordefaultstring-key-bool-default-bool)

- casts value to `bool` (true, false, 0, 1)
- or returns default

#### `string(string $key, string $default = ''): string`

[](#stringstring-key-string-default---string)

- casts value to `string` if possible
- or throws `InvalidArgumentException` when value set but cannot be casted
- or returns default when value not set

#### `stringNullable(string $key, ?string $default = null): ?string`

[](#stringnullablestring-key-string-default--null-string)

- casts value to `string` if possible
- or returns default when value not set or is `null`
- or throws `InvalidArgumentException` when value not `null` but cannot be casted

#### `stringOrNull(string $key): ?string`

[](#stringornullstring-key-string)

- casts value to `string` if possible
- or returns `null`

#### `stringOrDefault(string $key, string $default): string`

[](#stringordefaultstring-key-string-default-string)

- casts value to `string` if possible
- or returns default

#### `int(string $key, int $default = 0): int`

[](#intstring-key-int-default--0-int)

- casts value to `int` if possible
- or throws `InvalidArgumentException` when value set but cannot be casted
- or returns default when value not set

#### `intNullable(string $key, ?int $default = null): ?int`

[](#intnullablestring-key-int-default--null-int)

- casts value to `int` if possible
- or returns default when value not set or is `null`
- or throws `InvalidArgumentException` when value not `null` but cannot be casted

#### `intOrNull(string $key): ?int`

[](#intornullstring-key-int)

- casts value to `int` if possible
- or returns `null`

#### `intOrDefault(string $key, int $default): int`

[](#intordefaultstring-key-int-default-int)

- casts value to `int` if possible
- or returns default

#### `float(string $key, float $default = 0): float`

[](#floatstring-key-float-default--0-float)

- casts value to `float` if possible
- or throws `InvalidArgumentException` when value set but cannot be casted
- or returns default when value not set

#### `floatNullable(string $key, ?float $default = null): ?float`

[](#floatnullablestring-key-float-default--null-float)

- casts value to `float` if possible
- or returns default when value not set or is `null`
- or throws `InvalidArgumentException` when value not `null` but cannot be casted

#### `floatOrNull(string $key): ?float`

[](#floatornullstring-key-float)

- casts value to `float` if possible
- or returns `null`

#### `floatOrDefault(string $key, float $default): float`

[](#floatordefaultstring-key-float-default-float)

- casts value to `float` if possible
- or returns default

#### `strings(string $key): array`

[](#stringsstring-key-arrayint-string)

- casts value to array of strings if possible
- or throws `InvalidArgumentException` when some item cannot be casted

#### `stringsFiltered(string $key): array`

[](#stringsfilteredstring-key-arrayint-string)

- casts value to array of strings skipping items that cannot be casted

#### `stringsForced(string $key, string $default = ''): array`

[](#stringsforcedstring-key-string-default---arrayint-string)

- casts value to array of strings replacing with default items that cannot be casted

#### `ints(string $key): array`

[](#intsstring-key-arrayint-int)

- casts value to array of ints if possible
- or throws `InvalidArgumentException` when some item cannot be casted

#### `intsFiltered(string $key): array`

[](#intsfilteredstring-key-arrayint-int)

- casts value to array of ints skipping items that cannot be casted

#### `intsForced(string $key, int $default = ''): array`

[](#intsforcedstring-key-int-default---arrayint-int)

- casts value to array of ints replacing with default items that cannot be casted

#### `floats(string $key): array`

[](#floatsstring-key-arrayint-float)

- casts value to array of floats if possible
- or throws `InvalidArgumentException` when some item cannot be casted

#### `floatsFiltered(string $key): array`

[](#floatsfilteredstring-key-arrayint-float)

- casts value to array of floats skipping items that cannot be casted

#### `floatsForced(string $key, float $default = ''): array`

[](#floatsforcedstring-key-float-default---arrayint-float)

- casts value to array of floats replacing with default items that cannot be casted

#### `array(string $key): SafeAssocArray`

[](#arraystring-key-safeassocarray)

- casts value to associative array and wraps with `SafeAssocArray`
- or throws `InvalidArgumentException` when value cannot be casted

#### `list(string $key): SafeAssocList`

[](#liststring-key-safeassoclist)

- casts value to list of associative arrays and wraps with `SafeAssocList`
- or throws `InvalidArgumentException` when value cannot be casted

Accessors
---------

[](#accessors)

### SafeAssocArray

[](#safeassocarray)

```
$user = [
    'name' => 'John',
    'age' => 18,
    'sports' => ['football', 'handball'],
];

$safe = SafeAssocArray::from($user);
$safe->string('name');             // 'John'
$safe->int('age');                 // 18

$safe->string('nickname', '--');   // '-'
$safe->stringNullable('nickname'); // NULL
$safe->string('nickname');         // InvalidArgumentException

$safe->strings('sports');          // ['football', 'handball']
$safe->ints('sports');             // InvalidArgumentException
```

### SafeConsoleInput

[](#safeconsoleinput)

```
final class ExampleCommand extends Command
{
    // ...
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $arguments = SafeConsoleInput::arguments($input);

        // require string from argument
        $file = $arguments->string('name');

        $options = SafeConsoleInput::options($input);

        // integer with default value
        $limit = $options->int('limit', 20);

        // optional integer value
        $pageOrNull = $options->intNullable('page');

        // bool
        $isDryRun = $options->bool('dry-run', false);

        // string[]
        $tags = $options->strings('tag');

        // int[]
        $tags = $options->ints('status');
    }
}
```

### SafeRequest

[](#saferequest)

```
final class ExampleAction extends Command
{
    // ...
    public function __invoke(Request $request): Response
    {
        $safeRequest = SafeRequest::from($request);
        $query = $safeRequest->query();
        $post = $safeReques->request();
        $attributes = $safeReques->attributes();

        $ip = $safeReques->ip();
        $postId = $attributes->string('postId');
        $tags = $post->strings('tags');
        // ...
    }
}
```

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 64.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~210 days

Total

6

Last Release

1241d ago

PHP version history (2 changes)0.1.0PHP ^7.4

v0.2PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/180052?v=4)[Dawid Łakomski](/maintainers/dlakomski)[@dlakomski](https://github.com/dlakomski)

![](https://www.gravatar.com/avatar/89312a3f1019de6ee55c9170b96980ecba62786feb01b710d3d3c52f9de53e84?d=identicon)[gowork](/maintainers/gowork)

---

Top Contributors

[![bronek89](https://avatars.githubusercontent.com/u/3485209?v=4)](https://github.com/bronek89 "bronek89 (9 commits)")[![dazet](https://avatars.githubusercontent.com/u/9936733?v=4)](https://github.com/dazet "dazet (3 commits)")[![dlakomski](https://avatars.githubusercontent.com/u/180052?v=4)](https://github.com/dlakomski "dlakomski (2 commits)")

---

Tags

phpsymfonytypedtypesafe

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gowork-safe/health.svg)

```
[![Health](https://phpackages.com/badges/gowork-safe/health.svg)](https://phpackages.com/packages/gowork-safe)
```

###  Alternatives

[enomotodev/php-cs-fixer-commit

Create commit of php-cs-fixer

18126.0k](/packages/enomotodev-php-cs-fixer-commit)[paulzi/yii2-materialized-path

Materialized Path Behavior for Yii2

2619.6k8](/packages/paulzi-yii2-materialized-path)[padam87/address-bundle

Symfony2 AddressBundle

1013.2k](/packages/padam87-address-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
