ClientTrait.php 2.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\authclient;

use Yii;
use yii\base\NotSupportedException;
use yii\helpers\StringHelper;

/**
 * Class ProviderTrait
 *
 * @see ClientInterface
 *
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
trait ClientTrait
{
	/**
	 * @var string service id.
	 * This value mainly used as HTTP request parameter.
	 */
	private $_id;
	/**
	 * @var string service unique name.
	 * This value may be used in database records, CSS files and so on.
	 */
	private $_name;
	/**
	 * @var string service title to display in views.
	 */
	private $_title;
	/**
	 * @var array authenticated user attributes.
	 */
	private $_userAttributes;

	/**
	 * @param string $id service id.
	 */
	public function setId($id)
	{
		$this->_id = $id;
	}

	/**
	 * @return string service id
	 */
	public function getId()
	{
		if (empty($this->_id)) {
			$this->_id = $this->getName();
		}
		return $this->_id;
	}

	/**
	 * @return string service name.
	 */
	public function getName()
	{
		if ($this->_name === null) {
			$this->_name = $this->defaultName();
		}
		return $this->_name;
	}

	/**
	 * @param string $name service name.
	 */
	public function setName($name)
	{
		$this->_name = $name;
	}

	/**
	 * @return string service title.
	 */
	public function getTitle()
	{
		if ($this->_title === null) {
			$this->_title = $this->defaultTitle();
		}
		return $this->_title;
	}

	/**
	 * @param string $title service title.
	 */
	public function setTitle($title)
	{
		$this->_title = $title;
	}

	/**
	 * @return array list of user attributes
	 */
	public function getUserAttributes()
	{
		if ($this->_userAttributes === null) {
			$this->_userAttributes = $this->initUserAttributes();
		}
		return $this->_userAttributes;
	}

	/**
	 * @param array $userAttributes list of user attributes
	 */
	public function setUserAttributes(array $userAttributes)
	{
		$this->_userAttributes = $userAttributes;
	}

	/**
	 * Generates service name.
	 * @return string service name.
	 */
	protected function defaultName()
	{
		return StringHelper::basename(get_class($this));
	}

	/**
	 * Generates service title.
	 * @return string service title.
	 */
	protected function defaultTitle()
	{
		return StringHelper::basename(get_class($this));
	}

	/**
	 * Initializes authenticated user attributes.
	 * @return array auth user attributes.
	 */
	protected function initUserAttributes()
	{
		throw new NotSupportedException('Method "' . get_class($this) . '::' . __FUNCTION__ . '" not implemented.');
	}
}