Skip to content
Open
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
10 changes: 10 additions & 0 deletions src/Lti/Lti11/Context/DeepLinkingProps.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Cerpus\EdlibResourceKit\Lti\Message\DeepLinking\PresentationDocumentTarget;
use DateTimeImmutable;

use function array_filter;
use function array_map;
use function preg_replace;
Expand All @@ -15,6 +16,7 @@ final class DeepLinkingProps
public const JSONLD_VOCAB = 'http://purl.imsglobal.org/ctx/lti/v1/ContentItem';

public const COPY_ADVICE = 'copyAdvice';
public const CUSTOM = 'custom';
public const EXPIRES_AT = 'expiresAt';
public const DISPLAY_HEIGHT = 'displayHeight';
public const DISPLAY_WIDTH = 'displayWidth';
Expand All @@ -41,6 +43,7 @@ final class DeepLinkingProps
public const TYPE_FLOAT = 'http://www.w3.org/2001/XMLSchema#float';
public const TYPE_INTEGER = 'http://www.w3.org/2001/XMLSchema#integer';
public const TYPE_NORMALIZED_STRING = 'http://www.w3.org/2001/XMLSchema#normalizedString';
public const TYPE_STRING = 'http://www.w3.org/2001/XMLSchema#string';

public static function getCopyAdvice(array $data): bool|null
{
Expand Down Expand Up @@ -116,6 +119,13 @@ public static function getWindowTarget(array $data): string|null
return self::getOfType($data, self::WINDOW_TARGET, self::TYPE_NORMALIZED_STRING);
}

public static function getCustom(array $data): array|null
{
$custom = self::getArrayOfType($data, self::CUSTOM, self::TYPE_STRING);

return empty($custom) ? null : $custom;
}

public static function getOfType(array $data, string $prop, string $type): mixed
{
$value = $data[$prop] ?? null;
Expand Down
4 changes: 2 additions & 2 deletions src/Lti/Lti11/Mapper/DeepLinking/ContentItemMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public function map(array $data): ContentItem
$thumbnail,
Prop::getTitle($data),
Prop::getUrl($data),
custom: [], // TODO
lineItem: $lineItem,
Prop::getCustom($data),
$lineItem,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
final readonly class LtiLinkItemSerializer implements LtiLinkItemSerializerInterface
{
public function __construct(
private ContentItemSerializer $serializer = new ContentItemSerializer(),
private ContentItemSerializerInterface $serializer = new ContentItemSerializer(),
private LineItemSerializerInterface $lineItemSerializer = new LineItemSerializer(),
) {
}

/**
* @todo Handle the "custom" property
*/
public function serialize(LtiLinkItem $item): array
{
$serialized = [
Expand All @@ -31,6 +28,10 @@ public function serialize(LtiLinkItem $item): array
->serialize($item->getLineItem());
}

if (!empty($item->getCustom())) {
$serialized[Prop::CUSTOM] = $item->getCustom();
}

return $serialized;
}
}
4 changes: 2 additions & 2 deletions src/Lti/Message/DeepLinking/LtiLinkItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(
Image|null $thumbnail = null,
string|null $title = null,
string|null $url = null,
private readonly array $custom = [],
private readonly array|null $custom = null,
private readonly LineItem|null $lineItem = null,
) {
parent::__construct(
Expand All @@ -31,7 +31,7 @@ public function __construct(
);
}

public function getCustom(): array
public function getCustom(): array|null
{
return $this->custom;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Lti/Lti11/Mapper/ContentItemsMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public function testItMapsAllTheStuff(): void
'totalMaximum' => 42.0,
],
],
'custom' => [
'level' => 'expert',
'numericLevel' => 42,
],
],
[
'@type' => 'FileItem',
Expand Down Expand Up @@ -105,6 +109,11 @@ public function testItMapsAllTheStuff(): void
$this->assertSame(39.5, $score->getNormalMaximum());
$this->assertSame(2.5, $score->getExtraCreditMaximum());
$this->assertSame(42.0, $score->getTotalMaximum());

$custom = $contentItems[0]->getCustom();
$this->assertIsArray($custom);
$this->assertSame('expert', $custom['level']);
$this->assertSame(42, $custom['numericLevel']);
}

public function testMapsDataWithAdditionalJsonldContexts(): void
Expand Down
8 changes: 8 additions & 0 deletions tests/Lti/Lti11/Serializer/ContentItemsSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function testSerializesContentItems(): array
text: 'A cool text description of my cool content',
title: 'My Cool Content',
url: 'https://example.com/lti',
custom: [
'level' => 'expert',
'numericLevel' => 42,
],
lineItem: new LineItem(
new ScoreConstraints(39.5, 2.5)
),
Expand Down Expand Up @@ -118,6 +122,10 @@ public function testSerializesContentItems(): array
'totalMaximum' => 42.0,
],
],
'custom' => [
'level' => 'expert',
'numericLevel' => 42,
],
],
[
'@type' => 'FileItem',
Expand Down
Loading