<?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 CertToken implementation.
 *
 * <pre>
 * CertToken ::= SEQUENCE {
 *    version         INTEGER { v1(1) },
 *    history         OCTET STRING,
 *    publishedData   PublishedData,
 *    pubReference    SET OF OCTET STRING
 *    extensions      [0] Extensions OPTIONAL
 * }
 * </pre>
 *
 * @package asn1
 * @subpackage gt
 */
class GTCertToken {

    private $version = 1;
    private $history;
    private $publishedData;
    private $pubReference;

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

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

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

        if ($object->getObjectCount() < 4 || $object->getObjectCount() > 5) {
            throw new GTException("Invalid sequence size: " . $object->getObjectCount());
        }

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

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

        $this->history = $object->getObjectAt(1)->getValue();

        $publishedData = new GTPublishedData();
        $publishedData->decode($object->getObjectAt(2));

        $this->publishedData = $publishedData;

        $pubReference = array();

        foreach ($object->getObjectAt(3)->getObjects() as $item) {
            array_push($pubReference, $item->getValue());
        }

        $this->pubReference = $pubReference;

        if ($object->getObjectCount() == 5) {
            throw new GTException("CertToken.extensions not yet implemented");
        }
    }

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

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

    /**
     * Gets the published data.
     *
     * @return GTPublishedData published data
     */
    public function getPublishedData() {
        return $this->publishedData;
    }

    /**
     * Gets the pub reference.
     *
     * @return array array of byte arrays
     */
    public function getPubReference() {
        return $this->pubReference;
    }

}

?>
