Skip to content

Commit

Permalink
Enhance data validation of incoming messages and service responses
Browse files Browse the repository at this point in the history
  • Loading branch information
spvickers committed Aug 29, 2024
1 parent ab9dee9 commit 398ec88
Show file tree
Hide file tree
Showing 21 changed files with 2,000 additions and 853 deletions.
28 changes: 19 additions & 9 deletions src/Content/FileItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace ceLTIc\LTI\Content;

use ceLTIc\LTI\Util;

/**
* Class to represent a file content-item object
*
Expand Down Expand Up @@ -100,19 +102,27 @@ public function toJsonObject(): object
*
* @param object $item A JSON object representing a file content-item
*
* @return void
* @return bool True if the item is valid
*/
protected function fromJsonObject(object $item): void
protected function fromJsonObject(object $item): bool
{
parent::fromJsonObject($item);
foreach (get_object_vars($item) as $name => $value) {
switch ($name) {
case 'copyAdvice':
case 'expiresAt':
$this->{$name} = $item->{$name};
break;
$ok = parent::fromJsonObject($item);
if ($ok) {
foreach (get_object_vars($item) as $name => $value) {
switch ($name) {
case 'copyAdvice':
$this->copyAdvice = Util::checkBoolean($item, 'FileItem/copyAdvice');
$ok = $ok && !is_null($this->copyAdvice);
break;
case 'expiresAt':
$this->expiresAt = Util::checkBoolean($item, 'FileItem/expiresAt');
$ok = $ok && !is_null($this->expiresAt);
break;
}
}
}

return $ok;
}

}
21 changes: 15 additions & 6 deletions src/Content/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace ceLTIc\LTI\Content;

use ceLTIc\LTI\Util;

/**
* Class to represent a content-item image object
*
Expand Down Expand Up @@ -89,12 +91,13 @@ public function toJsonObject(): object
/**
* Generate an Image object from its JSON or JSON-LD representation.
*
* @param object $item A JSON or JSON-LD object representing a content-item
* @param object|string $item A JSON or JSON-LD object representing an image or an image URL
*
* @return Image|null The Image object
*/
public static function fromJsonObject(object $item): ?Image
public static function fromJsonObject(object|string $item): ?Image
{
$ok = true;
$obj = null;
$width = null;
$height = null;
Expand All @@ -104,20 +107,26 @@ public static function fromJsonObject(object $item): ?Image
switch ($name) {
case '@id':
case 'url':
$url = $item->{$name};
$url = Util::checkString($item, "Image/{$name}", false, false, '', false, null);
break;
case 'width':
$width = $item->width;
$width = Util::checkInteger($item, "Image/width", false, 0, true);
if (is_null($width)) {
$ok = false;
}
break;
case 'height':
$height = $item->height;
$height = Util::checkInteger($item, "Image/height", false, 0, true);
if (is_null($height)) {
$ok = false;
}
break;
}
}
} else {
$url = $item;
}
if ($url) {
if ($ok && $url) {
$obj = new Image($url, $width, $height);
}

Expand Down
Loading

0 comments on commit 398ec88

Please sign in to comment.