<?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 x509
 */

/**
 * X.509 AlgorithmIdentifier implementation.
 *
 * <pre>
 * AlgorithmIdentifier  ::=  SEQUENCE  {
 *  algorithm   OBJECT IDENTIFIER,
 *  parameters  ANY DEFINED BY algorithm OPTIONAL
 * }
 * </pre>
 *
 * @package asn1
 * @subpackage x509
 */
class X509AlgorithmIdentifier implements ASN1DEREncodable {

    private $algorithm;

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

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

        if (!$object instanceof ASN1Sequence) {
            throw new GTException("object must be an instance of ASN1Sequence");
        }

        if ($object->getObjectCount() < 1) {
            throw new GTException("sequence must contain at least 1 object");
        }

        if ($object->getObjectCount() > 2) {
            throw new GTException("sequence must not contain more than 2 objects");
        }

        if ($object->getObjectCount() == 2 && !($object->getObjectAt(1) instanceof ASN1Null)) {
            throw new GTException("parameters not implemented");
        }

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

        if (!$algorithm instanceof ASN1ObjectId) {
            throw new GTException("algorithm must be an instance of ASN1ObjectId");
        }

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

    }

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

        $sequence = new ASN1Sequence();
        $sequence->add(new ASN1ObjectId($this->algorithm));
        $sequence->add(new ASN1Null());

        return $sequence->encodeDER();
    }

    /**
     * Sets the algorithm.
     *
     * @param  string $algorithm oid
     * @return void
     */
    public function setAlgorithm($algorithm) {
        $this->algorithm = $algorithm;

    }

    /**
     * Gets the algorithm.
     *
     * @return string
     */
    public function getAlgorithm() {
        return $this->algorithm;

    }

}

?>
