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

/**
 * ASN.1 Octet String (byte array) implementation.
 *
 * @package asn1
 */
class ASN1OctetString extends ASN1Object {

    protected $value;

    /**
     * Constructs a new ASN1OctetString.
     *
     * @throws GTException
     * @param  array $value byte array
     */
    public function __construct($value = null) {

        if (!is_null($value)) {

            if (!is_array($value)) {
                throw new GTException("value must be an array of bytes");
            }

            $this->value = $value;
        }
    }

    /**
     * Gets the byte array stored as value.
     *
     * @return array byte array
     */
    public function getValue() {
        return $this->value;
    }

    /**
     * Encodes the contents of this ASN1 Octet String.
     *
     * @return array DER encoding of this octet string
     */
    public function encodeDER() {

        $bytes = array();

        $this->append($bytes, ASN1DER::encodeType(ASN1_TAG_OCTET_STRING));
        $this->append($bytes, ASN1DER::encodeLength(count($this->value)));
        $this->append($bytes, $this->value);

        return $bytes;
    }

    /**
     * Decodes an ASN.1 Octet String from the given byte stream.
     *
     * @param  $bytes V bytes of this ASN.1 Octet String TLV
     * @return void
     */
    public function decodeDER($bytes) {
        $this->value = $bytes;
    }
}

?>
