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

/**
 * CMS Attribute implementation.
 *
 * <pre>
 * Attribute ::= SEQUENCE {
 *      attrType OBJECT IDENTIFIER,
 *      attrValues SET OF AttributeValue
 * }
 * </pre>
 *
 * <pre>
 * AttributeValue ::= ANY
 * </pre>
 *
 * @package asn1
 * @subpackage cms
 *
 * @link http://tools.ietf.org/html/rfc3852#section-5.3 RFC 3852: Cryptographic Message Syntax
 */
class CMSAttribute implements ASN1DEREncodable {

    private $type;
    private $values;

    /**
     * Construct a new instance of CMSAttribute.
     */
    public function __construct() {
    }

    /**
     * Decodes the given ASN1Sequence as CMSAttribute.
     *
     * @throws GTException
     * @param  ASN1Sequence $object CMSAttribute encoded as an 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}");
        }

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

        if (!$type instanceof ASN1ObjectId) {
            throw new GTException("Expecting an ASN1ObjectId");
        }

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

        $this->values = array();

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

        if (!$values instanceof ASN1Set) {
            throw new GTException("Expecting an ASN1Set");
        }

        $size = $values->getObjectCount();

        if ($size < 1) {
            throw new GTException("Missing attribute values");
        }

        for ($i = 0; $i < $size; $i++) {
            array_push($this->values, $values->getObjectAt(0));
        }

    }

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

        $values = new ASN1Set();

        foreach ($this->values as $value) {
            $values->add($value);
        }

        $sequence = new ASN1Sequence();
        $sequence->add(new ASN1ObjectId($this->type));
        $sequence->add($values);

        return $sequence->encodeDER();
    }

    /**
     * Gets the attribute type.
     *
     * @return string object id
     */
    public function getType() {
        return $this->type;
    }

    /**
     * Gets the attribute values.
     *
     * @return array attribute values
     */
    public function getValues() {
        return $this->values;
    }

}

?>
