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 * Generic base class for ASN.1 String types.
26 *
27 * @package asn1
28 */
29abstract class ASN1String extends ASN1Object {
30
31    protected $value;
32
33    /**
34     * Constructs a new instance of ASN1String.
35     *
36     * @param  string $value
37     */
38    public function __construct($value = null) {
39
40        if (!is_null($value)) {
41            $this->setValue($value);
42        }
43
44    }
45
46    /**
47     * Encodes the contents of this ASN1String.
48     *
49     * @return array DER encoding for this string
50     */
51    public function encodeDER() {
52
53        $bytes = GTUtil::toByteArray($this->value);
54
55        $this->prepend($bytes, ASN1DER::encodeLength(count($bytes)));
56        $this->prepend($bytes, ASN1DER::encodeType($this->getType()));
57
58        return $bytes;
59    }
60
61    /**
62     * Decodes an ASN1String from the given byte stream.
63     *
64     * @param  $bytes V bytes of this ASN1String TLV
65     * @return void
66     */
67    public function decodeDER($bytes) {
68        $this->setValue(GTUtil::fromByteArray($bytes));
69    }
70
71    /**
72     * Sets the value of this ASN1String.
73     *
74     * @param  string $value
75     * @return void
76     */
77    protected function setValue($value) {
78        $this->value = $value;
79    }
80
81    /**
82     * Gets the value of this ASN1String.
83     *
84     * @return string
85     */
86    public function getValue() {
87        return $this->value;
88    }
89
90    /**
91     * Gets the actual ASN.1 type to use for encoding/decoding this ASN1String.
92     * @abstract
93     * @return int
94     */
95    protected abstract function getType();
96
97}
98
99?>
100