-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joel Butcher
committed
Dec 2, 2022
0 parents
commit 8135730
Showing
13 changed files
with
867 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
* text=auto | ||
|
||
*.md diff=markdown | ||
*.php diff=php | ||
|
||
/.github export-ignore | ||
/tests export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
phpunit.xml.dist export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: tests | ||
|
||
on: | ||
push: | ||
pull_request: | ||
schedule: | ||
- cron: '0 0 * * *' | ||
|
||
jobs: | ||
tests: | ||
|
||
runs-on: ubuntu-18.04 | ||
strategy: | ||
fail-fast: true | ||
matrix: | ||
php: [ '8.0', 8.1, 8.2 ] | ||
|
||
name: PHP ${{ matrix.php }} | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
tools: composer:v2 | ||
coverage: xdebug | ||
|
||
- name: Install dependencies | ||
run: composer update --prefer-dist --no-interaction --no-progress | ||
|
||
- name: Execute tests | ||
run: vendor/bin/pest --verbose --coverage --min=100 | ||
|
||
- name: Check static analysis | ||
run: vendor/bin/phpstan --xdebug analyse | ||
|
||
- name: Check code style analysis | ||
run: vendor/bin/pint --test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.idea/ | ||
vendor/ | ||
composer.lock | ||
.env | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Joel Butcher | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# PHP Optional | ||
|
||
Inspired by Java's [Optional](https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/util/function/Predicate.java) class, this package aims to provide a comprehensive API | ||
for optional field values. | ||
|
||
## Examples | ||
|
||
You can use the `Optional` class in a variety of ways, see below for a few examples: | ||
|
||
### Updating a user's profile | ||
|
||
The below class indicates who this package may be used to update a users profile information. All fields are optional, because | ||
our user may not want to update all fields. | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\DataObjects; | ||
|
||
use JoelButcher\PhpOptional\Optional; | ||
use Psl\Type; | ||
use Webmozart\Assert\Assert; | ||
|
||
class UpdateProfile | ||
{ | ||
public function __construct( | ||
private readonly int $id | ||
private readonly Optional $name | ||
private readonly Optional $email | ||
private readonly Options $bio | ||
) { | ||
// | ||
} | ||
|
||
public static function fromFormRequest(int $id UpdateProfileRequest $request): self | ||
{ | ||
return new self( | ||
id: $id, | ||
name: Optional::forNullable($request->get('name')), | ||
email: Optional::forNullable($request->get('email')), | ||
bio: Optional::forNullable($request->get('bio')), | ||
); | ||
} | ||
|
||
public static function fromArray(array $data): self | ||
{ | ||
Assert::keyExists($post, 'id'); | ||
Assert::positiveInteger($post['id']); | ||
|
||
|
||
return new self( | ||
id: $data['id'], | ||
name: Optional::forOptionalArrayKey($data, 'name', Type\non_empty_string()), | ||
email: Optional::forOptionalArrayKey($data, 'email', Type\non_empty_string()), | ||
bio: Optional::forOptionalArrayKey($data, 'bio', Type\non_empty_string()), | ||
); | ||
} | ||
|
||
public function handle(UserRepository $users): void | ||
{ | ||
$user = $users->findOneById($this->id); | ||
|
||
// These callbacks are only called if the value for each optional field is present. | ||
$this->name->apply(fn (string $name) => $user->updateName($name)); | ||
$this->email->apply(fn (string $email) => $user->updateEmail($email)); | ||
$this->bio->apply(fn (string $bio) => $user->updateBio($bio)); | ||
|
||
$users->save($user); | ||
} | ||
} | ||
``` | ||
|
||
Noticed the `\Psl\Type\non_empty_string()` call? That's an abstraction coming from `azjezz/psl`, which allows for having a type declared both at runtime and at static analysis level. We use it to parse inputs into valid values, or to produce crashes, if something is malformed. | ||
|
||
The `azjezz/psl`, `Psl\Type` tooling gives us both type safety and runtime validation by implicitly validating our values: | ||
|
||
```php | ||
OptionalField::forPossiblyMissingArrayKey( | ||
['foo' => 'bar'], | ||
'foo', | ||
\Psl\Type\positive_int() | ||
); // crashes: `foo` does not contain a `positive-int`! | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "joelbutcher/php-optional", | ||
"description": "An opinionated implementation of Java's Optional class for PHP.", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Joel Butcher", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.0.2", | ||
"vimeo/psalm": "^4.30", | ||
"azjezz/psl": "^1.9|^2.1" | ||
}, | ||
"require-dev": { | ||
"pestphp/pest": "^1.22", | ||
"phpstan/phpstan": "^1.9", | ||
"laravel/pint": "^1.2" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"JoelButcher\\PhpOptional\\": "src/", | ||
"JoelButcher\\PhpOptional\\Tests\\": "tests/" | ||
} | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"pestphp/pest-plugin": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
parameters: | ||
|
||
paths: | ||
- src/ | ||
|
||
level: 7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace JoelButcher\PhpOptional\Exceptions; | ||
|
||
class NoSuchElementException extends \RuntimeException | ||
{ | ||
// | ||
} |
Oops, something went wrong.