<?php
/*
 * Copyright 2008-2010 GuardTime AS
 *
 * This file is part of the GuardTime PHP SDK.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @package asn1
 * @subpackage gt
 */

/**
 * GT VerificationRequest implementation.
 *
 * <pre>
 * VerificationRequest :: = SEQUENCE {
 *      version     INTEGER { v1(1) },
 *      content     ContentInfo,
 *      data        OCTET STRING
 * }
 * </pre>
 *
 * @package asn1
 * @subpackage gt
 */
class GTVerificationRequest implements ASN1DEREncodable {

    private $version;
    private $content;
    private $data;

    /**
     * Constructs a new instance of GTVerificationRequest.
     *
     */
    public function __construct() {
    }

    /**
     * Decodes the given ASN1Sequence as GTVerificationRequest.
     *
     * @throws GTException
     * @param  ASN1Sequence $object GTVerificationRequest encoded as ASN1Sequence
     * @return void
     */
    public function decode($object) {

        if (!$object instanceof ASN1Sequence) {
            throw new GTException("Expecting an ASN1Sequence");
        }

        $size = $object->getObjectCount();

        if ($size != 3) {
            throw new GTException("Invalid sequence size: {$size}");
        }

        $version = $object->getObjectAt(0);

        if (!$version instanceof ASN1Integer) {
            throw new GTException("Expecting an ASN1Integer as version");
        }

        $this->version = (int) $version->getValue();

        if ($this->version != 1) {
            throw new GTException("Invalid value for version: {$this->version}");
        }

        $content = new CMSContentInfo();
        $content->decode($object->getObjectAt(1));

        $this->content = $content;

        $data = $object->getObjectAt(2);

        if (!$data instanceof ASN1OctetString) {
            throw new GTException("Expecting an ASN1OctetString as data");
        }

        $this->data = $data->getValue();

    }

    /**
     * Encodes this GTVerificationRequest using DER.
     *
     * @return array byte array that contains the DER encoding of this GTVerificationRequest
     */
    public function encodeDER() {

        $sequence = new ASN1Sequence();
        $sequence->add(new ASN1Integer($this->version));
        $sequence->add($this->content);
        $sequence->add(new ASN1OctetString($this->data));

        return $sequence->encodeDER();
    }

    /**
     * Gets the version.
     *
     * @return int version
     */
    public function getVersion() {
        return $this->version;
    }

    /**
     * Sets the version.
     *
     * @param  int $version the version
     * @return void
     */
    public function setVersion($version) {
        $this->version = $version;
    }

    /**
     * Gets the content (timestamp).
     *
     * @return CMSContentInfo timestamp
     */
    public function getContent() {
        return $this->content;
    }

    /**
     * Sets the content (timestamp).
     *
     * @param  CMSContentInfo $content timestamp
     * @return void
     */
    public function setContent($content) {
        $this->content = $content;
    }

    /**
     * Gets the data.
     *
     * @return array byte array containing the data bytes.
     */
    public function getData() {
        return $this->data;
    }

    /**
     * Sets the data.
     *
     * @param  array $data byte array containing the data bytes.
     * @return void
     */
    public function setData($data) {
        $this->data = $data;
    }

}

?>
