{"id":37014861,"url":"https://github.com/xtompie/guard","last_synced_at":"2026-01-14T01:29:28.133Z","repository":{"id":57085137,"uuid":"342917277","full_name":"xtompie/guard","owner":"xtompie","description":"Guard container object for dealing with optional/null values","archived":false,"fork":false,"pushed_at":"2021-05-01T08:55:25.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-06T11:36:33.884Z","etag":null,"topics":["guard","null","nullable","nullpointerexception","optional"],"latest_commit_sha":null,"homepage":"https://github.com/xtompie/guard","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xtompie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-27T17:33:51.000Z","updated_at":"2024-05-24T19:47:49.000Z","dependencies_parsed_at":"2022-08-24T14:56:37.615Z","dependency_job_id":null,"html_url":"https://github.com/xtompie/guard","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/xtompie/guard","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtompie%2Fguard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtompie%2Fguard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtompie%2Fguard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtompie%2Fguard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xtompie","download_url":"https://codeload.github.com/xtompie/guard/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtompie%2Fguard/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28408112,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T00:40:43.272Z","status":"ssl_error","status_checked_at":"2026-01-14T00:40:42.636Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["guard","null","nullable","nullpointerexception","optional"],"created_at":"2026-01-14T01:29:27.664Z","updated_at":"2026-01-14T01:29:28.123Z","avatar_url":"https://github.com/xtompie.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Guard\n\nGuard container object for dealing with optional/null values.\n\n```php\nuse Xtompie\\Guard\\Guard;\n\n$user = Guard::of(request()-\u003einput('id'))\n    -\u003efilter(fn($id) =\u003e ctype_digit($id))\n    -\u003emap(fn($id) =\u003e User::find($id))\n    -\u003enot(fn() =\u003e abort(404))\n    -\u003eis(fn($user) =\u003e info(\"User found {$user-\u003eid}\"))\n    -\u003eget();\n```\n\n## Installation\n\nUsing [composer](https://getcomposer.org/)\n\n```\ncomposer require xtompie/guard\n```\n\n## Docs\n\nGuard protects for calling methods on nulls values. \nThe value might or might not be present.\nIt has elegant fluent chaining syntax.\nNo need for if/else statments and additional variables. \nGuard syntax is cleaner and more readable.\nGuard object is immutable.\n\nMethods:\n- `of` - Creates guard\n- `ofEmpty` - Creates empty guard\n- `is` - Tells if value present or runs callback when value present\n- `not` - Same as `is` but negation\n- `get` - Gets raw value\n- `getFn` - Gets raw value with fallback callback\n- `filter` - Filter the value if present\n- `reject` - Same as `filter` but negation\n- `map` - Maps value if present \n- `assert` - Throw exception if value is not present\n- `blank` - Gets guard with provided value if current is not present\n- `blankFn` - Same as `blank` but with callback\n- `let` - Returns guard capture mechanism for nested properties, calls, array offsets\n\nMore info in source [Guard.php](src/Guard.php)\n\n### Usage\n\n#### NoValueException\n\n```php\nuse Xtompie\\Guard\\Guard;\n\nGuard::of(null)-\u003eassert()-\u003eget(); // NoValueException will be thrown\n```\n\n#### Default value \n```php\nuse Xtompie\\Guard\\Guard;\n\necho Guard::of(null)-\u003eget('default'); // -\u003e default\n```\n\n#### Complex type and value\n```php\nuse Xtompie\\Guard\\Guard;\n\nfunction divide($a, $b) {\n    $b = Guard::of($b)\n        -\u003emap(fn($i) =\u003e (int)$i)\n        -\u003ereject(fn($i) =\u003e $i === 0)\n        -\u003eassert(\\UnexpectedValueException::class)\n        -\u003eget();\n    return $a / $b;\n}\n```\n\n#### Let\n\n`let()` Returns Let object. Offset get, property get, method call can be called on Let. \nAfter that operation new Guard with operation result will be returned.\nWhen offset, property, method not exist, empty Guard will be returned.\n\n```php\nuse Xtompie\\Guard\\Guard;\n\n$options = [\n    'a' =\u003e 'A',\n    'b' =\u003e 'B',\n];\n$key = 'c';\necho Guard::of($options)-\u003elet()[$key]-\u003eget();\n```\n\n```php\nuse Xtompie\\Guard\\Guard;\n\necho Guard::of(new \\stdClass())\n    -\u003elet()-\u003enonExistingMethod()\n    -\u003elet()-\u003enonExistingProperty\n    -\u003elet()['nonExistingOffset']\n    -\u003eget('Undefined')\n;\n```\n\n#### Extending\n\n```php\nnamespace MyApp\\Util;\n\nuse Xtompie\\Guard\\Guard as BaseGuard;\n\nclass Guard extends BaseGuard\n{\n    public function or404()\n    {\n        $this-\u003enot(fn() =\u003e abort(404));\n    }\n\n    public function reject0()\n    {\n        return $this-\u003ereject(fn($i) =\u003e $i === 0);\n    }\n}\n\necho gettype(Guard::of(0)-\u003ereject0()-\u003eget());\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtompie%2Fguard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxtompie%2Fguard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtompie%2Fguard/lists"}