Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 3 additions & 68 deletions lib/Controller/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCA\OpenRegister\Controller\Support\ControllerSession;
use OCA\OpenRegister\Controller\Support\ObjectGovernanceServices;
use OCA\OpenRegister\Controller\Support\LinkedDataServices;
use OCA\OpenRegister\Controller\Support\RelationUrlStamper;
use OCA\OpenRegister\Controller\Support\GeoServices;
use OCA\OpenRegister\Controller\Support\ObjectsControllerServices;
use OCA\OpenRegister\Controller\Trait\ResolvesRegisterAndSchemaTrait;
Expand Down Expand Up @@ -2127,7 +2128,7 @@ private function indexFromObjectService(array $listing, ObjectOperationsInterfac
* Batched object-count endpoint — POST /api/objects/counts.
*
* Accepts a JSON body of the shape
* `{ "counts": [ { "register": <id|slug>, "schema": <id|slug>, "filter": <object?> } ] }`
* `{ "counts": [ { "register": <id|slug>, "schema": <id|slug>, "filter": <object|null> } ] }`
* and returns `{ "results": [ { "register": ..., "schema": ..., "count": <int|null> } ] }`
* with exactly one result per input entry, in the same order as the
* request array. Identical `(register, schema, filter)` triples are
Expand Down Expand Up @@ -4893,73 +4894,7 @@ public function contracts(string $id, string $register, string $schema, ObjectOp
* @return array The same envelope with `url` stamped on each resolvable record.
*/
private function stampObjectUrls(array $result): array {
if ($this->linkedData->deepLinkRegistry === null
|| isset($result['results']) === false
|| is_array($result['results']) === false
) {
return $result;
}

foreach ($result['results'] as $index => $record) {
// Normalise entities to their serialized array form.
if (is_object($record) === true && method_exists($record, 'jsonSerialize') === true) {
$record = $record->jsonSerialize();
}

if (is_array($record) === false) {
continue;
}

$self = ($record['@self'] ?? []);
$uuid = ($self['id'] ?? ($record['id'] ?? null));
$registerId = ($self['register'] ?? null);
$schemaId = ($self['schema'] ?? null);

if ($uuid === null || is_numeric($registerId) === false || is_numeric($schemaId) === false) {
$result['results'][$index] = $record;
continue;
}

try {
$flat = array_merge(
$record,
[
'uuid' => $uuid,
'id' => $uuid,
'register' => $registerId,
'schema' => $schemaId,
]
);

$url = $this->linkedData->deepLinkRegistry->resolveUrl(
registerId: (int)$registerId,
schemaId: (int)$schemaId,
objectData: $flat
);

if ($url === null && $this->linkedData->relationUrlGenerator !== null) {
$url = $this->linkedData->relationUrlGenerator->linkToRoute(
'openregister.objects.show',
[
'register' => $registerId,
'schema' => $schemaId,
'id' => $uuid,
]
);
}

if ($url !== null) {
$record['url'] = $url;
}
} catch (\Throwable $e) {
// Defensive: never let URL resolution break the relation response.
$this->logger->debug('Relation URL resolution failed: ' . $e->getMessage());
}//end try

$result['results'][$index] = $record;
}//end foreach

return $result;
return (new RelationUrlStamper(linkedData: $this->linkedData, logger: $this->logger))->stamped(result: $result);
}//end stampObjectUrls()

/**
Expand Down
171 changes: 171 additions & 0 deletions lib/Controller/Support/RelationUrlStamper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

/**
* OpenRegister relation url stamper
*
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V.
*
* @category Controller
* @package OCA\OpenRegister\Controller\Support
*
* @author Conduction Development Team <info@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* @version GIT: <git_id>
*
* @link https://www.OpenRegister.nl
*/

declare(strict_types=1);

namespace OCA\OpenRegister\Controller\Support;

use Psr\Log\LoggerInterface;

/**
* Stamps the url an object opens at on each record of a relation envelope.
*
* Asks the deep link registry, the resolver the unified-search provider uses,
* and falls back to OpenRegister's own object route. A failure omits the url
* of that record and leaves the record itself as it is.
*
* @spec openspec/specs/object-interactions/spec.md
*/
final class RelationUrlStamper {

/**
* Constructor.
*
* @param LinkedDataServices $linkedData The deep link registry and url generator.
* @param LoggerInterface $logger The logger.
*
* @return void
*/
public function __construct(
private readonly LinkedDataServices $linkedData,
private readonly LoggerInterface $logger,
) {
}//end __construct()

/**
* The envelope with a url on each record that has one.
*
* @param array $result The relation envelope ({results, total, ...}).
*
* @return array The envelope, its serializable records as arrays.
*
* @spec openspec/specs/object-interactions/spec.md
*/
public function stamped(array $result): array {
if ($this->linkedData->deepLinkRegistry === null || is_array($result['results'] ?? null) === false) {
return $result;
}

foreach ($result['results'] as $index => $record) {
$row = $this->rowOf(record: $record);
if ($row !== null) {
$result['results'][$index] = $this->stampedRow(row: $row);
}
}

return $result;
}//end stamped()

/**
* A record as an array.
*
* @param mixed $record The record: an array, or an entity that serializes.
*
* @return array|null The row, or null for anything else.
*/
private function rowOf(mixed $record): ?array {
if (is_object($record) === true && method_exists($record, 'jsonSerialize') === true) {
$record = $record->jsonSerialize();
}

if (is_array($record) === true) {
return $record;
}

return null;
}//end rowOf()

/**
* A row with its url, when it identifies an object and a url resolves.
*
* @param array $row The row.
*
* @return array The row.
*/
private function stampedRow(array $row): array {
$identity = $this->identityOf(row: $row);
if ($identity === null) {
return $row;
}

try {
$url = $this->urlOf(row: $row, identity: $identity);
if ($url !== null) {
$row['url'] = $url;
}
} catch (\Throwable $e) {
$this->logger->debug('Relation URL resolution failed: '.$e->getMessage());
}

return $row;
}//end stampedRow()

/**
* The uuid, register and schema of a row.
*
* @param array $row The row.
*
* @return array{uuid: mixed, id: mixed, register: mixed, schema: mixed}|null Null when one is missing.
*/
private function identityOf(array $row): ?array {
$self = ($row['@self'] ?? []);
$uuid = ($self['id'] ?? ($row['id'] ?? null));
$register = ($self['register'] ?? null);
$schema = ($self['schema'] ?? null);

if ($uuid === null || is_numeric($register) === false || is_numeric($schema) === false) {
return null;
}

return ['uuid' => $uuid, 'id' => $uuid, 'register' => $register, 'schema' => $schema];
}//end identityOf()

/**
* The url of a row: the registered deep link, or else the object route.
*
* @param array $row The row.
* @param array{uuid: mixed, id: mixed, register: mixed, schema: mixed} $identity The row's identity.
*
* @return string|null The url.
*/
private function urlOf(array $row, array $identity): ?string {
$url = $this->linkedData->deepLinkRegistry->resolveUrl(
registerId: (int)$identity['register'],
schemaId: (int)$identity['schema'],
objectData: array_merge($row, $identity)
);

return ($url ?? $this->routeUrl(identity: $identity));
}//end urlOf()

/**
* The url of OpenRegister's own object route.
*
* @param array{uuid: mixed, id: mixed, register: mixed, schema: mixed} $identity The row's identity.
*
* @return string|null The url, or null without a url generator.
*/
private function routeUrl(array $identity): ?string {
return $this->linkedData->relationUrlGenerator?->linkToRoute(
'openregister.objects.show',
['register' => $identity['register'], 'schema' => $identity['schema'], 'id' => $identity['uuid']]
);
}//end routeUrl()
}//end class
Loading
Loading