<?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 GeneralName implementation.
 *
 * <pre>
 * GeneralName ::= CHOICE {
 *  otherName                       [0]     OtherName,
 *  rfc822Name                      [1]     IA5String,
 *  dNSName                         [2]     IA5String,
 *  x400Address                     [3]     ORAddress,
 *  directoryName                   [4]     Name,
 *  ediPartyName                    [5]     EDIPartyName,
 *  uniformResourceIdentifier       [6]     IA5String,
 *  iPAddress                       [7]     OCTET STRING,
 *  registeredID                    [8]     OBJECT IDENTIFIER}
 * </pre>
 *
 * <pre>
 * OtherName ::= SEQUENCE {
 *  type-id    OBJECT IDENTIFIER,
 *  value      [0] EXPLICIT ANY DEFINED BY type-id }
 * </pre>
 *
 * <pre>
 * EDIPartyName ::= SEQUENCE {
 *  nameAssigner            [0]     DirectoryString OPTIONAL,
 *  partyName               [1]     DirectoryString
 * }
 * </pre>
 *
 * <pre>
 * Name ::= CHOICE { RDNSequence }
 * </pre>
 *
 * @package asn1
 * @subpackage x509
 */
class X509GeneralName {

    private $object;

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

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

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

        if ($object->getTagValue() < 0 || $object->getTagValue() > 8) {
            throw new GTException("Unknown TAG value: {$object->getTagValue()}");
        }

        $this->object = $object;

    }

    /**
     * Gets the formatted name.
     *
     * @throws GTException
     * @return string formatted name used for timestamps.
     */
    public function getFormatted() {

        if ($this->object == null) {
            return null;
        }

        switch ($this->object->getTagValue()) {

            case 0:
                throw new GTException("[0] not implemented");

            case 1:
                return "RFC:" . $this->object->getObjectAs(ASN1_TAG_IA5_STRING)->getValue();

            case 2:
                return "DNS:" . $this->object->getObjectAs(ASN1_TAG_IA5_STRING)->getValue();

            case 3:
                throw new GTException("[3] not implemented");

            case 4:
                throw new GTException("[4] not implemented");

            case 5:
                throw new GTException("[5] not implemented");

            case 6:
                return "URI:" . $this->object->getObjectAs(ASN1_TAG_IA5_STRING)->getValue();

            case 7:
                throw new GTException("[7] not implemented");

            case 8:
                return "RID: " . $this->object->getObjectAs(ASN1_TAG_OBJECT_ID)->getValue();

            default:
                throw new GTException("Unknown TAG value {$this->object->getTagValue()}");

        }

    }

}

?>
