Skip to content

Commit

Permalink
Convert loanObject:# IDs to cantor paired ints [MA-156]
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhiltri committed Feb 28, 2024
1 parent 4fdddd7 commit 8dc39e3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
20 changes: 20 additions & 0 deletions app/Helpers/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Helpers;

class Util
{
public const COLLECTION_OBJECT = 1;
public const LOAN_OBJECT = 2;

/**
* Generate a unique ID based on a combination of two numbers.
* @param int $x
* @param int $y
* @return int
*/
public static function cantorPair($x, $y)
{
return (($x + $y) * ($x + $y + 1)) / 2 + $y;
}
}
11 changes: 8 additions & 3 deletions app/Models/Transformers/ObjectTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models\Transformers;

use A17\Twill\Models\Contracts\TwillModelContract;
use App\Helpers\Util;
use App\Models\CollectionObject;
use App\Models\LoanObject;
use App\Repositories\Serializers\OptionalKeyArraySerializer;
Expand All @@ -18,26 +19,30 @@ class ObjectTransformer extends TransformerAbstract

public function transform(TwillModelContract $object): array
{
$objectType = lcfirst(class_basename($object));
$latitude = $object->latitude ?? $object->gallery?->latitude;
$longitude = $object->longitude ?? $object->gallery?->longitude;
switch (get_class($object)) {
case CollectionObject::class:
$thumbnail = $object->getApiModel()->image('iiif', 'thumbnail');
$image = $object->getApiModel()->image('iiif');
$objectType = Util::COLLECTION_OBJECT;
break;
case LoanObject::class:
$thumbnail = $object->image('upload', 'thumbnail');
$image = $object->image('upload');
$objectType = Util::LOAN_OBJECT;
break;
default:
$thumbnail = null;
$image = null;
$objectType = 0;
}
$nid = Util::cantorPair($objectType, $object->id);

return [
"$objectType:$object->id" => $this->withCustomIncludes($object, [
$nid => $this->withCustomIncludes($object, [
'title' => $object->title,
'nid' => "$objectType:$object->id", // Legacy from Drupal
'nid' => $nid, // Legacy from Drupal
'id' => $object->datahub_id ? (int) $object->datahub_id : null,
'artist_culture_place_delim' => $object->artist_display,
'credit_line' => $object->credit_line,
Expand Down
16 changes: 15 additions & 1 deletion app/Models/Transformers/StopTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@

use A17\Twill\Models\Contracts\TwillModelContract;
use League\Fractal\TransformerAbstract;
use App\Helpers\Util;
use App\Models\CollectionObject;
use App\Models\LoanObject;

class StopTransformer extends TransformerAbstract
{
public function transform(TwillModelContract $stop): array
{
switch (get_class($stop->selector?->object)) {
case CollectionObject::class:
$objectType = Util::COLLECTION_OBJECT;
break;
case LoanObject::class:
$objectType = Util::LOAN_OBJECT;
break;
default:
$objectType = 0;
}
$nid = Util::cantorPair($objectType, $stop->object_id);
return [
'object' => lcfirst(class_basename($stop->selector?->object)) . ':' . $stop->object_id,
'object' => $nid,
'audio_id' => (string) $stop->selector?->number,
'audio_bumper' => null, // Legacy from Drupal
'sort' => $stop->pivot->position,
Expand Down

0 comments on commit 8dc39e3

Please sign in to comment.