{"id":27091694,"url":"https://github.com/dgame/php-ensurance","last_synced_at":"2025-04-06T07:53:28.327Z","repository":{"id":48939445,"uuid":"63493775","full_name":"Dgame/php-ensurance","owner":"Dgame","description":null,"archived":false,"fork":false,"pushed_at":"2021-07-04T19:24:34.000Z","size":140,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T23:24:33.484Z","etag":null,"topics":["constraints","design-by-contracts","ensurance","php"],"latest_commit_sha":null,"homepage":null,"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/Dgame.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":"2016-07-16T17:08:03.000Z","updated_at":"2023-03-05T03:25:22.000Z","dependencies_parsed_at":"2022-09-22T14:41:16.968Z","dependency_job_id":null,"html_url":"https://github.com/Dgame/php-ensurance","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-ensurance","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-ensurance/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-ensurance/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-ensurance/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Dgame","download_url":"https://codeload.github.com/Dgame/php-ensurance/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247451628,"owners_count":20940946,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["constraints","design-by-contracts","ensurance","php"],"created_at":"2025-04-06T07:53:27.737Z","updated_at":"2025-04-06T07:53:28.317Z","avatar_url":"https://github.com/Dgame.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-ensurance\n\n[![CircleCI](https://circleci.com/gh/Dgame/php-ensurance/tree/master.svg?style=svg)](https://circleci.com/gh/Dgame/php-ensurance/tree/master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Dgame/php-ensurance/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Dgame/php-ensurance/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/Dgame/php-ensurance/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Dgame/php-ensurance/?branch=master)\n[![Build Status](https://scrutinizer-ci.com/g/Dgame/php-ensurance/badges/build.png?b=master)](https://scrutinizer-ci.com/g/Dgame/php-ensurance/build-status/master)\n[![StyleCI](https://styleci.io/repos/63493775/shield?branch=master)](https://styleci.io/repos/63493775)\n\n## design by contract for PHP\n\nIf your check fails, an Exception is thrown\n\n## Strings\n\n### equality\n```php\nensure('foo')-\u003eisString()-\u003eisEqualTo('foo');\nensure('foo')-\u003eisString()-\u003eisNotEqualTo('bar');\n```\n\n### pattern\n```php\nensure('test@foo')-\u003eisString()-\u003ematches('#^[a-z]+@\\w{3}$#i');\nensure('FooBar')-\u003eisString()-\u003ebeginsWith('Fo');\nensure('FooBar')-\u003eisString()-\u003eendsWith('ar');\n```\n\n### size\n```php\nensure('foo')-\u003eisString()-\u003ehasLengthOf(3);\nensure('foo')-\u003eisString()-\u003eisShorterThan(4);\nensure('foo')-\u003eisString()-\u003eisLongerThan(2);\n```\n\nand more\n\n## Numerics\n\n### type check\n```php\nensure(42)-\u003eisInt();\nensure('42')-\u003eisInt();\n\nensure(4.2)-\u003eisFloat();\nensure('4.2')-\u003eisFloat();\n```\n\n### value check\n```php\nensure(42)-\u003eisNumeric()-\u003eisGreaterThan(23);\nensure(23)-\u003eisNumeric()-\u003eisLessThan(42);\nensure(42)-\u003eisEqualTo(42);\n```\n\n### positive / negative\n```php\nforeach (range(0, 100) as $n) {\n    ensure($n)-\u003eisPositive();\n}\n````\n\n```php\nforeach (range(-1, -100) as $n) {\n    ensure($n)-\u003eisNegative();\n}\n```\n\n### even / odd\n```php\nfor ($i = 0; $i \u003c 42; $i += 2) {\n    ensure($i)-\u003eisEven();\n}\n```\n\n```php\nfor ($i = 1; $i \u003c 42; $i += 2) {\n    ensure($i)-\u003eisOdd();\n}\n```\n\n### between range\n```php\nensure(2)-\u003eisNumeric()-\u003eisBetween(1, 3);\n```\n\n## array\n\n### check for a key\n```php\nensure(['a' =\u003e 'b'])-\u003eisArray()-\u003ehasKey('a');\n```\n\n### check for a value\n```php\nensure(['a', 'b'])-\u003eisArray()-\u003ehasValue('a');\n```\n\n### check length\n```php\nensure([])-\u003eisArray()-\u003ehasLengthOf(0);\nensure(range(0, 99))-\u003eisArray()-\u003ehasLengthOf(100);\n```\n\n```php\nensure([1, 2, 3])-\u003eisArray()-\u003eisShorterThan(4);\nensure([1, 2, 3])-\u003eisArray()-\u003eisLongerThan(2);\n```\n\n### check if associativ or not\n```php\nensure(['a' =\u003e 'b'])-\u003eisArray()-\u003eisAssociative();\n```\n\n## ensure not empty / not null\n\n```php\nensure('')-\u003eisNotNull()-\u003eisNotEmpty();\n```\n\n## ensure identity (`===`) / equality (`==`)\n```php\nensure(42)-\u003eisEqualTo('42');\n```\n\n```php\nensure(42)-\u003eisIdenticalTo(42);\n```\n\n## bool\n\n### is true / false\n\n```php\nensure((2 * 3) === (3 * 2))-\u003eisTrue();\nensure((2 * 3) === (3 * 3))-\u003eisFalse();\n```\n----\n\nYou can also specify your own Exception messages:\n\n```php\nensure(1 === 1)-\u003eisTrue()-\u003eorThrow('You will never see this error');\n```\n\n# Enforcement\n\nIf you want to enforce that some condition is true, use `enforce`:\n```php\nenforce(true)-\u003eorThrow('That is not true...');\n```\n\nIf you don't specify a Throwable, an `AssertionError` will be used:\n```php\nenforce(0); // throws AssertionError\n```\n\n# Expectations\n\nBind expectations to your values and offer default values if the expectation don't apply.\nYou can either use `else` or `then` to evaluate if an Throwable was thrown. The usage of `else` or `then` will disregard and invalidate the Throwable internally:\n\n```php\n$this-\u003eassertEquals('foo', ensure(42)-\u003eisEven()-\u003ethen('foo'));\n$this-\u003eassertEquals(23, ensure(42)-\u003eisOdd()-\u003eelse(23));\n```\n\nalso you can use `either ... or` to set values for both outcomes:\n\n```php\n$this-\u003eassertTrue(ensure(42)-\u003eisOdd()-\u003eeither(false)-\u003eor(true));\n$this-\u003eassertFalse(ensure(23)-\u003eisOdd()-\u003eeither(false)-\u003eor(true));\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgame%2Fphp-ensurance","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgame%2Fphp-ensurance","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgame%2Fphp-ensurance/lists"}