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 createHttpClient( string|array $reference ): \yii\httpclient\Client | ||
| $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 createRequest( ): \yii\httpclient\Request | ||
| return | \yii\httpclient\Request |
HTTP request instance. |
|---|---|---|
public function createRequest()
{
return $this->getHttpClient()
->createRequest()
->addOptions($this->defaultRequestOptions())
->addOptions($this->getRequestOptions());
}
Generates service name.
| protected defaultName( ): string | ||
| 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 defaultNormalizeUserAttributeMap( ): array | ||
| return | array |
Normalize attribute map. |
|---|---|---|
protected function defaultNormalizeUserAttributeMap()
{
return [];
}
Returns default HTTP request options.
| protected defaultRequestOptions( ): array | ||
| return | array |
HTTP request options. |
|---|---|---|
protected function defaultRequestOptions()
{
return [
'timeout' => 30,
'sslVerifyPeer' => false,
];
}
Generates service title.
| protected defaultTitle( ): string | ||
| 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 defaultViewOptions( ): array | ||
| return | array |
List of default $viewOptions |
|---|---|---|
protected function defaultViewOptions()
{
return [];
}
Returns HTTP client.
| public getHttpClient( ): \yii\httpclient\Client | ||
| 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 getId( ): string | ||
| return | string |
Service id |
|---|---|---|
public function getId()
{
if (empty($this->_id)) {
$this->_id = $this->getName();
}
return $this->_id;
}
| public getName( ): string | ||
| return | string |
Service name. |
|---|---|---|
public function getName()
{
if ($this->_name === null) {
$this->_name = $this->defaultName();
}
return $this->_name;
}
| public getNormalizeUserAttributeMap( ): array | ||
| return | array |
Normalize user attribute map. |
|---|---|---|
public function getNormalizeUserAttributeMap()
{
if ($this->_normalizeUserAttributeMap === null) {
$this->_normalizeUserAttributeMap = $this->defaultNormalizeUserAttributeMap();
}
return $this->_normalizeUserAttributeMap;
}
| public getRequestOptions( ): array | ||
| return | array |
HTTP request options. |
|---|---|---|
public function getRequestOptions()
{
return $this->_requestOptions;
}
Returns persistent state value.
| protected getState( string $key ): mixed | ||
| $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 getStateKeyPrefix( ): string | ||
| return | string |
Session key prefix. |
|---|---|---|
protected function getStateKeyPrefix()
{
return get_class($this) . '_' . $this->getId() . '_';
}
| public getStateStorage( ): yii\authclient\StateStorageInterface | ||
| return | yii\authclient\StateStorageInterface |
Stage storage. |
|---|---|---|
public function getStateStorage()
{
if (!is_object($this->_stateStorage)) {
$this->_stateStorage = Yii::createObject($this->_stateStorage);
}
return $this->_stateStorage;
}
| public getTitle( ): string | ||
| return | string |
Service title. |
|---|---|---|
public function getTitle()
{
if ($this->_title === null) {
$this->_title = $this->defaultTitle();
}
return $this->_title;
}
| public getUserAttributes( ): array | ||
| return | array |
List of user attributes |
|---|---|---|
public function getUserAttributes()
{
if ($this->_userAttributes === null) {
$this->_userAttributes = $this->normalizeUserAttributes($this->initUserAttributes());
}
return $this->_userAttributes;
}
| public getViewOptions( ): array | ||
| 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 initUserAttributes( ): array | ||
| return | array |
Auth user attributes. |
|---|---|---|
abstract protected function initUserAttributes();
Normalize given user attributes according to $normalizeUserAttributeMap.
| protected normalizeUserAttributes( array $attributes ): array | ||
| $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 removeState( string $key ): boolean | ||
| $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 setHttpClient( array|\yii\httpclient\Client $httpClient ): mixed | ||
| $httpClient | array|\yii\httpclient\Client |
Internal HTTP client. |
public function setHttpClient($httpClient)
{
$this->_httpClient = $httpClient;
}
| public setId( string $id ): mixed | ||
| $id | string |
Service id. |
public function setId($id)
{
$this->_id = $id;
}
| public setName( string $name ): mixed | ||
| $name | string |
Service name. |
public function setName($name)
{
$this->_name = $name;
}
| public setNormalizeUserAttributeMap( array $normalizeUserAttributeMap ): mixed | ||
| $normalizeUserAttributeMap | array |
Normalize user attribute map. |
public function setNormalizeUserAttributeMap($normalizeUserAttributeMap)
{
$this->_normalizeUserAttributeMap = $normalizeUserAttributeMap;
}
| public setRequestOptions( array $options ): mixed | ||
| $options | array |
HTTP request options. |
public function setRequestOptions(array $options)
{
$this->_requestOptions = $options;
}
Sets persistent state.
| protected setState( string $key, mixed $value ): $this | ||
| $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 setStateStorage( yii\authclient\StateStorageInterface|array|string $stateStorage ): mixed | ||
| $stateStorage | yii\authclient\StateStorageInterface|array|string |
Stage storage to be used. |
public function setStateStorage($stateStorage)
{
$this->_stateStorage = $stateStorage;
}
| public setTitle( string $title ): mixed | ||
| $title | string |
Service title. |
public function setTitle($title)
{
$this->_title = $title;
}
| public setUserAttributes( array $userAttributes ): mixed | ||
| $userAttributes | array |
List of user attributes |
public function setUserAttributes($userAttributes)
{
$this->_userAttributes = $this->normalizeUserAttributes($userAttributes);
}
| public setViewOptions( array $viewOptions ): mixed | ||
| $viewOptions | array |
View options in format: optionName => optionValue |
public function setViewOptions($viewOptions)
{
$this->_viewOptions = $viewOptions;
}