Skip to content
Snippets Groups Projects
Select Git revision
  • 3abeb6de6112bd177a5f6b96dc944b5637c55b96
  • main default protected
  • step-3263
  • feature/plugins-cli
  • feature/vite
  • step-2484-peerreview
  • biest/issue-5051
  • tests/simplify-jsonapi-tests
  • fix/typo-in-1a70031
  • feature/broadcasting
  • database-seeders-and-factories
  • feature/peer-review-2
  • feature-feedback-jsonapi
  • feature/peerreview
  • feature/balloon-plus
  • feature/stock-images-unsplash
  • tic-2588
  • 5.0
  • 5.2
  • biest/unlock-blocks
  • biest-1514
21 results

WikiPage.php

Blame
  • Forked from Stud.IP / Stud.IP
    1493 commits behind the upstream repository.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    WikiPage.php 5.31 KiB
    <?php
    
    namespace JsonApi\Schemas;
    
    use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
    use Neomerx\JsonApi\Schema\Link;
    
    class WikiPage extends SchemaProvider
    {
        const REGEXP_KEYWORD = '/([\w\.\-\:\(\)_§\/@# ]|&[AOUaou]uml;|&szlig;)+/A';
    
        const TYPE = 'wiki-pages';
        const REL_AUTHOR = 'author';
        const REL_CHILDREN = 'children';
        const REL_DESCENDANTS = 'descendants';
        const REL_PARENT = 'parent';
        const REL_RANGE = 'range';
    
        public static function getRangeClasses(): array
        {
            return [
                'sem' => \Course::class,
                'inst' => \Institute::class,
            ];
        }
    
        public static function getRangeTypes(): array
        {
            return [
                'sem' => Course::TYPE,
                'inst' => Institute::TYPE,
            ];
        }
    
        /**
         * @param mixed $resource
         *
         * @return ?string
         */
        public static function getRangeClass($resource)
        {
            $classes = self::getRangeClasses();
    
            return $classes[
                get_object_type(
                    $resource->range_id,
                    array_keys($classes)
                )
            ];
        }
    
        /**
         * @param mixed $resource
         *
         * @return ?string
         */
        public static function getRangeType($resource)
        {
            $types = self::getRangeTypes();
    
            return $types[
                get_object_type(
                    $resource->range_id,
                    array_keys($types)
                )
            ];
        }
    
        public function getId($wiki): ?string
        {
            return $wiki->id;
        }
    
        public function getAttributes($wiki, ContextInterface $context): iterable
        {
            return [
                'name' => $wiki->name,
                'content' => $wiki->content,
                'chdate' => date('c', $wiki->chdate),
                'version' => count($wiki->versions) + 1,
            ];
        }
    
        public function getRelationships($wiki, ContextInterface $context): iterable
        {
            $isPrimary = $context->getPosition()->getLevel() === 0;
            $includeList = $context->getIncludePaths();
    
            $relationships = [];
    
            if ($isPrimary) {
                $relationships = $this->addAuthorRelationship($relationships, $wiki, $includeList);
                $relationships = $this->addChildrenRelationship($relationships, $wiki, $includeList);
                $relationships = $this->addDescendantsRelationship($relationships, $wiki, $includeList);
                $relationships = $this->addParentRelationship($relationships, $wiki, $includeList);
                $relationships = $this->addRangeRelationship($relationships, $wiki, $includeList);
            }
    
            return $relationships;
        }
    
        private function addParentRelationship(array $relationships, \WikiPage $wiki, array $includeList): array
        {
            $related = $wiki->parent;
            $relationships[self::REL_PARENT] = [
                self::RELATIONSHIP_LINKS_SELF => true,
                self::RELATIONSHIP_DATA => $related
            ];
            if ($related) {
                $relationships[self::REL_PARENT][self::RELATIONSHIP_LINKS] = [
                    Link::RELATED => $this->createLinkToResource($related),
                ];
            }
    
            return $relationships;
        }
    
        /**
         * @SuppressWarnings(PHPMD.UnusedFormalParameter)
         */
        private function addChildrenRelationship(array $relationships, \WikiPage $wiki, array $includeList): array
        {
            $relationships[self::REL_CHILDREN] = [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->getRelationshipRelatedLink($wiki, self::REL_CHILDREN),
                ],
            ];
    
            return $relationships;
        }
    
        /**
         * @SuppressWarnings(PHPMD.UnusedFormalParameter)
         */
        private function addDescendantsRelationship(array $relationships, \WikiPage $wiki, array $includeList): array
        {
            $relationships[self::REL_DESCENDANTS] = [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->getRelationshipRelatedLink($wiki, self::REL_DESCENDANTS),
                ],
            ];
    
            return $relationships;
        }
    
        /**
         * @param array $relationships
         * @param \WikiPage $wiki
         * @param array $includeList
         *
         * @return array
         */
        private function addAuthorRelationship($relationships, $wiki, $includeList)
        {
            if ($wiki->user_id && $wiki->user_id !== 'nobody') {
                $relationships[self::REL_AUTHOR] = [
                    self::RELATIONSHIP_LINKS => [
                        Link::RELATED => $this->createLinkToResource($wiki->user),
                    ],
                    self::RELATIONSHIP_DATA => $wiki->user,
                ];
            }
    
            return $relationships;
        }
    
        /**
         * @param array $relationships
         * @param \WikiPage $wiki
         * @param array $includeList
         *
         * @return array
         */
        private function addRangeRelationship($relationships, $wiki, $includeList)
        {
            $range = $this->prepareRange($wiki);
            $relationships[self::REL_RANGE] = [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($range),
                ],
                self::RELATIONSHIP_DATA => $range,
            ];
    
            return $relationships;
        }
    
        private function prepareRange(\WikiPage $wiki): \Range
        {
            $class = self::getRangeClass($wiki);
    
            /** @var \Range $range */
            $range = $class::build(['id' => $wiki->range_id], false);
    
            return  $range;
        }
    }