first commit
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Package;
|
||||
use App\Models\Version;
|
||||
|
||||
class RepoFormatter
|
||||
{
|
||||
public static function packageListItem(Package $package, bool $allowPrerelease = false): array
|
||||
{
|
||||
$latest = $allowPrerelease
|
||||
? ($package->latestVersion ?: $package->latestStableVersion)
|
||||
: $package->latestStableVersion;
|
||||
|
||||
return [
|
||||
'type' => $package->type,
|
||||
'slug' => $package->slug,
|
||||
'name' => $package->name,
|
||||
'summary' => $package->summary,
|
||||
'author' => $package->author,
|
||||
'icon_url' => $package->icon_url,
|
||||
'latest_version' => $latest?->version ?: $package->latest_version,
|
||||
'download_count' => (int) $package->download_count,
|
||||
'is_featured' => (bool) $package->is_featured,
|
||||
'categories' => $package->categories->pluck('slug')->values()->all(),
|
||||
'compatibility' => $latest ? self::compatibility($latest) : [
|
||||
'typecho_min' => '',
|
||||
'typecho_max' => '',
|
||||
'php_min' => '',
|
||||
'php_max' => '',
|
||||
],
|
||||
'updated_at' => optional($package->updated_at)->toAtomString(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function packageDetail(Package $package, bool $includeDownloadMeta = true): array
|
||||
{
|
||||
return [
|
||||
'type' => $package->type,
|
||||
'slug' => $package->slug,
|
||||
'name' => $package->name,
|
||||
'summary' => $package->summary,
|
||||
'description' => $package->description,
|
||||
'author' => $package->author,
|
||||
'homepage' => $package->homepage,
|
||||
'icon_url' => $package->icon_url,
|
||||
'license' => $package->license,
|
||||
'is_featured' => (bool) $package->is_featured,
|
||||
'download_count' => (int) $package->download_count,
|
||||
'categories' => $package->categories->pluck('slug')->values()->all(),
|
||||
'screenshots' => $package->screenshots->map(function ($item) {
|
||||
return [
|
||||
'url' => $item->image_url,
|
||||
'caption' => $item->caption,
|
||||
];
|
||||
})->values()->all(),
|
||||
'versions' => $package->versions->map(function ($version) use ($includeDownloadMeta) {
|
||||
return self::versionDetail($version, $includeDownloadMeta);
|
||||
})->values()->all(),
|
||||
'created_at' => optional($package->created_at)->toAtomString(),
|
||||
'updated_at' => optional($package->updated_at)->toAtomString(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function versionDetail(Version $version, bool $includeDownloadMeta = true): array
|
||||
{
|
||||
$payload = [
|
||||
'version' => $version->version,
|
||||
'is_stable' => (bool) $version->is_stable,
|
||||
'is_latest' => (bool) $version->is_latest,
|
||||
'changelog' => $version->changelog,
|
||||
'compatibility' => self::compatibility($version),
|
||||
'published_at' => optional($version->published_at)->toAtomString(),
|
||||
];
|
||||
|
||||
if ($includeDownloadMeta) {
|
||||
$payload['package'] = [
|
||||
'size' => (int) $version->package_size,
|
||||
'sha256' => $version->sha256,
|
||||
'download_url' => $version->package_url,
|
||||
];
|
||||
}
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
public static function compatibility(Version $version): array
|
||||
{
|
||||
return [
|
||||
'typecho_min' => $version->typecho_min,
|
||||
'typecho_max' => $version->typecho_max,
|
||||
'php_min' => $version->php_min,
|
||||
'php_max' => $version->php_max,
|
||||
'php_extensions' => $version->php_extensions_array,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user