Skip to content

Commit

Permalink
Add new exception
Browse files Browse the repository at this point in the history
Add new period method
  • Loading branch information
eliseekn committed Aug 22, 2023
1 parent 5d03106 commit 106478b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ LaravelMetrics::query(
->byDay($count = 0)
->byMonth($count = 0)
->byYear($count = 0)
->by($period, $count = 0)
->between($startDate, $endDate)
```

```php
$count = 0 => for every day, month or year
$count = 1 => for the current day, month or year
$count > 1 => for an interval of day, month or year from the $count value to now
$period = 'day', 'month' or 'year'
```

### Types of aggregates
Expand Down
16 changes: 16 additions & 0 deletions src/Exceptions/InvalidPeriodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace Eliseekn\LaravelMetrics\Exceptions;

use Exception;

/**
* This exception occurs when date format is invalid
*/
class InvalidPeriodException extends Exception
{
public function __construct() {
parent::__construct('Invalid period value. Valid period is day, month or year');
}
}
14 changes: 14 additions & 0 deletions src/LaravelMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Carbon\Carbon;
use DateTime;
use Eliseekn\LaravelMetrics\Exceptions\InvalidDateFormatException;
use Eliseekn\LaravelMetrics\Exceptions\InvalidPeriodException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -98,6 +99,19 @@ public function byYear(int $count = 0): self
return $this;
}

public function by(string $period, int $count = 0): self
{
$period = strtolower($period);

if (!in_array($period, [self::DAY, self::MONTH, self::YEAR])) {
throw new InvalidPeriodException();
}

$this->period = $period;
$this->count = $count;
return $this;
}

public function between(string $start, string $end): self
{
$this->checkDateFormat([$start, $end]);
Expand Down

0 comments on commit 106478b

Please sign in to comment.