<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

namespace Crypt\GPG;

use Crypt\GPG\SubKey;
use Crypt\GPG\UserId;

/**
 * A data class for GPG key information
 *
 * This class is used to store the results of the {@link Crypt\GPG::getKeys()} method.
 *
 * @category  Encryption
 * @package   Crypt_GPG
 * @author    Michael Gauthier <mike@silverorange.com>
 * @copyright 2008-2010 silverorange
 * @license   https://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 * @link      https://github.com/pear/Crypt_GPG
 */
class Key
{
    /**
     * The user ids associated with this key
     *
     * This is an array of {@link \Crypt\GPG\UserId} objects.
     *
     * @var array<UserId>
     *
     * @see Key::addUserId()
     * @see Key::getUserIds()
     */
    private $_userIds = [];

    /**
     * The subkeys of this key
     *
     * This is an array of {@link \Crypt\GPG\SubKey} objects.
     *
     * @var array<SubKey>
     *
     * @see Key::addSubKey()
     * @see Key::getSubKeys()
     */
    private $_subKeys = [];

    /**
     * Gets the sub-keys of this key
     *
     * @return array<SubKey> the sub-keys of this key.
     *
     * @see Key::addSubKey()
     */
    public function getSubKeys(): array
    {
        return $this->_subKeys;
    }

    /**
     * Gets the user ids of this key
     *
     * @return array<UserId> the user ids of this key.
     *
     * @see Key::addUserId()
     */
    public function getUserIds(): array
    {
        return $this->_userIds;
    }

    /**
     * Gets the primary sub-key of this key
     *
     * The primary key is the first added sub-key.
     *
     * @return ?SubKey the primary sub-key of this key.
     */
    public function getPrimaryKey(): ?SubKey
    {
        $primary_key = null;
        if (count($this->_subKeys) > 0) {
            $primary_key = $this->_subKeys[0];
        }
        return $primary_key;
    }

    /**
     * Gets whether or not this key can sign data
     *
     * This key can sign data if any sub-key of this key can sign data.
     *
     * @return bool True if this key can sign data and false if this key
     *              cannot sign data.
     */
    public function canSign(): bool
    {
        foreach ($this->_subKeys as $subKey) {
            if ($subKey->canSign()) {
                return true;
            }
        }

        return false;
    }

    /**
     * Gets whether or not this key can encrypt data
     *
     * This key can encrypt data if any sub-key of this key can encrypt data.
     *
     * @return bool True if this key can encrypt data and false if this
     *              key cannot encrypt data.
     */
    public function canEncrypt(): bool
    {
        foreach ($this->_subKeys as $subKey) {
            if ($subKey->canEncrypt()) {
                return true;
            }
        }

        return false;
    }

    /**
     * Adds a sub-key to this key
     *
     * The first added sub-key will be the primary key of this key.
     *
     * @param SubKey $subKey the sub-key to add.
     *
     * @return $this the current object, for fluent interface.
     */
    public function addSubKey(SubKey $subKey)
    {
        $this->_subKeys[] = $subKey;
        return $this;
    }

    /**
     * Adds a user id to this key
     *
     * @param UserId $userId the user id to add.
     *
     * @return $this the current object, for fluent interface.
     */
    public function addUserId(UserId $userId)
    {
        $this->_userIds[] = $userId;
        return $this;
    }

    /**
     * String representation of the key
     *
     * @return string The key ID.
     */
    public function __toString(): string
    {
        foreach ($this->_subKeys as $subKey) {
            if ($id = $subKey->getId()) {
                return $id;
            }
        }

        return '';
    }
}
