Files
mystuff_backend/api/models/Adult.php
Lars Behrends e38a6e1f7b Add strict types and type hints across API
Apply strict_types and extensive type declarations throughout the API and models, improving type safety and error handling. Key changes: add declare(strict_types=1) to many files; convert properties, method parameters and return values to typed signatures (PDO, arrays, ints, strings, bools, nullables); switch exception handling to Throwable in index and Router; improve Router, controllers and model method signatures and nullability handling; refine file/image serving security checks and headers in ImageController; strengthen Database typing and initialization methods; return explicit types from BaseModel CRUD helpers and counting; update Media/Cast/Adult/Game/Console/Settings controllers and models to use typed methods, better validation, and clearer update/create return types. Also add AGENTS.md (agent skills index), update README with Swagger/OpenAPI usage instructions, and add /.windsurf to .gitignore. These changes aim to harden runtime correctness, make intended contracts explicit, and prepare the codebase for easier maintenance and static analysis.
2026-04-16 16:40:31 +02:00

89 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/MediaType.php';
require_once __DIR__ . '/../services/ImageHandler.php';
class Adult extends MediaType {
private ImageHandler $imageHandler;
private bool $isUpdate = false;
private ?int $mediaId = null;
public function __construct(PDO $pdo) {
parent::__construct($pdo);
$this->imageHandler = new ImageHandler();
}
protected function getType(): string {
return 'Adult';
}
protected function getTypeSpecificFields(): array {
return [];
}
protected function validateTypeSpecificFields(array $data): array {
// Adult spezifische Validierung
// Eventuell Altersverifikation etc.
return $data;
}
protected function processPosterField(array $data): array {
error_log("Adult::processPosterField - Checking for poster field, isUpdate: " . ($this->isUpdate ? 'yes' : 'no'));
if ($this->isUpdate && $this->mediaId && isset($data['poster']) && !empty($data['poster'])) {
$currentMedia = $this->findById($this->mediaId);
if ($currentMedia && !empty($currentMedia['poster'])) {
$oldPoster = $currentMedia['poster'];
if (strpos($oldPoster, '/images/') === 0) {
error_log("Adult::processPosterField - Deleting old poster: " . $oldPoster);
$this->imageHandler->deleteImage($oldPoster);
}
}
}
if (isset($data['poster']) && !empty($data['poster'])) {
error_log("Adult::processPosterField - Poster found, length: " . strlen($data['poster']));
if (strpos($data['poster'], '/images/') === 0 || filter_var($data['poster'], FILTER_VALIDATE_URL)) {
error_log("Adult::processPosterField - Poster is already a path or URL, skipping processing");
return $data;
}
$posterPath = $this->imageHandler->saveBase64Image($data['poster'], 'adult/poster');
error_log("Adult::processPosterField - ImageHandler returned: " . ($posterPath ?: 'null'));
if ($posterPath) {
$data['poster'] = $posterPath;
error_log("Adult::processPosterField - Poster path set to: " . $posterPath);
} else {
error_log("Adult::processPosterField - Failed to process poster, keeping original data");
}
} else {
error_log("Adult::processPosterField - No poster field found or empty");
}
return $data;
}
public function createWithRelations(array $data): int {
$data['type'] = 'Adult';
$this->validateTypeSpecificFields($data);
$data = $this->processPosterField($data);
return parent::createWithRelations($data);
}
public function updateWithRelations(int $id, array $data): bool {
$this->isUpdate = true;
$this->mediaId = $id;
$this->validateTypeSpecificFields($data);
$data = $this->processPosterField($data);
return parent::updateWithRelations($id, $data);
}
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
// Nur Adult Content suchen
$filters['type'] = 'Adult';
return parent::search($filters, $page, $limit);
}
}