diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml index 87b12ab..ddd5897 100644 --- a/.github/workflows/default.yml +++ b/.github/workflows/default.yml @@ -15,10 +15,10 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Semantic Release - uses: cycjimmy/semantic-release-action@v2 + uses: cycjimmy/semantic-release-action@v3 id: semantic with: extra_plugins: | diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 11b0eda..0e2d277 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -12,14 +12,14 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Validate composer.json and composer.lock run: composer validate --strict - name: Cache Composer packages id: composer-cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: vendor key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 3babae1..86d6e14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,52 @@ +## [1.2.6](https://github.com/customd/hashed-search/compare/v1.2.5...v1.2.6) (2022-11-08) + + +### Bug Fixes + +* missing ; in contract ([a6ecf6a](https://github.com/customd/hashed-search/commit/a6ecf6ab16f9e03dce4e615ce166be8e334ac7d4)) + +## [1.2.5](https://github.com/customd/hashed-search/compare/v1.2.4...v1.2.5) (2022-10-13) + + +### Bug Fixes + +* add ability to forcebly hash a record ([4cdd650](https://github.com/customd/hashed-search/commit/4cdd65080f9332be1cefa9cf3470cee28f2da11a)) + +## [1.2.4](https://github.com/customd/hashed-search/compare/v1.2.3...v1.2.4) (2022-08-31) + + +### Bug Fixes + +* add or where search ([5ab83c6](https://github.com/customd/hashed-search/commit/5ab83c6e56a38aee1586ea6092998a759e23e402)) + +## [1.2.3](https://github.com/customd/hashed-search/compare/v1.2.2...v1.2.3) (2022-07-12) + + +### Bug Fixes + +* filter null values before creating hashes ([79169a6](https://github.com/customd/hashed-search/commit/79169a6490266cbd2e558c453cf1f5ce5d208b14)) + +## [1.2.2](https://github.com/customd/hashed-search/compare/v1.2.1...v1.2.2) (2022-07-12) + + +### Bug Fixes + +* nullable searchable values ([87d8062](https://github.com/customd/hashed-search/commit/87d8062d8012837196877b25356b91db00c7eaf5)) + +## [1.2.1](https://github.com/customd/hashed-search/compare/v1.2.0...v1.2.1) (2022-06-16) + + +### Bug Fixes + +* nullable create ([a1b6950](https://github.com/customd/hashed-search/commit/a1b69505db3dea0b722795515f6c5859f97c38e9)) + +# [1.2.0](https://github.com/customd/hashed-search/compare/v1.1.0...v1.2.0) (2022-05-31) + + +### Features + +* laravel 9 ([5569ddc](https://github.com/customd/hashed-search/commit/5569ddc55c148c632947960ee6744e0521c66905)) + # [1.1.0](https://github.com/customd/hashed-search/compare/v1.0.1...v1.1.0) (2021-05-04) diff --git a/composer.json b/composer.json index 7eb8c1b..0c0741a 100644 --- a/composer.json +++ b/composer.json @@ -14,13 +14,13 @@ } ], "require": { - "php": ">=7.2", + "php": "^8.0", "ext-intl": "*", - "illuminate/support": "^6.0|^7.0|^8.0" + "illuminate/support": "^8.0|^9.0|^9.0|^10.0" }, "require-dev": { - "orchestra/testbench": "^4.0|^5.0|^6.0", - "phpunit/phpunit": "^8.4|^9.0" + "orchestra/testbench": "^6.0|^7.0|^8.0", + "phpunit/phpunit": "^9.0|^10.0" }, "autoload": { "psr-4": { diff --git a/src/Contracts/HasSearchableHash.php b/src/Contracts/HasSearchableHash.php index 8360f7b..9ec8ce6 100644 --- a/src/Contracts/HasSearchableHash.php +++ b/src/Contracts/HasSearchableHash.php @@ -16,26 +16,35 @@ public static function bootHasSearchableHash(): void { //We need to setup hashes for this static::saved(function ($model) { - $model->generateSearchHashes()->each(function ($hash, $field) use ($model) { - ModelsHashedSearch::updateOrCreate([ - 'hash_field' => $field, - 'hash_id' => $model->getKey(), - 'hash_type' => $model->getMorphClass() - ], [ - 'hash' => $hash, - ]); - }); + $model->buildSearchHash(); }); } - protected function generateSearchHashes(): Collection + public function buildSearchHash(): void { - return collect($this->searchableHash ?? [])->filter()->mapWithKeys(function ($field) { - return [$field => HashedSearch::create($this->getAttribute($field))]; + $this->generateSearchHashes()->each(function ($hash, $field) { + ModelsHashedSearch::updateOrCreate([ + 'hash_field' => $field, + 'hash_id' => $this->getKey(), + 'hash_type' => $this->getMorphClass() + ], [ + 'hash' => $hash, + ]); }); } - public function scopeSearchHashedField(Builder $builder, string $field, string $clearText) + protected function generateSearchHashes(): Collection + { + return collect($this->searchableHash ?? []) + ->filter( + fn($field) => ! blank($this->getAttribute($field)) + ) + ->mapWithKeys( + fn ($field) => [$field => HashedSearch::create($this->getAttribute($field))] + ); + } + + public function scopeSearchHashedField(Builder $builder, string $field, string $clearText): void { $builder->whereHas('searchHashRelation', function (Builder $builder) use ($field, $clearText) { $builder->where('hash_field', $field); @@ -46,6 +55,17 @@ public function scopeSearchHashedField(Builder $builder, string $field, string $ }); } + public function scopeOrSearchHashedField(Builder $builder, string $field, string $clearText): void + { + $builder->orWhereHas('searchHashRelation', function (Builder $builder) use ($field, $clearText) { + $builder->where('hash_field', $field); + $builder->where( + 'hash', + HashedSearch::create($clearText) + ); + }); + } + public function searchHashRelation() { return $this->hasMany(ModelsHashedSearch::class, 'hash_id')->where('hash_type', $this->getMorphClass()); diff --git a/src/HashedSearch.php b/src/HashedSearch.php index bada3b8..b3b368d 100644 --- a/src/HashedSearch.php +++ b/src/HashedSearch.php @@ -28,7 +28,7 @@ public function __construct(array $config) } - public function create(string $value, string $saltModifier = ""): ?string + public function create(?string $value, string $saltModifier = ""): ?string { //Nothing to hash here! if (strlen($value) === 0 || $value === null) {