Skip to content

Commit

Permalink
Merge pull request #79 from patinthehat/v2
Browse files Browse the repository at this point in the history
New Major Version: v2 - PHP 8-only Support, Object Casting
  • Loading branch information
freekmurze authored Apr 9, 2021
2 parents a44a3d1 + 22a935a commit b0ba90a
Show file tree
Hide file tree
Showing 21 changed files with 213 additions and 132 deletions.
6 changes: 3 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
Expand All @@ -13,3 +10,6 @@ trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
File renamed without changes.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github: spatie
custom: https://spatie.be/open-source/support-us
11 changes: 11 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/vendor_slug/package_slug/discussions/new?category=q-a
about: Ask the community for help
- name: Request a feature
url: https://github.com/vendor_slug/package_slug/discussions/new?category=ideas
about: Share ideas for new features
- name: Report a bug
url: https://github.com/vendor_slug/package_slug/issues/new
about: Report a reproducable bug
3 changes: 3 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
2 changes: 1 addition & 1 deletion .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Fix style
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php_cs --allow-risky=yes
args: --config=.php_cs.dist --allow-risky=yes

- name: Extract branch name
shell: bash
Expand Down
17 changes: 8 additions & 9 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.3, 7.4, 8.0]
laravel: [6.*, 7.*, 8.*]
php: [8.0]
laravel: [8.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*
- laravel: 6.*
testbench: 4.*
testbench: '^6.15'

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

services:
mysql:
image: mysql:5.7
image: mysql:8.0
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: laravel_tags
MYSQL_DATABASE: laravel_schemaless_attributes
MYSQL_ROOT_PASSWORD: root_password
MYSQL_USER: username
MYSQL_PASSWORD: password
ports:
- 3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
Expand Down
13 changes: 10 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
.idea
.php_cs
.php_cs.cache
.phpunit.result.cache
build
composer.lock
coverage
docs
phpunit.xml
psalm.xml
testbench.yaml
vendor
coverage
.env
.php_cs.cache
node_modules
.env
7 changes: 4 additions & 3 deletions .php_cs → .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$finder = Symfony\Component\Finder\Finder::create()
->notPath('bootstrap/*')
->notPath('storage/*')
->notPath('vendor')
->notPath('resources/view/mail/*')
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
Expand Down Expand Up @@ -31,12 +31,13 @@ return PhpCsFixer\Config::create()
'phpdoc_var_without_name' => true,
'class_attributes_separation' => [
'elements' => [
'method', 'property',
'method',
],
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
]
],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to `laravel-schemaless-attributes` will be documented in this file

## 2.0.0 - 2021-04-09

- require PHP 8+
- use PHP 8 syntax where possible
- drop support for PHP 7
- drop support for Laravel 6
- implement `spatie/laravel-package-tools`
- implement custom class attribute casting

## 1.8.3 - 2020-11-04

- allow PHP 8.0
Expand Down
45 changes: 21 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $yourModel->extra_attributes->get('rey.side'); // returns 'light'
// retrieve default value when attribute is not exists
$yourModel->extra_attributes->get('non_existing', 'default'); // returns 'default'

// it has a scope to retrieve all models with the given schemaless attributes
// it has a modelScope to retrieve all models with the given schemaless attributes
$yourModel->withSchemalessAttributes(['name' => 'value', 'name2' => 'value2'])->get();

// delete key & value
Expand All @@ -56,6 +56,8 @@ This package requires a database with support for `json` columns like MySQL 5.7

## Installation

> For Laravel versions 6 & 7 or PHP 7, use version 1.x of this package.
You can install the package via composer:

```bash
Expand All @@ -76,29 +78,24 @@ Schema::table('your_models', function (Blueprint $table) {

### Preparing the model

In order to work with the schemaless attributes you'll need to add a cast, an accessor and a scopes on your model. Here's an example of what you need to add if you've chosen `extra_attributes` as your column name.
In order to work with the schemaless attributes you'll need to add a custom cast and a scope on your model. Here's an example of what you need to add if you've chosen `extra_attributes` as your column name.

```php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Spatie\SchemalessAttributes\SchemalessAttributes;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;

class TestModel extends Model
{
// ...

public $casts = [
'extra_attributes' => 'array',
'extra_attributes' => SchemalessAttributes::class,
];

public function getExtraAttributesAttribute(): SchemalessAttributes
{
return SchemalessAttributes::createForModel($this, 'extra_attributes');
}

public function scopeWithExtraAttributes(): Builder
{
return SchemalessAttributes::scopeWithSchemalessAttributes('extra_attributes');
return $this->extra_attributes->modelCast();
}

// ...
Expand Down Expand Up @@ -129,12 +126,12 @@ class TestModel extends Model

public function scopeWithExtraAttributes(): Builder
{
return SchemalessAttributes::scopeWithSchemalessAttributes('extra_attributes');
return $this->extra_attributes->modelScope();
}

public function scopeWithOtherExtraAttributes(): Builder
{
return SchemalessAttributes::scopeWithSchemalessAttributes('other_extra_attributes');
return $this->other_extra_attributes->modelScope();
}

// ...
Expand All @@ -149,18 +146,18 @@ namespace App\Models\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Spatie\SchemalessAttributes\SchemalessAttributes;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;

trait HasSchemalessAttributes
{
public function getExtraAttributesAttribute(): SchemalessAttributes
public function initializeHasSchemalessAttributesTrait()
{
return SchemalessAttributes::createForModel($this, 'extra_attributes');
$this->casts['extra_attributes'] = SchemalessAttributes::class;
}

public function scopeWithExtraAttributes(): Builder
{
return SchemalessAttributes::scopeWithSchemalessAttributes('extra_attributes');
return $this->extra_attributes->modelScope();
}
}
```
Expand Down Expand Up @@ -221,14 +218,14 @@ $yourModel->save(); // Persists both normal and schemaless attributes

### Retrieving models with specific schemaless attributes

Here's how you can use the provided scope.
Here's how you can use the provided modelScope.

```php
// Returns all models that have all the given schemaless attributes
$yourModel->withExtraAttributes(['name' => 'value', 'name2' => 'value2'])->get();
```

If you only want to search on a single custom attribute, you can use the scope like this
If you only want to search on a single custom attribute, you can use the modelScope like this

```php
// returns all models that have a schemaless attribute `name` set to `value`
Expand All @@ -239,21 +236,21 @@ $yourModel->withExtraAttributes('name', 'value')->get();

First create a MySQL database named `laravel_schemaless_attributes`. After that you can run the tests with:

``` bash
```bash
composer test
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

## Security
## Security Vulnerabilities

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

## Credits

Expand Down
16 changes: 10 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
}
],
"require": {
"php": "^7.3|^8.0",
"illuminate/database": "^6.0|^7.0|^8.0",
"illuminate/support": "^6.0|^7.0|^8.0"
"php": "^8.0",
"illuminate/database": "^7.0|^8.0",
"illuminate/support": "^7.0|^8.0",
"spatie/laravel-package-tools": "^1.4.3",
"illuminate/contracts": "^7.0|^8.0"
},
"require-dev": {
"mockery/mockery": "^1.4",
"orchestra/testbench": "^4.0|^5.0|^6.0",
"phpunit/phpunit": "^9.4"
"orchestra/testbench": "^6.15",
"phpunit/phpunit": "^9.5.4",
"brianium/paratest": "^6.2",
"nunomaduro/collision": "^5.3"
},
"autoload": {
"psr-4": {
Expand All @@ -36,7 +40,7 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test": "./vendor/bin/testbench package:test --parallel --no-coverage",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
},
Expand Down
63 changes: 45 additions & 18 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Spatie Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_USERNAME" value="root"/>
<env name="DB_DATABASE" value="laravel_tags"/>
<env name="DB_HOST" value="127.0.0.1"/>
<env name="DB_PORT" value="3306"/>
</php>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
>
<testsuites>
<testsuite name="VendorName Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_USERNAME" value="username"/>
<env name="DB_PASSWORD" value="password"/>
<env name="DB_DATABASE" value="laravel_schemaless_attributes"/>
<env name="DB_HOST" value="127.0.0.1"/>
<env name="DB_PORT" value="3306"/>
</php>
</phpunit>
Loading

0 comments on commit b0ba90a

Please sign in to comment.