Abstract Class yii\authclient\BaseClient
BaseClient is a base Auth Client class.
See also yii\authclient\ClientInterface.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$httpClient | \yii\httpclient\Client | Internal HTTP client. | yii\authclient\BaseClient |
$id | string | Service id. | yii\authclient\BaseClient |
$name | string | Service name. | yii\authclient\BaseClient |
$normalizeUserAttributeMap | array | Normalize user attribute map. | yii\authclient\BaseClient |
$requestOptions | array | HTTP request options. | yii\authclient\BaseClient |
$stateStorage | yii\authclient\StateStorageInterface | Stage storage. | yii\authclient\BaseClient |
$title | string | Service title. | yii\authclient\BaseClient |
$userAttributes | array | List of user attributes. | yii\authclient\BaseClient |
$viewOptions | array | View options in format: optionName => optionValue. | yii\authclient\BaseClient |
Public Methods
Protected Methods
Property Details
Internal HTTP client. Note that the type of this property differs in getter and setter. See getHttpClient() and setHttpClient() for details.
Normalize user attribute map.
HTTP request options. This property is read-only.
Stage storage. Note that the type of this property differs in getter and setter. See getStateStorage() and setStateStorage() for details.
View options in format: optionName => optionValue.
Method Details
Creates HTTP client instance from reference or configuration.
protected \yii\httpclient\Client createHttpClient ( $reference ) | ||
$reference | string|array |
Component name or array configuration. |
return | \yii\httpclient\Client |
HTTP client instance. |
---|
protected function createHttpClient($reference)
{
return Instance::ensure($reference, Client::className());
}
Creates HTTP request instance.
public \yii\httpclient\Request createRequest ( ) | ||
return | \yii\httpclient\Request |
HTTP request instance. |
---|
public function createRequest()
{
return $this->getHttpClient()
->createRequest()
->addOptions($this->defaultRequestOptions())
->addOptions($this->getRequestOptions());
}
Generates service name.
protected string defaultName ( ) | ||
return | string |
Service name. |
---|
protected function defaultName()
{
return Inflector::camel2id(StringHelper::basename(get_class($this)));
}
Returns the default $normalizeUserAttributeMap value.
Particular client may override this method in order to provide specific default map.
protected array defaultNormalizeUserAttributeMap ( ) | ||
return | array |
Normalize attribute map. |
---|
protected function defaultNormalizeUserAttributeMap()
{
return [];
}
Returns default HTTP request options.
protected array defaultRequestOptions ( ) | ||
return | array |
HTTP request options. |
---|
protected function defaultRequestOptions()
{
return [
'timeout' => 30,
'sslVerifyPeer' => false,
];
}
Generates service title.
protected string defaultTitle ( ) | ||
return | string |
Service title. |
---|
protected function defaultTitle()
{
return StringHelper::basename(get_class($this));
}
Returns the default $viewOptions value.
Particular client may override this method in order to provide specific default view options.
protected array defaultViewOptions ( ) | ||
return | array |
List of default $viewOptions |
---|
protected function defaultViewOptions()
{
return [];
}
Returns HTTP client.
public \yii\httpclient\Client getHttpClient ( ) | ||
return | \yii\httpclient\Client |
Internal HTTP client. |
---|
public function getHttpClient()
{
if (!is_object($this->_httpClient)) {
$this->_httpClient = $this->createHttpClient($this->_httpClient);
}
return $this->_httpClient;
}
public string getId ( ) | ||
return | string |
Service id |
---|
public function getId()
{
if (empty($this->_id)) {
$this->_id = $this->getName();
}
return $this->_id;
}
public string getName ( ) | ||
return | string |
Service name. |
---|
public function getName()
{
if ($this->_name === null) {
$this->_name = $this->defaultName();
}
return $this->_name;
}
public array getNormalizeUserAttributeMap ( ) | ||
return | array |
Normalize user attribute map. |
---|
public function getNormalizeUserAttributeMap()
{
if ($this->_normalizeUserAttributeMap === null) {
$this->_normalizeUserAttributeMap = $this->defaultNormalizeUserAttributeMap();
}
return $this->_normalizeUserAttributeMap;
}
public array getRequestOptions ( ) | ||
return | array |
HTTP request options. |
---|
public function getRequestOptions()
{
return $this->_requestOptions;
}
Returns persistent state value.
protected mixed getState ( $key ) | ||
$key | string |
State key. |
return | mixed |
State value. |
---|
protected function getState($key)
{
return $this->getStateStorage()->get($this->getStateKeyPrefix() . $key);
}
Returns session key prefix, which is used to store internal states.
protected string getStateKeyPrefix ( ) | ||
return | string |
Session key prefix. |
---|
protected function getStateKeyPrefix()
{
return get_class($this) . '_' . $this->getId() . '_';
}
public yii\authclient\StateStorageInterface getStateStorage ( ) | ||
return | yii\authclient\StateStorageInterface |
Stage storage. |
---|
public function getStateStorage()
{
if (!is_object($this->_stateStorage)) {
$this->_stateStorage = Yii::createObject($this->_stateStorage);
}
return $this->_stateStorage;
}
public string getTitle ( ) | ||
return | string |
Service title. |
---|
public function getTitle()
{
if ($this->_title === null) {
$this->_title = $this->defaultTitle();
}
return $this->_title;
}
public array getUserAttributes ( ) | ||
return | array |
List of user attributes |
---|
public function getUserAttributes()
{
if ($this->_userAttributes === null) {
$this->_userAttributes = $this->normalizeUserAttributes($this->initUserAttributes());
}
return $this->_userAttributes;
}
public array getViewOptions ( ) | ||
return | array |
View options in format: optionName => optionValue |
---|
public function getViewOptions()
{
if ($this->_viewOptions === null) {
$this->_viewOptions = $this->defaultViewOptions();
}
return $this->_viewOptions;
}
Initializes authenticated user attributes.
protected abstract array initUserAttributes ( ) | ||
return | array |
Auth user attributes. |
---|
abstract protected function initUserAttributes();
Normalize given user attributes according to $normalizeUserAttributeMap.
protected array normalizeUserAttributes ( $attributes ) | ||
$attributes | array |
Raw attributes. |
return | array |
Normalized attributes. |
---|---|---|
throws | \yii\base\InvalidConfigException |
on incorrect normalize attribute map. |
protected function normalizeUserAttributes($attributes)
{
foreach ($this->getNormalizeUserAttributeMap() as $normalizedName => $actualName) {
if (is_scalar($actualName)) {
if (array_key_exists($actualName, $attributes)) {
$attributes[$normalizedName] = $attributes[$actualName];
}
} else {
if (is_callable($actualName)) {
$attributes[$normalizedName] = call_user_func($actualName, $attributes);
} elseif (is_array($actualName)) {
$haystack = $attributes;
$searchKeys = $actualName;
$isFound = true;
while (($key = array_shift($searchKeys)) !== null) {
if (is_array($haystack) && array_key_exists($key, $haystack)) {
$haystack = $haystack[$key];
} else {
$isFound = false;
break;
}
}
if ($isFound) {
$attributes[$normalizedName] = $haystack;
}
} else {
throw new InvalidConfigException('Invalid actual name "' . gettype($actualName) . '" specified at "' . get_class($this) . '::normalizeUserAttributeMap"');
}
}
}
return $attributes;
}
Removes persistent state value.
protected boolean removeState ( $key ) | ||
$key | string |
State key. |
return | boolean |
Success. |
---|
protected function removeState($key)
{
return $this->getStateStorage()->remove($this->getStateKeyPrefix() . $key);
}
Sets HTTP client to be used.
public void setHttpClient ( $httpClient ) | ||
$httpClient | array|\yii\httpclient\Client |
Internal HTTP client. |
public function setHttpClient($httpClient)
{
$this->_httpClient = $httpClient;
}
public void setName ( $name ) | ||
$name | string |
Service name. |
public function setName($name)
{
$this->_name = $name;
}
public void setNormalizeUserAttributeMap ( $normalizeUserAttributeMap ) | ||
$normalizeUserAttributeMap | array |
Normalize user attribute map. |
public function setNormalizeUserAttributeMap($normalizeUserAttributeMap)
{
$this->_normalizeUserAttributeMap = $normalizeUserAttributeMap;
}
public void setRequestOptions ( array $options ) | ||
$options | array |
HTTP request options. |
public function setRequestOptions(array $options)
{
$this->_requestOptions = $options;
}
Sets persistent state.
protected $this setState ( $key, $value ) | ||
$key | string |
State key. |
$value | mixed |
State value |
return | $this |
The object itself |
---|
protected function setState($key, $value)
{
$this->getStateStorage()->set($this->getStateKeyPrefix() . $key, $value);
return $this;
}
public void setStateStorage ( $stateStorage ) | ||
$stateStorage | yii\authclient\StateStorageInterface|array|string |
Stage storage to be used. |
public function setStateStorage($stateStorage)
{
$this->_stateStorage = $stateStorage;
}
public void setTitle ( $title ) | ||
$title | string |
Service title. |
public function setTitle($title)
{
$this->_title = $title;
}
public void setUserAttributes ( $userAttributes ) | ||
$userAttributes | array |
List of user attributes |
public function setUserAttributes($userAttributes)
{
$this->_userAttributes = $this->normalizeUserAttributes($userAttributes);
}
public void setViewOptions ( $viewOptions ) | ||
$viewOptions | array |
View options in format: optionName => optionValue |
public function setViewOptions($viewOptions)
{
$this->_viewOptions = $viewOptions;
}