Skip to content

Commit

Permalink
feat: add cast
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Dec 21, 2021
1 parent 78bf6f1 commit 6743e69
Show file tree
Hide file tree
Showing 13 changed files with 440 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
@@ -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
11 changes: 11 additions & 0 deletions .gitattributes
@@ -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
20 changes: 20 additions & 0 deletions .github/workflows/release-please.yml
@@ -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 }}
37 changes: 37 additions & 0 deletions .github/workflows/static.yml
@@ -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
45 changes: 45 additions & 0 deletions .github/workflows/tests.yml
@@ -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
25 changes: 25 additions & 0 deletions .gitignore
@@ -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
33 changes: 33 additions & 0 deletions README.md
@@ -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)
34 changes: 34 additions & 0 deletions composer.json
@@ -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"
}
}
5 changes: 5 additions & 0 deletions phpstan.neon
@@ -0,0 +1,5 @@
parameters:
checkMissingIterableValueType: false
level: max
paths:
- src
19 changes: 19 additions & 0 deletions phpunit.xml
@@ -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>
83 changes: 83 additions & 0 deletions src/Alpha2Cast.php
@@ -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']];
}
}
28 changes: 28 additions & 0 deletions tests/AdvancedOrderFixture.php
@@ -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',
];
}

0 comments on commit 6743e69

Please sign in to comment.