akaunting/app/Traits/Scopes.php

103 lines
2.6 KiB
PHP
Raw Normal View History

2020-12-26 16:13:34 +03:00
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
2020-12-26 16:13:34 +03:00
trait Scopes
{
2022-06-10 19:26:32 +03:00
public function applyNotRecurringScope(Builder $builder, Model $model): void
2020-12-26 16:13:34 +03:00
{
2022-06-10 19:26:32 +03:00
// Skip if type already set
if ($this->scopeColumnExists($builder, $model->getTable(), 'type')) {
2020-12-27 02:19:50 +03:00
return;
}
// Apply not recurring scope
$builder->isNotRecurring();
}
2022-06-10 19:26:32 +03:00
public function applyNotSplitScope(Builder $builder, Model $model): void
{
2022-06-10 19:26:32 +03:00
// Skip if type already set
if ($this->scopeColumnExists($builder, $model->getTable(), 'type')) {
2020-12-28 14:50:32 +03:00
return;
}
// Apply not split scope
$builder->isNotSplit();
2020-12-26 16:13:34 +03:00
}
2022-06-10 19:26:32 +03:00
public function scopeColumnExists(Builder $builder, string $table, string $column): bool
2020-12-26 16:13:34 +03:00
{
$query = $builder->getQuery();
foreach ((array) $query->wheres as $key => $where) {
if (empty($where) || empty($where['column'])) {
continue;
}
if (strstr($where['column'], '.')) {
$whr = explode('.', $where['column']);
2022-06-10 19:26:32 +03:00
$where['table'] = $whr[0];
2020-12-26 16:13:34 +03:00
$where['column'] = $whr[1];
}
2022-06-10 19:26:32 +03:00
if (! empty($where['table']) && ! empty($table) && ($where['table'] != $table)) {
continue;
}
2020-12-26 16:13:34 +03:00
if ($where['column'] != $column) {
continue;
}
return true;
}
return false;
}
2020-12-26 21:01:11 +03:00
2022-06-10 19:26:32 +03:00
public function scopeValueExists(Builder $builder, string $table, string $column, string $value): bool
2020-12-26 21:01:11 +03:00
{
$query = $builder->getQuery();
foreach ((array) $query->wheres as $key => $where) {
if (empty($where) || empty($where['column']) || empty($where['value'])) {
continue;
}
if (strstr($where['column'], '.')) {
$whr = explode('.', $where['column']);
2022-06-10 19:26:32 +03:00
$where['table'] = $whr[0];
$where['column'] = $whr[1];
}
2022-06-10 19:26:32 +03:00
if (! empty($where['table']) && ! empty($table) && ($where['table'] != $table)) {
continue;
}
if ($where['column'] != $column) {
continue;
}
2020-12-26 21:01:11 +03:00
if (! Str::endsWith($where['value'], $value)) {
continue;
}
return true;
}
return false;
2020-12-26 21:01:11 +03:00
}
// @deprecated version 3.0.0
2022-06-10 19:26:32 +03:00
public function scopeExists($builder, $column): bool
{
2022-06-10 19:26:32 +03:00
return $this->scopeColumnExists($builder, '', $column);
}
2020-12-26 16:13:34 +03:00
}