🆙 Add cms i using 🆙

This commit is contained in:
Remco
2025-11-25 22:42:56 +01:00
parent 94704e0925
commit d44196149e
35591 changed files with 3601123 additions and 0 deletions
@@ -0,0 +1,40 @@
<?php
$finder = Symfony\Component\Finder\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'class_attributes_separation' => [
'elements' => [
'method' => 'one',
],
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
@@ -0,0 +1,7 @@
# Changelog
All notable changes to `laravel-trend` will be documented in this file.
## 1.0.0 - 202X-XX-XX
- initial release
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Flowframe <lars@flowframe.nl>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,123 @@
# Laravel Trend
Generate trends for your models. Easily generate charts or reports.
## Why?
Most applications require charts or reports to be generated. Doing this over again, and again can be a painful process. That's why we've created a fluent Laravel package to solve this problem.
You can aggregate average, min, max, and totals per minute, hour, day, month, and year.
## Installation
You can install the package via composer:
```bash
composer require flowframe/laravel-trend
```
## Usage
To generate a trend for your model, import the `Flowframe\Trend\Trend` class and pass along a model or query.
Example:
```php
// Totals per month
$trend = Trend::model(User::class)
->between(
start: now()->startOfYear(),
end: now()->endOfYear(),
)
->perMonth()
->count();
// Average user weight where name starts with a over a span of 11 years, results are grouped per year
$trend = Trend::query(User::where('name', 'like', 'a%'))
->between(
start: now()->startOfYear()->subYears(10),
end: now()->endOfYear(),
)
->perYear()
->average('weight');
```
## Starting a trend
You must either start a trend using `::model()` or `::query()`. The difference between the two is that using `::query()` allows you to add additional filters, just like you're used to using eloquent. Using `::model()` will just consume it as it is.
```php
// Model
Trend::model(Order::class)
->between(...)
->perDay()
->count();
// More specific order query
Trend::query(
Order::query()
->hasBeenPaid()
->hasBeenShipped()
)
->between(...)
->perDay()
->count();
```
## Interval
You can use the following aggregates intervals:
- `perMinute()`
- `perHour()`
- `perDay()`
- `perMonth()`
- `perYear()`
## Aggregates
You can use the following aggregates:
- `sum('column')`
- `average('column')`
- `max('column')`
- `min('column')`
- `count('*')`
## Date Column
By default, laravel-trend assumes that the model on which the operation is being performed has a `created_at` date column. If your model uses a different column name for the date or you want to use a different one, you should specify it using the `dateColumn(string $column)` method.
Example:
```php
Trend::model(Order::class)
->dateColumn('custom_date_column')
->between(...)
->perDay()
->count();
```
This allows you to work with models that have custom date column names or when you want to analyze data based on a different date column.
## Drivers
We currently support four drivers:
- MySQL
- MariaDB
- SQLite
- PostgreSQL
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Lars Klopstra](https://github.com/flowframe)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
@@ -0,0 +1,61 @@
{
"name": "flowframe/laravel-trend",
"description": "Easily generate model trends",
"keywords": [
"Flowframe",
"laravel",
"laravel-trend"
],
"homepage": "https://github.com/flowframe/laravel-trend",
"license": "MIT",
"authors": [
{
"name": "Lars Klopstra",
"email": "lars@flowframe.nl",
"role": "Developer"
}
],
"require": {
"php": "^8.2",
"spatie/laravel-package-tools": "^1.4.3",
"illuminate/contracts": "^8.37|^9|^10.0|^11.0|^12.0"
},
"require-dev": {
"nunomaduro/collision": "^5.3|^6.1|^8.0",
"orchestra/testbench": "^6.15|^7.0|^8.0|^9.0|^10.0",
"pestphp/pest": "^1.18|^2.34|^3.7",
"pestphp/pest-plugin-laravel": "^1.1|^2.3|^3.1",
"spatie/laravel-ray": "^1.23",
"vimeo/psalm": "^4.8|^5.6|^6.5"
},
"autoload": {
"psr-4": {
"Flowframe\\Trend\\": "src",
"Flowframe\\Trend\\Database\\Factories\\": "database/factories"
}
},
"autoload-dev": {
"psr-4": {
"Flowframe\\Trend\\Tests\\": "tests"
}
},
"scripts": {
"test": "./vendor/bin/pest --no-coverage",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Flowframe\\Trend\\TrendServiceProvider"
],
"aliases": {
"Trend": "Flowframe\\Trend\\TrendFacade"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
@@ -0,0 +1,8 @@
<?php
namespace Flowframe\Trend\Adapters;
abstract class AbstractAdapter
{
abstract public function format(string $column, string $interval): string;
}
@@ -0,0 +1,23 @@
<?php
namespace Flowframe\Trend\Adapters;
use Error;
class MySqlAdapter extends AbstractAdapter
{
public function format(string $column, string $interval): string
{
$format = match ($interval) {
'minute' => '%Y-%m-%d %H:%i:00',
'hour' => '%Y-%m-%d %H:00',
'day' => '%Y-%m-%d',
'week' => '%Y-%u',
'month' => '%Y-%m',
'year' => '%Y',
default => throw new Error('Invalid interval.'),
};
return "date_format({$column}, '{$format}')";
}
}
@@ -0,0 +1,23 @@
<?php
namespace Flowframe\Trend\Adapters;
use Error;
class PgsqlAdapter extends AbstractAdapter
{
public function format(string $column, string $interval): string
{
$format = match ($interval) {
'minute' => 'YYYY-MM-DD HH24:MI:00',
'hour' => 'YYYY-MM-DD HH24:00:00',
'day' => 'YYYY-MM-DD',
'week' => 'IYYY-IW',
'month' => 'YYYY-MM',
'year' => 'YYYY',
default => throw new Error('Invalid interval.'),
};
return "to_char(\"{$column}\", '{$format}')";
}
}
@@ -0,0 +1,23 @@
<?php
namespace Flowframe\Trend\Adapters;
use Error;
class SqliteAdapter extends AbstractAdapter
{
public function format(string $column, string $interval): string
{
$format = match ($interval) {
'minute' => '%Y-%m-%d %H:%M:00',
'hour' => '%Y-%m-%d %H:00',
'day' => '%Y-%m-%d',
'week' => '%Y-%W',
'month' => '%Y-%m',
'year' => '%Y',
default => throw new Error('Invalid interval.'),
};
return "strftime('{$format}', {$column})";
}
}
@@ -0,0 +1,195 @@
<?php
namespace Flowframe\Trend;
use Carbon\CarbonInterface;
use Carbon\CarbonPeriod;
use Error;
use Flowframe\Trend\Adapters\MySqlAdapter;
use Flowframe\Trend\Adapters\PgsqlAdapter;
use Flowframe\Trend\Adapters\SqliteAdapter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class Trend
{
public string $interval;
public CarbonInterface $start;
public CarbonInterface $end;
public string $dateColumn = 'created_at';
public string $dateAlias = 'date';
public function __construct(public Builder $builder)
{
}
public static function query(Builder $builder): self
{
return new static($builder);
}
public static function model(string $model): self
{
return new static($model::query());
}
public function between($start, $end): self
{
$this->start = $start;
$this->end = $end;
return $this;
}
public function interval(string $interval): self
{
$this->interval = $interval;
return $this;
}
public function perMinute(): self
{
return $this->interval('minute');
}
public function perHour(): self
{
return $this->interval('hour');
}
public function perDay(): self
{
return $this->interval('day');
}
public function perWeek(): self
{
return $this->interval('week');
}
public function perMonth(): self
{
return $this->interval('month');
}
public function perYear(): self
{
return $this->interval('year');
}
public function dateColumn(string $column): self
{
$this->dateColumn = $column;
return $this;
}
public function dateAlias(string $alias): self
{
$this->dateAlias = $alias;
return $this;
}
public function aggregate(string $column, string $aggregate): Collection
{
$values = $this->builder
->toBase()
->selectRaw("
{$this->getSqlDate()} as {$this->dateAlias},
{$aggregate}({$column}) as aggregate
")
->whereBetween($this->dateColumn, [$this->start, $this->end])
->groupBy($this->dateAlias)
->orderBy($this->dateAlias)
->get();
return $this->mapValuesToDates($values);
}
public function average(string $column): Collection
{
return $this->aggregate($column, 'avg');
}
public function min(string $column): Collection
{
return $this->aggregate($column, 'min');
}
public function max(string $column): Collection
{
return $this->aggregate($column, 'max');
}
public function sum(string $column): Collection
{
return $this->aggregate($column, 'sum');
}
public function count(string $column = '*'): Collection
{
return $this->aggregate($column, 'count');
}
public function mapValuesToDates(Collection $values): Collection
{
$values = $values->map(fn ($value) => new TrendValue(
date: $value->{$this->dateAlias},
aggregate: $value->aggregate,
));
$placeholders = $this->getDatePeriod()->map(
fn (CarbonInterface $date) => new TrendValue(
date: $date->format($this->getCarbonDateFormat()),
aggregate: 0,
)
);
return $values
->merge($placeholders)
->unique('date')
->sort()
->flatten();
}
protected function getDatePeriod(): Collection
{
return collect(
CarbonPeriod::between(
$this->start,
$this->end,
)->interval("1 {$this->interval}")
);
}
protected function getSqlDate(): string
{
$adapter = match ($this->builder->getConnection()->getDriverName()) {
'mysql', 'mariadb' => new MySqlAdapter(),
'sqlite' => new SqliteAdapter(),
'pgsql' => new PgsqlAdapter(),
default => throw new Error('Unsupported database driver.'),
};
return $adapter->format($this->dateColumn, $this->interval);
}
protected function getCarbonDateFormat(): string
{
return match ($this->interval) {
'minute' => 'Y-m-d H:i:00',
'hour' => 'Y-m-d H:00',
'day' => 'Y-m-d',
'week' => 'Y-W',
'month' => 'Y-m',
'year' => 'Y',
default => throw new Error('Invalid interval.'),
};
}
}
@@ -0,0 +1,14 @@
<?php
namespace Flowframe\Trend;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
class TrendServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package->name('laravel-trend');
}
}
@@ -0,0 +1,12 @@
<?php
namespace Flowframe\Trend;
class TrendValue
{
public function __construct(
public string $date,
public mixed $aggregate,
) {
}
}