Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Dec 23, 2023
1 parent 1d1a7c1 commit afc8367
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/Widgets/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,50 +76,60 @@ public function resolveQuery(Request $request): Builder
/**
* Aggregate count values.
*/
public function count(Request $request, Builder $query, string $column = '*'): ValueResult
protected function count(Request $request, Builder $query, string $column = '*'): ValueResult
{
return $this->toResult(
$query->selectRaw(sprintf('count(%s) as `__value`', $query->getQuery()->getGrammar()->wrap($column)))
$this->aggregate($query, 'count', $column)
);
}

/**
* Aggregate average values.
*/
public function avg(Request $request, Builder $query, string $column): ValueResult
protected function avg(Request $request, Builder $query, string $column): ValueResult
{
return $this->toResult(
$query->selectRaw(sprintf('avg(%s) as `__value`', $query->getQuery()->getGrammar()->wrap($column)))
$this->aggregate($query, 'avg', $column)
);
}

/**
* Aggregate min values.
*/
public function min(Request $request, Builder $query, string $column): ValueResult
protected function min(Request $request, Builder $query, string $column): ValueResult
{
return $this->toResult(
$query->selectRaw(sprintf('min(%s) as `__value`', $query->getQuery()->getGrammar()->wrap($column)))
$this->aggregate($query, 'min', $column)
);
}

/**
* Aggregate max values.
*/
public function max(Request $request, Builder $query, string $column): ValueResult
protected function max(Request $request, Builder $query, string $column): ValueResult
{
return $this->toResult(
$query->selectRaw(sprintf('max(%s) as `__value`', $query->getQuery()->getGrammar()->wrap($column)))
$this->aggregate($query, 'max', $column)
);
}

/**
* Aggregate sum values.
*/
public function sum(Request $request, Builder $query, string $column): ValueResult
protected function sum(Request $request, Builder $query, string $column): ValueResult
{
return $this->toResult(
$query->selectRaw(sprintf('sum(%s) as `__value`', $query->getQuery()->getGrammar()->wrap($column)))
$this->aggregate($query, 'sum', $column)
);
}

/**
* Apply the aggregate function on the query.
*/
protected function aggregate(Builder $query, string $fn, string $column): Builder
{
return $query->selectRaw(
sprintf('%s(%s) as `__value`', $fn, $query->getQuery()->getGrammar()->wrap($column))
);
}

Expand Down

0 comments on commit afc8367

Please sign in to comment.