<commit_before><?php

namespace Apigee\SmartDocs;

use Apigee\Util\OrgConfig;
use Apigee\Util\APIObject;
use Apigee\Exceptions\ParameterException;

/**
 * Abstraction of a Revision of a model.
 *
 * @package Apigee\SmartDocs
 * @author djohnson
 */
class Revision extends APIObject
{
    /** @var string */
    protected $id;

    /** @var string */
    protected $displayName;

    /** @var string */
    protected $description;

    /** @var string */
    protected $releaseVersion;

    /** @var string */
    protected $changeLog;

    /** @var array */
    protected $apiSchema;

    /** @var string */
    protected $baseUrl;

    /** @var array */
    protected $params;

    /** @var array */
    protected $paramGroups;

    /** @var array */
    protected $tags;

    /** @var array */
    protected $customAttributes;


    /** @var array */
    protected $resources;

    /** @var int */
    protected $revisionNumber;

    /** @var int */
    protected $createdTime;

    /** @var int */
    protected $createdBy;

    /** @var string */
    protected $modifiedTime;

    /** @var string */
    protected $modifiedBy;

    /** @var  string */
    protected $apiId;

    /** @var string */
    protected $apiName;

    /* * @var bool */
    //protected $isactive;

    /* * @var bool */
    //protected $isLatest;

    /**
     * Restores this Revision object to its pristine state.
     */
    protected function blankValues()
    {
        $this->displayName = '';
        $this->description = '';
        $this->releaseVersion = '';
        $this->changeLog = '';
        $this->apiSchema = array();
        $this->baseUrl = '';
        $this->params = array();
        $this->paramGroups = array();
        $this->tags = array();
        $this->customAttributes = array();

        // Auto-generated fields (read-only)
        $this->id = '';
        $this->revisionNumber = 0;
        $this->createdTime = 0;
        $this->modifiedTime = 0;
        $this->createdBy = '';
        $this->modifiedBy = '';
        //$this->isactive = '';
        //$this->isLatest = '';

        $this->resources = array();
        $this->apiId = '';
        $this->apiName = '';
    }

    /* Accessors (getters/setters) */
    public function getUuid()
    {
        return $this->id;
    }

    public function setUuid($uuid)
    {
        $this->id = $uuid;
    }

    public function getDisplayName()
    {
        return $this->displayName;
    }

    public function setDisplayName($name)
    {
        $this->displayName = $name;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setDescription($desc)
    {
        $this->description = $desc;
    }

    public function getApiName()
    {
        return $this->apiName;
    }

    public function getReleaseVersion()
    {
        return $this->releaseVersion;
    }

    public function setReleaseVersion($ver)
    {
        $this->releaseVersion = $ver;
    }

    public function getChangeLog()
    {
        return $this->changeLog;
    }

    public function setChangeLog($log)
    {
        $this->changeLog = $log;
    }

    public function getApiSchema()
    {
        return $this->apiSchema;
    }

    public function setApiSchema(array $schema)
    {
        $this->apiSchema = $schema;
    }

    public function getBaseUrl()
    {
        return $this->baseUrl;
    }

    public function setBaseUrl($url)
    {
        $this->baseUrl = $url;
    }

    public function getParameters()
    {
        return $this->params;
    }

    public function setParameters(array $params)
    {
        $this->params = $params;
    }

    public function getParamGroups()
    {
        return $this->paramGroups;
    }

    public function setParamGroups(array $groups)
    {
        $this->paramGroups = $groups;
    }

    public function getTags()
    {
        return $this->tags;
    }

    public function setTags(array $tags)
    {
        $this->tags = $tags;
    }

    public function getCustomAttributes()
    {
        return $this->customAttributes;
    }

    public function setCustomAttributes(array $attr)
    {
        $this->customAttributes = $attr;
    }

    public function getRevisionNumber()
    {
        return $this->revisionNumber;
    }

    public function &getResources()
    {
        return $this->resources;
    }

    public function getCreatedTime()
    {
        return floor($this->createdTime / 1000);
    }

    public function getModifiedTime()
    {
        return floor($this->modifiedTime / 1000);
    }


    /**
     * Takes values from an array and populates a Revision with them.
     *
     * @param Revision $model
     * @param array $array
     */
    public static function fromArray(Revision $revision, array $array)
    {
        foreach ($array as $key => $value) {
            if (property_exists($revision, $key)) {
                $revision->$key = $value;
            }
        }
        // Fill in sub-objects
        if (!empty($revision->resources)) {
            foreach ($revision->resources as &$resource) {
                if (is_array($resource)) {
                    $resourceObj = new Resource($revision->getConfig(), $revision->apiId, $revision->id);
                    Resource::fromArray($resourceObj, $resource);
                    $resource = $resourceObj;
                }
            }
        }
    }

    /**
     * Persists the current Revision as an array.
     *
     * @return array
     */
    public function toArray($verbose = TRUE)
    {
        $payload_keys = array(
            'displayName', 'description', 'releaseVersion', 'changeLog', 'apiSchema',
            'baseUrl', 'params', 'paramGroups', 'tags', 'customAttributes',
        );
        if ($verbose) {
            $payload_keys = array_merge($payload_keys, array(
                'id', 'revisionNumber', 'createdTime', 'modifiedTime', 'apiId',
                'createdBy', 'modifiedBy'
            ));
        }
        $payload = array();
        foreach ($payload_keys as $key) {
            $payload[$key] = $this->$key;
        }
        if ($verbose && count($this->resources) > 0) {
            $payload['resources'] = array();
            foreach ($this->resources as $resource) {
                $payload['resources'][] = $resource->toArray();
            }
        }
        return $payload;
    }

    /**
     * Initializes this object.
     *
     * @param OrgConfig $config
     * @param string $modelId
     * @param string $modelName
     */
    public function __construct(OrgConfig $config, $modelId, $modelName = "")
    {
        $this->blankValues();
        $this->apiId = $modelId;
        $this->apiName = $modelName;
//        $this->init($config, '/o/' . rawurlencode($config->orgName) . '/apimodels/' . rawurlencode($this->apiId) . '/revisions');
      $this->init($config, '/o/' . rawurlencode($config->orgName) . '/apimodels/');
    }

    /**
     * Returns an array of all revisions for this model. The order of items in
     * the array may be non-deterministic.
     *
     * @return array
     */
    public function listRevisions()
    {
        $revisions = array();
        $this->get();
        foreach ($this->responseObj as $key => $blob) {
            $revision = new Revision($this->getConfig(), $this->apiId, $this->apiName);
            self::fromArray($revision, $blob);
            $revisions[$key] = $revision;
        }
        return $revisions;
    }

    /**
     * Loads a revision.
     *
     * @param string|int|null $revisionId
     *        This may be a UUID or a revision number.
     * @throws ParameterException
     */
    public function load($revisionId = null)
    {
        $revisionId = $revisionId ?: $this->id;
        if (empty($revisionId)) {
            throw new ParameterException('Cannot load a revision with no Revision UUID.');
        }
        if (is_int($revisionId) && $revisionId < 1) {
            throw new ParameterException('Cannot load a revision number less than 1.');
        }
        $this->get(rawurlencode($this->apiId) . '/revisions/' . $revisionId . '?expand=true');
        self::fromArray($this, $this->responseObj);
    }

    /**
     * Saves the current revision. If $update is true we presume that this is
     * an update (PUT) operation; otherwise it's an insert (POST).
     *
     * @todo Validate that required fields are not empty.
     *
     * @param bool $update
     */
    public function save($update = FALSE)
    {
        $payload = $this->toArray(FALSE);
        $keys = array_keys($payload);
        foreach ($keys as $key) {
            if (empty($payload[$key])) {
                unset($payload[$key]);
            }
        }
        if ($update) {
            $url = $this->id;
            $method = 'put';
        } else {
            $url = '';
            $method = 'post';
        }
        $this->$method($url, $payload);
        self::fromArray($this, $this->responseObj);
    }

    /**
     * Deletes a revision.
     *
     * @param string|int|null $revisionUuid
     *        This may be a UUID or a revision number.
     * @throws ParameterException
     */
    public function delete($revisionId = null)
    {
        $revisionId = $revisionId ?: $this->id;
        if (empty($revisionId)) {
            throw new ParameterException('Cannot delete a revision with no Revision UUID.');
        }
        $this->http_delete($revisionId);
        // TODO: should we do this, or call blankValues()?
        self::fromArray($this, $this->responseObj);
    }

    /**
     * Imports a model revision from a Swagger URL.
     *
     * @param string $modelId
     * @param string $swaggerUrl
     */
    public function importSwagger($swaggerUrl)
    {
        $combinedString = rawurlencode($this->apiId) . '/revisions?action=import&format=swagger';
        $this->blankValues();
        $this->post($combinedString, 'URL=' . $swaggerUrl, 'text/plain; charset=utf-8');
        $response = $this->responseObj;
        self::fromArray($this, $response);
    }

  /**
   * Imports a model revision from a WADL document.
   *
   * @param string $modelId
   * @param string $xml
   */
  public function importSwaggerFile($yaml)
  {
    $combinedString = $this->apiName . '/import/file?format=swagger';
    $this->blankValues();
    $this->post($combinedString, $yaml, 'application/yaml; charset=utf-8');
    $response = $this->responseObj;
    self::fromArray($this, $response);
  }

    /**
     * Imports a model revision from a WADL document.
     *
     * @param string $modelId
     * @param string $xml
     */
    public function importWadl($xml)
    {
        $this->blankValues();
        $this->post('edge/revisions?action=import&format=wadl', $xml, 'application/xml; charset=utf-8');
        $response = $this->responseObj;
        self::fromArray($this, $response);
    }

    /**
     * Imports a model revision from an Apigee Internal JSON document.
     *
     * @param string $modelId
     * @param string $json
     */
    public function importApigeeJson($json)
    {
        $this->blankValues();
        $this->post('?action=import&format=apimodel', $json, 'application/json; charset=utf-8');
        $response = $this->responseObj;
        self::fromArray($this, $response);
    }

    /**
     * Exports a SmartDocs revision as JSON (default) or an XML-based format.
     *
     * @param string $modelId
     * @param string $format
     * @param int|null $revision
     *
     * @return string
     */
    public function export($format, $revision = NULL)
    {
        $revision = $revision ?: 'latest';
        if ($format == 'json' || empty($format)) {
            $this->get($revision . '?expand=yes');
        } else {
            $this->get($revision . '?expand=yes&format=' . $format, 'text/xml');
        }<commit_msg>DEVSOL-1313: import yaml model files<commit_after><?php

namespace Apigee\SmartDocs;

use Apigee\Util\OrgConfig;
use Apigee\Util\APIObject;
use Apigee\Exceptions\ParameterException;

/**
 * Abstraction of a Revision of a model.
 *
 * @package Apigee\SmartDocs
 * @author djohnson
 */
class Revision extends APIObject
{
    /** @var string */
    protected $id;

    /** @var string */
    protected $displayName;

    /** @var string */
    protected $description;

    /** @var string */
    protected $releaseVersion;

    /** @var string */
    protected $changeLog;

    /** @var array */
    protected $apiSchema;

    /** @var string */
    protected $baseUrl;

    /** @var array */
    protected $params;

    /** @var array */
    protected $paramGroups;

    /** @var array */
    protected $tags;

    /** @var array */
    protected $customAttributes;


    /** @var array */
    protected $resources;

    /** @var int */
    protected $revisionNumber;

    /** @var int */
    protected $createdTime;

    /** @var int */
    protected $createdBy;

    /** @var string */
    protected $modifiedTime;

    /** @var string */
    protected $modifiedBy;

    /** @var  string */
    protected $apiId;

    /** @var string */
    protected $apiName;

    /* * @var bool */
    //protected $isactive;

    /* * @var bool */
    //protected $isLatest;

    /**
     * Restores this Revision object to its pristine state.
     */
    protected function blankValues()
    {
        $this->displayName = '';
        $this->description = '';
        $this->releaseVersion = '';
        $this->changeLog = '';
        $this->apiSchema = array();
        $this->baseUrl = '';
        $this->params = array();
        $this->paramGroups = array();
        $this->tags = array();
        $this->customAttributes = array();

        // Auto-generated fields (read-only)
        $this->id = '';
        $this->revisionNumber = 0;
        $this->createdTime = 0;
        $this->modifiedTime = 0;
        $this->createdBy = '';
        $this->modifiedBy = '';
        //$this->isactive = '';
        //$this->isLatest = '';

        $this->resources = array();
        $this->apiId = '';
        $this->apiName = '';
    }

    /* Accessors (getters/setters) */
    public function getUuid()
    {
        return $this->id;
    }

    public function setUuid($uuid)
    {
        $this->id = $uuid;
    }

    public function getDisplayName()
    {
        return $this->displayName;
    }

    public function setDisplayName($name)
    {
        $this->displayName = $name;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setDescription($desc)
    {
        $this->description = $desc;
    }

    public function getApiName()
    {
        return $this->apiName;
    }

    public function getReleaseVersion()
    {
        return $this->releaseVersion;
    }

    public function setReleaseVersion($ver)
    {
        $this->releaseVersion = $ver;
    }

    public function getChangeLog()
    {
        return $this->changeLog;
    }

    public function setChangeLog($log)
    {
        $this->changeLog = $log;
    }

    public function getApiSchema()
    {
        return $this->apiSchema;
    }

    public function setApiSchema(array $schema)
    {
        $this->apiSchema = $schema;
    }

    public function getBaseUrl()
    {
        return $this->baseUrl;
    }

    public function setBaseUrl($url)
    {
        $this->baseUrl = $url;
    }

    public function getParameters()
    {
        return $this->params;
    }

    public function setParameters(array $params)
    {
        $this->params = $params;
    }

    public function getParamGroups()
    {
        return $this->paramGroups;
    }

    public function setParamGroups(array $groups)
    {
        $this->paramGroups = $groups;
    }

    public function getTags()
    {
        return $this->tags;
    }

    public function setTags(array $tags)
    {
        $this->tags = $tags;
    }

    public function getCustomAttributes()
    {
        return $this->customAttributes;
    }

    public function setCustomAttributes(array $attr)
    {
        $this->customAttributes = $attr;
    }

    public function getRevisionNumber()
    {
        return $this->revisionNumber;
    }

    public function &getResources()
    {
        return $this->resources;
    }

    public function getCreatedTime()
    {
        return floor($this->createdTime / 1000);
    }

    public function getModifiedTime()
    {
        return floor($this->modifiedTime / 1000);
    }


    /**
     * Takes values from an array and populates a Revision with them.
     *
     * @param Revision $model
     * @param array $array
     */
    public static function fromArray(Revision $revision, array $array)
    {
        foreach ($array as $key => $value) {
            if (property_exists($revision, $key)) {
                $revision->$key = $value;
            }
        }
        // Fill in sub-objects
        if (!empty($revision->resources)) {
            foreach ($revision->resources as &$resource) {
                if (is_array($resource)) {
                    $resourceObj = new Resource($revision->getConfig(), $revision->apiId, $revision->id);
                    Resource::fromArray($resourceObj, $resource);
                    $resource = $resourceObj;
                }
            }
        }
    }

    /**
     * Persists the current Revision as an array.
     *
     * @return array
     */
    public function toArray($verbose = TRUE)
    {
        $payload_keys = array(
            'displayName', 'description', 'releaseVersion', 'changeLog', 'apiSchema',
            'baseUrl', 'params', 'paramGroups', 'tags', 'customAttributes',
        );
        if ($verbose) {
            $payload_keys = array_merge($payload_keys, array(
                'id', 'revisionNumber', 'createdTime', 'modifiedTime', 'apiId',
                'createdBy', 'modifiedBy'
            ));
        }
        $payload = array();
        foreach ($payload_keys as $key) {
            $payload[$key] = $this->$key;
        }
        if ($verbose && count($this->resources) > 0) {
            $payload['resources'] = array();
            foreach ($this->resources as $resource) {
                $payload['resources'][] = $resource->toArray();
            }
        }
        return $payload;
    }

    /**
     * Initializes this object.
     *
     * @param OrgConfig $config
     * @param string $modelId
     * @param string $modelName
     */
    public function __construct(OrgConfig $config, $modelId, $modelName = "")
    {
        $this->blankValues();
        $this->apiId = $modelId;
        $this->apiName = $modelName;
//        $this->init($config, '/o/' . rawurlencode($config->orgName) . '/apimodels/' . rawurlencode($this->apiId) . '/revisions');
      $this->init($config, '/o/' . rawurlencode($config->orgName) . '/apimodels/');
    }

    /**
     * Returns an array of all revisions for this model. The order of items in
     * the array may be non-deterministic.
     *
     * @return array
     */
    public function listRevisions()
    {
        $revisions = array();
        $this->get();
        foreach ($this->responseObj as $key => $blob) {
            $revision = new Revision($this->getConfig(), $this->apiId, $this->apiName);
            self::fromArray($revision, $blob);
            $revisions[$key] = $revision;
        }
        return $revisions;
    }

    /**
     * Loads a revision.
     *
     * @param string|int|null $revisionId
     *        This may be a UUID or a revision number.
     * @throws ParameterException
     */
    public function load($revisionId = null)
    {
        $revisionId = $revisionId ?: $this->id;
        if (empty($revisionId)) {
            throw new ParameterException('Cannot load a revision with no Revision UUID.');
        }
        if (is_int($revisionId) && $revisionId < 1) {
            throw new ParameterException('Cannot load a revision number less than 1.');
        }
        $this->get(rawurlencode($this->apiId) . '/revisions/' . $revisionId . '?expand=true');
        self::fromArray($this, $this->responseObj);
    }

    /**
     * Saves the current revision. If $update is true we presume that this is
     * an update (PUT) operation; otherwise it's an insert (POST).
     *
     * @todo Validate that required fields are not empty.
     *
     * @param bool $update
     */
    public function save($update = FALSE)
    {
        $payload = $this->toArray(FALSE);
        $keys = array_keys($payload);
        foreach ($keys as $key) {
            if (empty($payload[$key])) {
                unset($payload[$key]);
            }
        }
        if ($update) {
            $url = $this->id;
            $method = 'put';
        } else {
            $url = '';
            $method = 'post';
        }
        $this->$method($url, $payload);
        self::fromArray($this, $this->responseObj);
    }

    /**
     * Deletes a revision.
     *
     * @param string|int|null $revisionUuid
     *        This may be a UUID or a revision number.
     * @throws ParameterException
     */
    public function delete($revisionId = null)
    {
        $revisionId = $revisionId ?: $this->id;
        if (empty($revisionId)) {
            throw new ParameterException('Cannot delete a revision with no Revision UUID.');
        }
        $this->http_delete($revisionId);
        // TODO: should we do this, or call blankValues()?
        self::fromArray($this, $this->responseObj);
    }

    /**
     * Imports a model revision from a Swagger URL.
     *
     * @param string $modelId
     * @param string $swaggerUrl
     */
    public function importSwagger($swaggerUrl)
    {
        $combinedString = rawurlencode($this->apiId) . '/revisions?action=import&format=swagger';
        $this->blankValues();
        $this->post($combinedString, 'URL=' . $swaggerUrl, 'text/plain; charset=utf-8');
        $response = $this->responseObj;
        self::fromArray($this, $response);
    }

  /**
   * Imports a model revision from a WADL document.
   *
   * @param string $modelId
   * @param string $xml
   */
  public function importSwaggerFile($yaml)
  {
    $combinedString = $this->apiName . '/import/file?format=swagger';
    $this->blankValues();
    $this->post($combinedString, $yaml, 'application/yaml; charset=utf-8');
    $response = $this->responseObj;
    self::fromArray($this, $response);
  }

    /**
     * Imports a model revision from a WADL document.
     *
     * @param string $modelId
     * @param string $xml
     */
    public function importWadl($xml)
    {
        $combinedString = rawurlencode($this->apiName) . '/revisions?action=import&format=wadl';
        $this->blankValues();
        $this->post($combinedString, $xml, 'application/xml; charset=utf-8');
        $response = $this->responseObj;
        self::fromArray($this, $response);
    }
    public function importWadlUrl($wadlUrl)
    {
      $combinedString = rawurlencode($this->apiName) . '/revisions?action=import&format=wadl';
      $this->blankValues();
      $this->post($combinedString, 'URL=' . $wadlUrl, 'application/xml; charset=utf-8', 'application/xml; charset=utf-8');
        $response = $this->responseObj;
        self::fromArray($this, $response);
    }

    /**
     * Imports a model revision from an Apigee Internal JSON document.
     *
     * @param string $modelId
     * @param string $json
     */
    public function importApigeeJson($json)
    {
        $combinedString = rawurlencode($this->apiName) . '/revisions?action=import&format=apimodel';
        $this->blankValues();
        $this->post($combinedString, $json, 'application/json; charset=utf-8');
        $response = $this->responseObj;
        self::fromArray($this, $response);
    }

    /**
     * Exports a SmartDocs revision as JSON (default) or an XML-based format.
     *
     * @param string $modelId
     * @param string $format
     * @param int|null $revision
     *
     * @return string
     */
    public function export($format, $revision = NULL)
    {
        $revision = $revision ?: 'latest';
        if ($format == 'json' || empty($format)) {
            $this->get(rawurlencode($this->apiName) . '/revisions/' . $revision . '?expand=yes');
        } else {
            $this->get(rawurlencode($this->apiName) . '/revisions/' . $revision . '?expand=yes&format=' . $format, 'text/xml');
        }
{'fraction_non_alphanumeric': 0.14366069962226966, 'fraction_numerical': 0.0017244210872064379, 'mean_word_length': 2.0294776119402984, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 213, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3802680', 'n_tokens_mistral': 7312, 'n_tokens_neox': 7078, 'n_words': 1640}