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

/**
 * An implementation of RFC 4648 Base-64 encoding.
 *
 * @link http://tools.ietf.org/html/rfc4648#section-4 RFC 4648
 * @see GTBaseX, GTBase16, GTBase32
 *
 * @package util
 */
class GTBase64 {

    private static $instance;

    /**
     * Encodes the given bytes into a base-64 string.
     *
     * @static
     * @param  array $bytes an array containing the bytes to encode.
     * @param  int $offset the start offset of the data within bytes
     * @param  int $length the number of bytes to encode
     * @return string the base-64 string
     */
    public static function encode(array $bytes, $offset = null, $length = null) {
        return self::getInstance()->encode($bytes, $offset, $length);
    }

    /**
     * Decodes the given base-64 string into a byte array.
     *
     * @static
     * @param  string $string base-64 encoded string
     * @return array array containing the decoded bytes
     */
    public static function decode($string) {
        return self::getInstance()->decode($string);
    }

    /**
     * Gets singleton instance of GTBaseX configured for base-64 encoding/decoding.
     *
     * @static
     * @return GTBaseX singleton instance of GTBaseX
     */
    private static function getInstance() {

        if (self::$instance == null) {
            self::$instance = new GTBaseX("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", true, '=');
        }

        return self::$instance;
    }

}

?>
