aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas
diff options
context:
space:
mode:
Diffstat (limited to 'lib/classes/JsonApi/Schemas')
-rw-r--r--lib/classes/JsonApi/Schemas/ResourceBookingIntervalSchema.php91
-rw-r--r--lib/classes/JsonApi/Schemas/ResourcePropertySchema.php37
-rw-r--r--lib/classes/JsonApi/Schemas/ResourceSchema.php89
3 files changed, 217 insertions, 0 deletions
diff --git a/lib/classes/JsonApi/Schemas/ResourceBookingIntervalSchema.php b/lib/classes/JsonApi/Schemas/ResourceBookingIntervalSchema.php
new file mode 100644
index 0000000..888d3cd
--- /dev/null
+++ b/lib/classes/JsonApi/Schemas/ResourceBookingIntervalSchema.php
@@ -0,0 +1,91 @@
+<?php
+namespace JsonApi\Schemas;
+
+use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
+use Neomerx\JsonApi\Schema\Link;
+use ResourceBookingInterval;
+
+final class ResourceBookingIntervalSchema extends SchemaProvider
+{
+ const TYPE = 'resource-booking-intervals';
+
+ const REL_BOOKING = 'booking';
+ const REL_RESOURCE = 'resource';
+
+ /**
+ * @param ResourceBookingInterval $resource
+ */
+ public function getId($resource): ?string
+ {
+ return $resource->id;
+ }
+
+ /**
+ * @param ResourceBookingInterval $resource
+ */
+ public function getAttributes($resource, ContextInterface $context): iterable
+ {
+ return [
+ 'begin' => date('c', $resource->begin),
+ 'end' => date('c', $resource->end),
+
+ 'takes-place' => (bool) $resource->takes_place,
+
+ 'mkdate' => date('c', $resource->mkdate),
+ 'chdate' => date('c', $resource->chdate),
+ ];
+ }
+
+ /**
+ * @param ResourceBookingInterval $resource
+ */
+ public function getRelationships($resource, ContextInterface $context): iterable
+ {
+ $relationships = [];
+
+ $isPrimary = $context->getPosition()->getLevel() === 0;
+ if ($isPrimary) {
+ $relationships = $this->getBookingRelationship(
+ $relationships,
+ $resource->booking,
+ $this->shouldInclude($context, self::REL_BOOKING)
+ );
+ $relationships = $this->getResourceRelationship(
+ $relationships,
+ $resource->resource,
+ $this->shouldInclude($context, self::REL_RESOURCE)
+ );
+ }
+
+ return $relationships;
+ }
+
+ private function getBookingRelationship(
+ array $relationships,
+ \ResourceBooking $booking,
+ bool $includeData
+ ): array {
+ $relationships[self::REL_BOOKING] = [
+ self::RELATIONSHIP_LINKS => [
+ Link::RELATED => $this->createLinkToResource($booking),
+ ],
+ self::RELATIONSHIP_DATA => $includeData ? $booking : \ResourceBooking::build(['id' => $booking->id]),
+ ];
+
+ return $relationships;
+ }
+
+ private function getResourceRelationship(
+ array $relationships,
+ \Resource $resource,
+ bool $includeData
+ ): array {
+ $relationships[self::REL_RESOURCE] = [
+ self::RELATIONSHIP_LINKS => [
+ Link::RELATED => $this->createLinkToResource($resource),
+ ],
+ self::RELATIONSHIP_DATA => $includeData ? $resource : \Resource::build(['id' => $resource->id]),
+ ];
+ return $relationships;
+ }
+}
diff --git a/lib/classes/JsonApi/Schemas/ResourcePropertySchema.php b/lib/classes/JsonApi/Schemas/ResourcePropertySchema.php
new file mode 100644
index 0000000..d9caf30
--- /dev/null
+++ b/lib/classes/JsonApi/Schemas/ResourcePropertySchema.php
@@ -0,0 +1,37 @@
+<?php
+namespace JsonApi\Schemas;
+
+use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
+use ResourceProperty;
+
+final class ResourcePropertySchema extends SchemaProvider
+{
+ /**
+ * @param ResourceProperty $resource
+ */
+ public function getId($resource): ?string
+ {
+ return $resource->id;
+ }
+
+ /**
+ * @param ResourceProperty $resource
+ */
+ public function getAttributes($resource, ContextInterface $context): iterable
+ {
+ return [
+ 'state' => $resource->state,
+
+ 'mkdate' => date('c', $resource->mkdate),
+ 'chdate' => date('c', $resource->chdate),
+ ];
+ }
+
+ /**
+ * @param ResourceProperty $resource
+ */
+ public function getRelationships($resource, ContextInterface $context): iterable
+ {
+ return [];
+ }
+}
diff --git a/lib/classes/JsonApi/Schemas/ResourceSchema.php b/lib/classes/JsonApi/Schemas/ResourceSchema.php
new file mode 100644
index 0000000..bcf89db
--- /dev/null
+++ b/lib/classes/JsonApi/Schemas/ResourceSchema.php
@@ -0,0 +1,89 @@
+<?php
+namespace JsonApi\Schemas;
+
+use Neomerx\JsonApi\Contracts\Schema\BaseLinkInterface;
+use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
+use Resource;
+
+final class ResourceSchema extends SchemaProvider
+{
+ const TYPE = 'resources';
+
+ const REL_CATEGORY = 'category';
+
+ /**
+ * @param Resource $resource
+ */
+ public function getId($resource): ?string
+ {
+ return $resource->id;
+ }
+
+ /**
+ * @param Resource $resource
+ */
+ public function getAttributes($resource, ContextInterface $context): iterable
+ {
+ return [
+ 'level' => (int) $resource->level,
+ 'name' => (string) $resource->name,
+ 'description' => (string) $resource->description,
+ 'requestable' => (bool) $resource->requestable,
+ 'lockable' => (bool) $resource->lockable,
+ 'sort_position' => (int) $resource->sort_position,
+
+ 'mkdate' => date('c', $resource->mkdate),
+ 'chdate' => date('c', $resource->chdate),
+ ];
+ }
+
+ /**
+ * @param Resource $resource
+ */
+ public function hasResourceMeta($resource): bool
+ {
+ return true;
+ }
+
+ /**
+ * @param Resource $resource
+ */
+ public function getResourceMeta($resource)
+ {
+ return [
+ 'class' => $resource->class_name,
+ ];
+ }
+
+ /**
+ * @param Resource $resource
+ */
+ public function getRelationships($resource, ContextInterface $context): iterable
+ {
+ if ($context->getPosition()->getLevel() > 0) {
+ return [];
+ };
+
+ $relationships = [];
+
+ $relationships = $this->getCategoryRelationship(
+ $relationships,
+ $resource,
+ $this->shouldInclude($context, self::REL_CATEGORY)
+ );
+
+ return $relationships;
+ }
+
+ private function getCategoryRelationship(array $relationships, $resource, bool $shouldInclude)
+ {
+ $relationships[self::REL_CATEGORY] = [
+ self::RELATIONSHIP_LINKS => [
+ BaseLinkInterface::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_CATEGORY),
+ ],
+ self::RELATIONSHIP_DATA => $shouldInclude ? $resource->category : \ResourceCategory::build(['id' => $resource->category_id]),
+ ];
+
+ return $relationships;
+ }
+}