<?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 PublishedData implementation.
 *
 * <pre>
 * PublishedData ::= SEQUENCE {
 *    publicationIdentifier   INTEGER,
 *    publicationImprint      DataImprint
 * }
 * </pre>
 *
 * <pre>
 * DataImprint ::= OCTET STRING
 * </pre>
 *
 * @package asn1
 * @subpackage gt
 */
class GTPublishedData implements ASN1DEREncodable {

    private $publicationIdentifier;
    private $publicationImprint;

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

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

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

        $size = $object->getObjectCount();

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

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

        if (!$publicationIdentifier instanceof ASN1Integer) {
            throw new GTException("Expecting an ASN1Integer for GTPublishedData.publicationIdentifier");
        }

        $publicationImprint = $object->getObjectAt(1);

        if (!$publicationImprint instanceof ASN1OctetString) {
            throw new GTException("Expecting an ASN1Integer for GTPublishedData.publicationImprint");
        }

        $this->publicationIdentifier = $publicationIdentifier->getValue();
        $this->publicationImprint = $publicationImprint->getValue();

        GTDataHash::getInstance($this->publicationImprint);
    }

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

        $sequence = new ASN1Sequence();

        $sequence->add(new ASN1Integer(new GTBigInteger($this->publicationIdentifier)));
        $sequence->add(new ASN1OctetString($this->publicationImprint));

        return $sequence->encodeDER();
    }

    /**
     * Gets the encoded publication.
     *
     * @return string encoded publication.
     */
    public function getEncodedPublication() {

        $int = new GTBigInteger($this->publicationIdentifier);

        $bytes = $int->toBytes();

        // pad to 64 bit
        while (count($bytes) % 8 != 0) {
            array_unshift($bytes, 0);
        }

        foreach ($this->publicationImprint as $byte) {
            array_push($bytes, $byte);
        }

        $bytes = GTUtil::addCrc32($bytes);

        return GTBase32::encodeWithDashes($bytes);
    }

    /**
     * Gets the publication identifier.
     *
     * @return string publication identifier
     */
    public function getPublicationIdentifier() {
        return $this->publicationIdentifier;
    }

    /**
     * Gets the publication imprint.
     *
     * @return array byte array containing the publication imprint bytes
     */
    public function getPublicationImprint() {
        return $this->publicationImprint;
    }

}

?>
