Backup #1168

ID1168
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Infrastructure/Persistence/OntologyRepository.php
Version2
Typ modified
Größe3.9 KB
Hashb32949f4f92ce803db6b4b27e6175a8e10dab532cfd03e75c5f9c9fa3909b541
Datum2025-12-25 10:28:53
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Infrastructure\Persistence;

// @responsibility: CRUD-Operationen für Ontologie-Klassen (Hierarchie + JSON-Properties)

use Domain\Repository\OntologyRepositoryInterface;
use PDO;

final class OntologyRepository implements OntologyRepositoryInterface
{
    private PDO $db;

    public function __construct()
    {
        $this->db = DatabaseFactory::content();
    }

    /**
     * {@inheritDoc}
     */
    public function findAll(): array
    {
        return $this->db->query(
            'SELECT oc.*,
                    COUNT(DISTINCT ec.entity_id) as entity_count,
                    (SELECT COUNT(*) FROM ontology_classes WHERE parent_class_id = oc.id) as subclass_count
             FROM ontology_classes oc
             LEFT JOIN entity_classifications ec ON oc.id = ec.ontology_class_id
             GROUP BY oc.id
             ORDER BY oc.name'
        )->fetchAll();
    }

    /**
     * {@inheritDoc}
     */
    public function getStats(): array
    {
        $result = $this->db->query(
            'SELECT
                (SELECT COUNT(*) FROM ontology_classes) as total_classes,
                (SELECT COUNT(*) FROM ontology_classes WHERE parent_class_id IS NULL) as root_classes,
                (SELECT COUNT(DISTINCT entity_id) FROM entity_classifications) as classified_entities'
        )->fetch();

        return $result !== false ? $result : [
            'total_classes' => 0,
            'root_classes' => 0,
            'classified_entities' => 0,
        ];
    }

    /**
     * {@inheritDoc}
     */
    public function find(int $id): ?array
    {
        $stmt = $this->db->prepare('SELECT * FROM ontology_classes WHERE id = :id');
        $stmt->execute(['id' => $id]);
        $result = $stmt->fetch();

        return $result === false ? null : $result;
    }

    /**
     * {@inheritDoc}
     */
    public function create(string $name, ?int $parentId = null, ?string $description = null, array $properties = []): int
    {
        $stmt = $this->db->prepare(
            'INSERT INTO ontology_classes (name, parent_class_id, description, properties, created_at)
             VALUES (:name, :parent, :description, :properties, NOW())'
        );
        $stmt->execute([
            'name' => $name,
            'parent' => $parentId,
            'description' => $description,
            'properties' => json_encode($properties),
        ]);

        return (int) $this->db->lastInsertId();
    }

    /**
     * {@inheritDoc}
     */
    public function update(int $id, string $name, ?int $parentId = null, ?string $description = null, array $properties = []): bool
    {
        $stmt = $this->db->prepare(
            'UPDATE ontology_classes SET name = :name, parent_class_id = :parent,
             description = :description, properties = :properties WHERE id = :id'
        );

        return $stmt->execute([
            'id' => $id,
            'name' => $name,
            'parent' => $parentId,
            'description' => $description,
            'properties' => json_encode($properties),
        ]);
    }

    /**
     * {@inheritDoc}
     */
    public function delete(int $id): bool
    {
        // Check for subclasses
        $stmt = $this->db->prepare('SELECT COUNT(*) FROM ontology_classes WHERE parent_class_id = :id');
        $stmt->execute(['id' => $id]);

        if ((int) $stmt->fetchColumn() > 0) {
            return false; // Has subclasses, cannot delete
        }

        // Delete entity classifications
        $stmt = $this->db->prepare('DELETE FROM entity_classifications WHERE ontology_class_id = :id');
        $stmt->execute(['id' => $id]);

        // Delete class
        $stmt = $this->db->prepare('DELETE FROM ontology_classes WHERE id = :id');

        return $stmt->execute(['id' => $id]);
    }

    /**
     * {@inheritDoc}
     */
    public function findForSelect(): array
    {
        return $this->db->query('SELECT id, name FROM ontology_classes ORDER BY name')->fetchAll();
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
1527 3 modified 3.9 KB 2025-12-26 07:01
1168 2 modified 3.9 KB 2025-12-25 10:28
1161 1 modified 4.0 KB 2025-12-25 10:28

← Zurück zur Übersicht