R/TaxonomicEnrichment.r

# Netherlands Biodiversity API
#
# Access to the digitised Natural History collection at the Naturalis Biodiversity Center
#
# OpenAPI spec version: v2
# Contact: support@naturalis.nl
# Generated by: https://github.com/swagger-api/swagger-codegen.git

#'
#' @docType class
#'
#' @format R6 class
#'
#' @usage
#' # TaxonomicEnrichment$new()
#'
#' @format
#' R6 class
#'
#' @title TaxonomicEnrichment Class
#'
#' @description
#' For more information on the NBA object model, please refer to the
#' official NBA documentation at
#' \href{https://docs.biodiversitydata.nl}{https://docs.biodiversitydata.nl} and
#' the NBA model and endpoints reference at
#' \href{https://docs.biodiversitydata.nl/endpoints-reference}{https://docs.biodiversitydata.nl/endpoints-reference}.
#'
#' @details Model class for TaxonomicEnrichment objects.
#'
#'
#' @field vernacularNames  list(SummaryVernacularName)
#'
#' @field synonyms  list(SummaryScientificName)
#'
#' @field sourceSystem  SummarySourceSystem
#'
#' @field taxonId  character
#'
#'
#'
#' @section Methods:
#' \describe{
#'
#' \item{\code{$new()}}{
#'
#'   Constructor TaxonomicEnrichment object.
#'
#' }
#' \item{\code{$fromList(TaxonomicEnrichmentList)}}{
#'
#'   Create TaxonomicEnrichment object from list.
#'
#' }
#'
#' \item{\code{$toList()}}{
#'
#'   Get list representation of TaxonomicEnrichment.
#'
#' }
#' \item{\code{fromJSONString(TaxonomicEnrichmentJson)}}{
#'
#'   Create TaxonomicEnrichment object from JSON.
#'
#' }
#' \item{\code{toJSONString(pretty=TRUE)}}{
#'
#'   Get JSON representation of TaxonomicEnrichment.
#'
#' }
#' }
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
TaxonomicEnrichment <- R6::R6Class(
  "TaxonomicEnrichment",
  public = list(
    `vernacularNames` = NULL,
    `synonyms` = NULL,
    `sourceSystem` = NULL,
    `taxonId` = NULL,
    initialize = function(
                              `vernacularNames`,
                              `synonyms`,
                              `sourceSystem`,
                              `taxonId`) {
      if (!missing(`vernacularNames`)) {
        stopifnot(
          is.list(`vernacularNames`),
          length(`vernacularNames`) != 0
        )
        lapply(
          `vernacularNames`,
          function(x) stopifnot(R6::is.R6(x))
        )
        ## omit names as they should not be part of JSON representation
        self[["vernacularNames"]] <- unname(`vernacularNames`)
      }
      if (!missing(`synonyms`)) {
        stopifnot(
          is.list(`synonyms`),
          length(`synonyms`) != 0
        )
        lapply(
          `synonyms`,
          function(x) stopifnot(R6::is.R6(x))
        )
        ## omit names as they should not be part of JSON representation
        self[["synonyms"]] <- unname(`synonyms`)
      }
      if (!missing(`sourceSystem`)) {
        stopifnot(R6::is.R6(`sourceSystem`))
        self[["sourceSystem"]] <- `sourceSystem`
      }
      if (!missing(`taxonId`)) {
        stopifnot(
          is.character(`taxonId`),
          length(`taxonId`) == 1
        )
        self[["taxonId"]] <- `taxonId`
      }
    },
    toList = function() {
      TaxonomicEnrichmentList <- list()
      if (!is.null(self[["vernacularNames"]])) {
        TaxonomicEnrichmentList[["vernacularNames"]] <-
          lapply(self[["vernacularNames"]], function(x) x$toList())
      }
      if (!is.null(self[["synonyms"]])) {
        TaxonomicEnrichmentList[["synonyms"]] <-
          lapply(self[["synonyms"]], function(x) x$toList())
      }
      if (!is.null(self[["sourceSystem"]])) {
        TaxonomicEnrichmentList[["sourceSystem"]] <-
          self[["sourceSystem"]]$toList()
      }
      if (!is.null(self[["taxonId"]])) {
        TaxonomicEnrichmentList[["taxonId"]] <-
          self[["taxonId"]]
      }
      ## omit empty nested lists in returned list
      TaxonomicEnrichmentList[vapply(
        TaxonomicEnrichmentList,
        length,
        FUN.VALUE = integer(1)
      ) > 0]
    },
    fromList = function(TaxonomicEnrichmentList,
                            typeMapping = NULL) {
      self[["vernacularNames"]] <- lapply(
        TaxonomicEnrichmentList[["vernacularNames"]],
        function(x) {
          SummaryVernacularName$new()$fromList(x,
            typeMapping = typeMapping
          )
        }
      )
      self[["synonyms"]] <- lapply(
        TaxonomicEnrichmentList[["synonyms"]],
        function(x) {
          SummaryScientificName$new()$fromList(x,
            typeMapping = typeMapping
          )
        }
      )
      self[["sourceSystem"]] <- SummarySourceSystem$new()$fromList(
        TaxonomicEnrichmentList[["sourceSystem"]],
        typeMapping = typeMapping
      )
      self[["taxonId"]] <-
        TaxonomicEnrichmentList[["taxonId"]]
      invisible(self)
    },
    toJSONString = function(pretty = TRUE) {
      jsonlite::toJSON(
        self$toList(),
        simplifyVector = TRUE,
        auto_unbox = TRUE,
        pretty = pretty
      )
    },
    fromJSONString = function(TaxonomicEnrichmentJson,
                                  typeMapping = NULL) {
      TaxonomicEnrichmentList <- jsonlite::fromJSON(
        TaxonomicEnrichmentJson,
        simplifyVector = FALSE
      )
      self <- self$fromList(TaxonomicEnrichmentList)
      invisible(self)
    },
    print = function(...) {
      ## print class name
      cat("<TaxonomicEnrichment>\n")
      ## print all members with values
      cat("Fields:\n")
      if (typeof(self$vernacularNames) == "environment") {
        cat("\tvernacularNames:\tobject of class", paste0("<", class(self$vernacularNames)[1], ">"), "\n")
      }
      else if (typeof(self$vernacularNames) == "list") {
        cat("\tvernacularNames:\tlist of length", length(self$vernacularNames), "\n")
      }
      else {
        cat("\tvernacularNames:\t", self$vernacularNames, "\n")
      }
      if (typeof(self$synonyms) == "environment") {
        cat("\tsynonyms:\tobject of class", paste0("<", class(self$synonyms)[1], ">"), "\n")
      }
      else if (typeof(self$synonyms) == "list") {
        cat("\tsynonyms:\tlist of length", length(self$synonyms), "\n")
      }
      else {
        cat("\tsynonyms:\t", self$synonyms, "\n")
      }
      if (typeof(self$sourceSystem) == "environment") {
        cat("\tsourceSystem:\tobject of class", paste0("<", class(self$sourceSystem)[1], ">"), "\n")
      }
      else if (typeof(self$sourceSystem) == "list") {
        cat("\tsourceSystem:\tlist of length", length(self$sourceSystem), "\n")
      }
      else {
        cat("\tsourceSystem:\t", self$sourceSystem, "\n")
      }
      if (typeof(self$taxonId) == "environment") {
        cat("\ttaxonId:\tobject of class", paste0("<", class(self$taxonId)[1], ">"), "\n")
      }
      else if (typeof(self$taxonId) == "list") {
        cat("\ttaxonId:\tlist of length", length(self$taxonId), "\n")
      }
      else {
        cat("\ttaxonId:\t", self$taxonId, "\n")
      }
      ## print all methods
      cat("Methods:\n")
      cat("\tfromJSONString\n")
      cat("\ttoJSONString\n")
      cat("\tfromList\n")
      cat("\ttoList\n")
      cat("\tprint\n")
      invisible(self)
    }
  )
)
naturalis/nbaR documentation built on Nov. 12, 2023, 4:47 p.m.