1<?php
2/*
3 * Copyright 2008-2010 GuardTime AS
4 *
5 * This file is part of the GuardTime PHP SDK.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/**
21 * @package asn1
22 */
23
24/**
25 * ASN.1 Set implementation.
26 *
27 * @package asn1
28 */
29class ASN1Set extends ASN1Object {
30
31    protected $objects = array();
32
33    /**
34     * Constructs a new ASN1Set instance.
35     */
36    public function __construct() {
37    }
38
39    /**
40     * Adds an ASN1Object to this set.
41     *
42     * @param  ASN1Object $object the object to add
43     * @return void
44     */
45    public function add($object) {
46        array_push($this->objects, $object);
47    }
48
49    /**
50     * Gets the object at index.
51     *
52     * @param  $index 0 based index
53     * @return ASN1Object object at index
54     */
55    public function getObjectAt($index) {
56        return $this->objects[$index];
57    }
58
59    /**
60     * Gets the number of objects stored inside this set.
61     *
62     * @return int the number of objects
63     */
64    public function getObjectCount() {
65        return count($this->objects);
66    }
67
68    /**
69     * Gets all objects stored inside this set.
70     *
71     * @return array objects stored inside this set.
72     */
73    public function getObjects() {
74        return $this->objects;
75    }
76
77    /**
78     * Encodes this ASN1Set and the objects contained within to DER.
79     *
80     * @return array DER encoding of this ASN1Set
81     */
82    public function encodeDER() {
83
84        $bytes = array();
85
86        foreach ($this->objects as $object) {
87            $this->append($bytes, $object->encodeDER());
88        }
89
90        $this->prepend($bytes, ASN1DER::encodeLength(count($bytes)));
91        $this->prepend($bytes, ASN1DER::encodeType(ASN1_TAG_SET));
92
93        return $bytes;
94    }
95
96    /**
97     * Decodes an ASN1Set from the given byte stream.
98     *
99     * @param  $bytes V bytes from the encoding of ASN1Set TLV
100     * @return void
101     */
102    public function decodeDER($bytes) {
103
104        while (count($bytes) > 0) {
105
106            $object = ASN1DER::decodeType($bytes);
107            $length = ASN1DER::decodeLength($bytes);
108
109            $object->decodeDER($this->readBytes($bytes, $length));
110
111            array_push($this->objects, $object);
112        }
113
114    }
115}
116
117?>
118