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

/**
 * Base class for GuardTime verification results.
 *
 * @package tsp
 */
abstract class GTVerifificationResultBase {

    const NO_CHECKS = 0;
    const NO_FAILURES = 0;

    private $errorCode;
    private $statusCode;

    /**
     * Construct a new instance of GTVerificationResultBase.
     *
     * @param int $statusCode verification status code bitfield
     * @param int $errorCode verification error code bitfield
     */
    public function __construct($statusCode = null, $errorCode = null) {

        if (is_null($statusCode)) {
            $statusCode = self::NO_CHECKS;
        }

        if (is_null($errorCode)) {
            $errorCode = self::NO_FAILURES;
        }

        $this->statusCode = $statusCode;
        $this->errorCode = $errorCode;
    }

    /**
     * Gets the status bitfield for this verification result.
     *
     * @return int status bitfield
     */
    public function getStatusCode() {
        return $this->statusCode;
    }

    /**
     * Gets the error bitfield for this verification result.
     *
     * @return int error bitfield
     */
    public function getErrorCode() {
        return $this->errorCode;
    }

    /**
     * Checks if this verification result has specified status code set.
     *
     * @param  int $statusCode the status code to check for
     * @return bool true if this verification result has specified status code set
     */
    public function hasStatus($statusCode) {
        return ($statusCode & $this->statusCode) > 0;
    }

    /**
     * Checks if this verification result has specified error code set.
     *
     * @param  int $errorCode the error code to check for
     * @return bool
     */
    public function hasError($errorCode) {
        return ($errorCode & $this->errorCode) > 0;
    }

    /**
     * Checks if this verification result is valid.
     *
     * @return bool true if no error codes are set
     */
    public function isValid() {
        return $this->errorCode == self::NO_FAILURES;
    }

    /**
     * Updates this verification result status bitfield with specified status code.
     *
     * @param  int $statusCode the status code to set
     * @return void
     */
    public function updateStatus($statusCode) {
        $this->statusCode |= $statusCode;
    }

    /**
     * Updates this verification result error bitfield with specified error code.
     *
     * @param  int $errorCode the error code to set
     * @return void
     */
    public function updateErrors($errorCode) {
        $this->errorCode |= $errorCode;
    }

}

?>
