public function addCollection(ApiCollection $apiCollection) { $aclRoles = $this->acl->getRoles(); $collection = new DocumentationCollection(); $collection->setName($apiCollection->getName()); $collection->setDescription($apiCollection->getDescription()); $collection->setPath($apiCollection->getPrefix()); // Set fields if ($apiCollection instanceof ApiResource) { if ($modelClass = $apiCollection->getModel()) { if ($transformerClass = $apiCollection->getTransformer()) { /** @var ModelTransformer $transformer */ $transformer = new $transformerClass(); if ($transformer instanceof ModelTransformer) { $transformer->setModelClass($modelClass); $responseFields = $transformer->getResponseProperties(); $dataTypes = $transformer->getModelDataTypes(); $fields = []; foreach ($responseFields as $field) { $fields[$field] = array_key_exists($field, $dataTypes) ? $dataTypes[$field] : ModelTransformer::TYPE_UNKNOWN; } $collection->setFields($fields); } } } } // Add endpoints foreach ($apiCollection->getEndpoints() as $apiEndpoint) { $endpoint = new DocumentationEndpoint(); $endpoint->setName($apiEndpoint->getName()); $endpoint->setDescription($apiEndpoint->getDescription()); $endpoint->setHttpMethod($apiEndpoint->getHttpMethod()); $endpoint->setPath($apiEndpoint->getPath()); $endpoint->setExampleResponse($apiEndpoint->getExampleResponse()); $allowedRoleNames = []; /** @var \Phalcon\Acl\Role $role */ foreach ($aclRoles as $role) { if ($this->acl->isAllowed($role->getName(), $apiCollection->getIdentifier(), $apiEndpoint->getIdentifier())) { $allowedRoleNames[] = $role->getName(); } } $endpoint->setAllowedRoles($allowedRoleNames); $collection->addEndpoint($endpoint); } $this->collections[] = $collection; }
public function transform(DocumentationEndpoint $endpoint) { return ['name' => $endpoint->getName(), 'description' => $endpoint->getDescription(), 'httpMethod' => $endpoint->getHttpMethod(), 'path' => $endpoint->getPath(), 'exampleResponse' => $endpoint->getExampleResponse(), 'allowedRoles' => $endpoint->getAllowedRoles()]; }