Skip to content

Commit 9e0e1d7

Browse files
committed
Initial version
0 parents  commit 9e0e1d7

32 files changed

+31692
-0
lines changed

.github/workflows/check.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions
2+
3+
name: "Check"
4+
5+
on:
6+
pull_request:
7+
push:
8+
branches:
9+
- "*"
10+
- "!gh-pages"
11+
12+
jobs:
13+
coding-standards:
14+
name: "Coding Standard"
15+
runs-on: "ubuntu-latest"
16+
17+
strategy:
18+
matrix:
19+
php-version:
20+
- "8.1"
21+
22+
steps:
23+
- name: "Checkout"
24+
uses: actions/checkout@v3
25+
26+
- name: "Install PHP"
27+
uses: "shivammathur/setup-php@v2"
28+
with:
29+
coverage: "none"
30+
php-version: "${{ matrix.php-version }}"
31+
32+
- name: "Validate Composer"
33+
run: "composer validate"
34+
35+
- name: "Install dependencies"
36+
run: "composer install --no-interaction --no-progress"
37+
38+
- name: "ECS"
39+
run: "composer lint:check"
40+
41+
- name: "Rector"
42+
run: "composer lint:upgrade:check"
43+
44+
tests:
45+
name: "Tests"
46+
runs-on: "ubuntu-latest"
47+
48+
strategy:
49+
fail-fast: false
50+
matrix:
51+
php-version:
52+
- "8.1"
53+
dependencies:
54+
- "lowest"
55+
- "highest"
56+
57+
steps:
58+
- name: "Checkout"
59+
uses: actions/checkout@v3
60+
61+
- name: "Install PHP"
62+
uses: "shivammathur/setup-php@v2"
63+
with:
64+
coverage: "none"
65+
php-version: "${{ matrix.php-version }}"
66+
67+
- name: "Install lowest dependencies"
68+
if: ${{ matrix.dependencies == 'lowest' }}
69+
run: "composer update --prefer-lowest --no-interaction --no-progress"
70+
71+
- name: "Install highest dependencies"
72+
if: ${{ matrix.dependencies == 'highest' }}
73+
run: "composer update --no-interaction --no-progress"
74+
75+
- name: "Tests"
76+
run: "composer test"
77+
78+
static-analysis:
79+
name: "PHPStan"
80+
runs-on: "ubuntu-latest"
81+
82+
strategy:
83+
fail-fast: false
84+
matrix:
85+
php-version:
86+
- "8.1"
87+
dependencies:
88+
- "lowest"
89+
- "highest"
90+
91+
steps:
92+
- name: "Checkout"
93+
uses: actions/checkout@v3
94+
95+
- name: "Install PHP"
96+
uses: "shivammathur/setup-php@v2"
97+
with:
98+
coverage: "none"
99+
php-version: "${{ matrix.php-version }}"
100+
extensions: mbstring
101+
tools: composer:v2
102+
103+
- name: "Install lowest dependencies"
104+
if: ${{ matrix.dependencies == 'lowest' }}
105+
run: "composer update --prefer-lowest --no-interaction --no-progress"
106+
107+
- name: "Install highest dependencies"
108+
if: ${{ matrix.dependencies == 'highest' }}
109+
run: "composer update --no-interaction --no-progress"
110+
111+
- name: "PHPStan"
112+
run: "composer lint:stan"

.github/workflows/deploy-docs.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Documentation
2+
on:
3+
push:
4+
branches:
5+
- main
6+
tags:
7+
- v**
8+
jobs:
9+
build-and-deploy:
10+
concurrency: docs-${{ github.ref }}
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout 🛎️
14+
uses: actions/checkout@v3
15+
16+
- name: Install and Build 🔧
17+
run: |
18+
cd docs
19+
npm ci
20+
npm run generate
21+
22+
- name: Deploy 🚀
23+
uses: JamesIves/github-pages-deploy-action@v4.3.3
24+
with:
25+
branch: gh-pages # The branch the action should deploy to.
26+
folder: docs/dist # The folder the action should deploy.
27+
clean: true

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/vendor
2+
.phpunit.result.cache
3+
composer.lock
4+
/docs/.nuxt
5+
/docs/dist
6+
/docs/node_modules
7+
/docs/static/sw.js

CONTRIBUTION.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# LaraStrict contribution
2+
3+
Feel free to contribute. Guide lines contributions:
4+
5+
## Bug fixes
6+
7+
For bug fixes create and issue and pull request if possible.
8+
9+
## Ideas
10+
11+
Use the discussion functionality and propose your idea:
12+
13+
- What you want to solve?
14+
- Sample proof of concept code? (just how it could look)
15+
16+
## Wait to take in account
17+
18+
- Always design your classes with dependency injection in mind (possibly constructor).
19+
- Always think about tests -> how they should be written and if it is easy.
20+
21+
## Lint and tests
22+
23+
```bash
24+
composer run check
25+
```
26+
27+
We are using set of tools to ensure that the code is consistent. Run this before pushing your code changes.
28+
29+
### [PHPStan](https://phpstan.org)
30+
31+
```bash
32+
composer run check
33+
```
34+
35+
### [Rector](https://github.com/rectorphp/rector)
36+
37+
```bash
38+
composer run check
39+
```
40+
41+
### [Easy coding standard](https://github.com/symplify/easy-coding-standard)
42+
43+
```bash
44+
composer run check
45+
```

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
> Get typed (strict mode) values from an Array / XML with basic validation.
2+
3+
![img](https://img.shields.io/badge/PHPStan-8-blue)
4+
![php](https://img.shields.io/badge/PHP-8.1-B0B3D6)
5+
6+
```bash
7+
composer require wrkflow/php-get-typed-value
8+
```
9+
10+
I've created this project as part of my mission to create `work flow` tools / libraries to make my (and yours) **dev
11+
life easier and more enjoyable**.
12+
13+
Want more tools or want to help? Check [wrk-flow.com](https://wrk-flow.com) or [CONTRIBUTE](CONTRIBUTION.md) (I need
14+
help with the documentation, new features, tests).
15+
16+
## Main features
17+
18+
- 🚀 Retrieve values from Array (JSON) / XML with correct return type
19+
- 🏆 Makes PHPStan / IDE happy due the return types
20+
- 🤹‍ Basic type without additional looping.
21+
22+
```php
23+
$data = new \Wrkflow\GetValue\GetValue(new \Wrkflow\GetValue\DataHolders\ArrayData([
24+
'page' => 1,
25+
'items' => [['name' => 'test', 'tags' => null, 'label' => 'yes'], ['name' => 'test 2', 'tags' => ['test']]]
26+
]));
27+
$page = $data->getRequiredInt('page'); // Will throw MissingValueForKeyException
28+
$items = $data->getRequiredArray('items');
29+
30+
foreach ($items as $item) {
31+
$itemData = new \Wrkflow\GetValue\GetValue(new \Wrkflow\GetValue\DataHolders\ArrayData($item));
32+
33+
$name = $itemData->getRequiredString('name');
34+
// getArray will ensure convert null (not present) values to empty array
35+
foreach ($itemData->getArray('tags') as $tag) {
36+
// $tag should be string, validate it
37+
}
38+
// Get label will return nullable string
39+
$label = $itemData->getString('label');
40+
}
41+
```
42+
43+
## Documentation
44+
45+
Documentation is hosted on [GitHub Pages](https://php-get-typed-value.wrk-flow.com).
46+

composer.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"name": "wrkflow/php-get-typed-value",
3+
"description": "Get typed (strict mode) values from an Array / XML with basic validation.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Martin Kluska",
9+
"email": "martin@kluska.cz"
10+
}
11+
],
12+
"require-dev": {
13+
"phpstan/phpstan": "^1.6.3",
14+
"phpstan/phpstan-deprecation-rules": "^1.0.0",
15+
"phpstan/phpstan-phpunit": "^1.1.1",
16+
"phpunit/phpunit": "^9.5.20",
17+
"rector/rector": "^0.12.22",
18+
"symplify/easy-coding-standard": "^10.2.2"
19+
},
20+
"scripts": {
21+
"check": "composer lint && composer test",
22+
"lint:check": "./vendor/bin/ecs",
23+
"lint:fix": "./vendor/bin/ecs --fix",
24+
"lint:stan": "./vendor/bin/phpstan",
25+
"lint:upgrade:check": "vendor/bin/rector process --dry-run",
26+
"lint:upgrade": "vendor/bin/rector process",
27+
"lint": "composer lint:fix && composer lint:upgrade && composer lint:stan",
28+
"test": "./vendor/bin/phpunit"
29+
},
30+
"suggest": {
31+
"wrkflow/larastrict": "Improve your Laravel code base with DDD and strict conventions."
32+
},
33+
"autoload": {
34+
"psr-4": {
35+
"Wrkflow\\GetValue\\": "src/"
36+
}
37+
},
38+
"autoload-dev": {
39+
"psr-4": {
40+
"Wrkflow\\GetValueTests\\": "tests/"
41+
}
42+
},
43+
"config": {
44+
"preferred-install": "dist",
45+
"sort-packages": true,
46+
"optimize-autoloader": true,
47+
"allow-plugins": {
48+
"symfony/thanks": false
49+
}
50+
},
51+
"archive": {
52+
"exclude": ["/docs"]
53+
},
54+
"funding": [
55+
{
56+
"type": "github",
57+
"url": "https://github.com/sponsors/pionl"
58+
}
59+
]
60+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: Custom data holder
3+
subtitle: 'Provide data access to a custom data format 🚀'
4+
category: Customization
5+
position: 11
6+
---
7+
8+
1. Create an object in DataHolders namespace
9+
2. Extend `\Wrkflow\GetValue\DataHolders`
10+
3. Implement `getValue(string $key): mixed` that should return required value (make it nullable).
11+
4. Implement `public function get(): array` that should return whole data when you need it.
12+
13+
```php
14+
<?php
15+
16+
declare(strict_types=1);
17+
18+
namespace Wrkflow\GetValue\DataHolders;
19+
20+
class ArrayData extends AbstractData
21+
{
22+
public function __construct(private readonly array $array)
23+
{
24+
}
25+
26+
public function getValue(string $key): mixed
27+
{
28+
return $this->array[$key] ?? null;
29+
}
30+
31+
public function get(): array
32+
{
33+
return $this->array;
34+
}
35+
}
36+
37+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: Custom exceptions
3+
subtitle: 'Provide your own exceptions 🚀'
4+
category: Customization
5+
position: 11
6+
---
7+
8+
In some cases you want to provide your own exceptions (like append custom data to the exception).
9+
10+
For this you need to implement your own `ExceptionBuilder` and provide it when creating `GetValue` instance.
11+
12+
```php
13+
class CustomExceptionBuilder extends
14+
{
15+
public function __construct(private readonly array $customLogContext) {}
16+
17+
public function missingValue(string $key): Exception
18+
{
19+
return new MissingValueForKeyException($key, $this->customLogContext);
20+
}
21+
22+
public function arrayIsEmpty(string $key): Exception
23+
{
24+
return new ArrayIsEmptyException($key, $this->customLogContext);
25+
}
26+
27+
public function notAnArray(string $key): Exception
28+
{
29+
return new NotAnArrayException($key, $this->customLogContext);
30+
}
31+
}
32+
```
33+
34+
Build your instance:
35+
36+
```php
37+
$data = new \Wrkflow\GetValue\GetValue(
38+
new \Wrkflow\GetValue\DataHolders\ArrayData([]),
39+
new CustomExceptionBuilder(['message' => 'parsing'])
40+
);
41+
```

0 commit comments

Comments
 (0)