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
1 parent
78bf6f1
commit 6743e69
Showing
13 changed files
with
440 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,15 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.{yml,yaml}] | ||
indent_size = 2 |
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,11 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto | ||
|
||
.gitattributes export-ignore | ||
.github export-ignore | ||
.gitignore export-ignore | ||
.github/ export-ignore | ||
tests/ export-ignore | ||
phpunit.xml export-ignore | ||
phpstan.neon export-ignore | ||
psalm.xml 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,20 @@ | ||
name: release-please | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
update_release_draft: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
|
||
steps: | ||
- uses: google-github-actions/release-please-action@v2 | ||
with: | ||
release-type: php | ||
bump-minor-pre-major: true | ||
|
||
- uses: actions/checkout@v2 | ||
if: ${{ steps.release.outputs.release_created }} |
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,37 @@ | ||
name: static analysis | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
phpstan: | ||
name: PHPStan | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
strategy: | ||
matrix: | ||
php-version: [8.0] | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: "${{ matrix.php-version }}" | ||
coverage: none | ||
extensions: mbstring | ||
tools: composer | ||
|
||
- name: Download dependencies | ||
run: composer update --no-interaction --no-progress | ||
|
||
- name: Download PHPStan | ||
run: composer bin phpstan require phpstan/phpstan | ||
|
||
- name: Execute PHPStan | ||
run: vendor/bin/phpstan analyze --no-progress |
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,45 @@ | ||
name: tests | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
laravel-tests: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
strategy: | ||
matrix: | ||
php-version: [8.0] | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Install PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: "${{ matrix.php-version }}" | ||
coverage: "pcov" | ||
|
||
- name: Install PHP dependencies | ||
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist | ||
|
||
- name: Run PHPUnit | ||
run: vendor/bin/phpunit --coverage-clover=coverage.xml | ||
|
||
- name: Upload coverage | ||
uses: codecov/codecov-action@v2 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
files: ./coverage.xml | ||
|
||
- name: Upload coverage to codeclimate | ||
uses: paambaati/codeclimate-action@v3.0.0 | ||
env: | ||
CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE_TOKEN }} | ||
with: | ||
coverageCommand: '' | ||
coverageLocations: ./coverage.xml:clover |
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,25 @@ | ||
/vendor/ | ||
/vendor-bin/ | ||
node_modules/ | ||
npm-debug.log | ||
yarn-error.log | ||
|
||
# Laravel 4 specific | ||
bootstrap/compiled.php | ||
app/storage/ | ||
|
||
# Laravel 5 & Lumen specific | ||
public/storage | ||
public/hot | ||
|
||
# Laravel 5 & Lumen specific with changed public path | ||
public_html/storage | ||
public_html/hot | ||
|
||
storage/*.key | ||
.env | ||
Homestead.yaml | ||
Homestead.json | ||
/.vagrant | ||
.phpunit.result.cache | ||
composer.lock |
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 @@ | ||
# country-casts-laravel | ||
|
||
[![Latest Stable Version](https://img.shields.io/github/v/release/brokeyourbike/country-casts-laravel)](https://github.com/brokeyourbike/country-casts-laravel/releases) | ||
[![Total Downloads](https://poser.pugx.org/brokeyourbike/country-casts-laravel/downloads)](https://packagist.org/packages/brokeyourbike/country-casts-laravel) | ||
[![License: MPL-2.0](https://img.shields.io/badge/license-MPL--2.0-purple.svg)](https://github.com/brokeyourbike/country-casts-laravel/blob/main/LICENSE) | ||
|
||
[![tests](https://github.com/brokeyourbike/country-casts-laravel/actions/workflows/tests.yml/badge.svg)](https://github.com/brokeyourbike/country-casts-laravel/actions/workflows/tests.yml) | ||
|
||
Cast country code attributes from ISO3 to ISO2 | ||
|
||
## Installation | ||
|
||
```bash | ||
composer require brokeyourbike/country-casts-laravel | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
use Illuminate\Database\Eloquent\Model; | ||
use BrokeYourBike\CountryCasts\Alpha2Cast; | ||
|
||
class Order extends Model | ||
{ | ||
protected $casts = [ | ||
'country_code' => 'string', | ||
'country_code_alpha2' => Alpha2Cast::class . ':country_code', | ||
]; | ||
} | ||
``` | ||
|
||
## License | ||
[Mozilla Public License v2.0](https://github.com/brokeyourbike/country-casts-laravel/blob/main/LICENSE) |
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,34 @@ | ||
{ | ||
"name": "brokeyourbike/country-casts-laravel", | ||
"description": "Cast Laravel country code attributes from ISO3 to ISO2", | ||
"type": "library", | ||
"license": "MPL-2.0", | ||
"autoload": { | ||
"psr-4": { | ||
"BrokeYourBike\\CountryCasts\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"BrokeYourBike\\CountryCasts\\Tests\\": "tests/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Ivan Stasiuk", | ||
"email": "ivan@stasi.uk", | ||
"homepage": "https://stasi.uk" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": "^8.0", | ||
"illuminate/contracts": "^8", | ||
"league/iso3166": "^4.0" | ||
}, | ||
"require-dev": { | ||
"bamarni/composer-bin-plugin": "^1.4", | ||
"phpunit/phpunit": "^9.5", | ||
"illuminate/database": "^8" | ||
} | ||
} |
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 @@ | ||
parameters: | ||
checkMissingIterableValueType: false | ||
level: max | ||
paths: | ||
- src |
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,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<phpunit | ||
colors="true" | ||
beStrictAboutOutputDuringTests="true" | ||
beStrictAboutTestsThatDoNotTestAnything="true" | ||
bootstrap="vendor/autoload.php" | ||
> | ||
<testsuites> | ||
<testsuite name="HTTP Client Test Suite"> | ||
<directory suffix=".php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<coverage> | ||
<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,83 @@ | ||
<?php | ||
|
||
// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
namespace BrokeYourBike\CountryCasts; | ||
|
||
use League\ISO3166\ISO3166; | ||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
|
||
/** | ||
* @author Ivan Stasiuk <brokeyourbike@gmail.com> | ||
*/ | ||
class Alpha2Cast implements CastsAttributes | ||
{ | ||
/** | ||
* Name of the source attribute. | ||
* | ||
* @var string | ||
*/ | ||
protected string $sourceAttributeName; | ||
|
||
/** | ||
* Create a new cast class instance. | ||
* | ||
* @param string $sourceAttributeName | ||
* @return void | ||
*/ | ||
public function __construct(string $sourceAttributeName) | ||
{ | ||
$this->sourceAttributeName = $sourceAttributeName; | ||
} | ||
|
||
/** | ||
* Transform the attribute from the underlying model values. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @param string $key | ||
* @param mixed $value | ||
* @param array $attributes | ||
* @return string | ||
*/ | ||
public function get($model, string $key, $value, array $attributes) | ||
{ | ||
$alpha3 = isset($attributes[$this->sourceAttributeName]) | ||
? $attributes[$this->sourceAttributeName] | ||
: null; | ||
|
||
if (!is_string($alpha3)) { | ||
throw new \InvalidArgumentException('The stored value should be sting'); | ||
} | ||
|
||
$data = (new ISO3166)->alpha3($alpha3); | ||
|
||
if (!is_string($data['alpha2'])) { | ||
throw new \LogicException('Obtained alpha2 value is not a string'); | ||
} | ||
|
||
return $data['alpha2']; | ||
} | ||
|
||
/** | ||
* Transform the attribute to its underlying model values. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @param string $key | ||
* @param mixed $value | ||
* @param array $attributes | ||
* @return mixed | ||
*/ | ||
public function set($model, string $key, $value, array $attributes) | ||
{ | ||
if (!is_string($value)) { | ||
throw new \InvalidArgumentException('The given value is not a string'); | ||
} | ||
|
||
$data = (new ISO3166)->alpha2($value); | ||
return [$this->sourceAttributeName => $data['alpha3']]; | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
namespace BrokeYourBike\CountryCasts\Tests; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use BrokeYourBike\CountryCasts\Alpha2Cast; | ||
|
||
/** | ||
* @author Ivan Stasiuk <brokeyourbike@gmail.com> | ||
*/ | ||
class AdvancedOrderFixture extends Model | ||
{ | ||
/** | ||
* The attributes that should be cast to native types. | ||
* | ||
* @var array<mixed> | ||
*/ | ||
protected $casts = [ | ||
'country_code' => 'string', | ||
'country_code_alpha2' => Alpha2Cast::class . ':country_code', | ||
]; | ||
} |
Oops, something went wrong.