Пример #1
0
 /**
  * Converts service attributes to app\modules\user\models\User model attributes
  * @param \yii\authclient\BaseClient $client
  * @return array Array of attributes by model type which we can apply by $model->setAttributes()
  */
 public static function mapUserAttributesWithService(\yii\authclient\BaseClient $client)
 {
     $mappings = ['service' => ['service_id' => static::$ServiceIdMapping], 'user' => ['username' => ['app\\modules\\user\\authclients\\GitHub' => 'login', 'yii\\authclient\\clients\\Twitter' => 'screen_name', 'app\\modules\\user\\authclients\\VKontakte' => 'nickname', 'yii\\authclient\\clients\\YandexOAuth' => 'login'], 'email' => ['app\\modules\\user\\authclients\\GitHub' => 'email', 'yii\\authclient\\clients\\YandexOpenId' => 'email', 'app\\modules\\user\\authclients\\Facebook' => 'email', 'yii\\authclient\\clients\\YandexOAuth' => 'default_email'], 'first_name' => ['app\\modules\\user\\authclients\\Facebook' => 'first_name', 'app\\modules\\user\\authclients\\VKontakte' => 'first_name', 'yii\\authclient\\clients\\YandexOAuth' => 'first_name'], 'last_name' => ['app\\modules\\user\\authclients\\Facebook' => 'last_name', 'app\\modules\\user\\authclients\\VKontakte' => 'last_name', 'yii\\authclient\\clients\\YandexOAuth' => 'last_name'], 'avatar_url' => ['app\\modules\\user\\authclients\\GitHub' => 'avatar_url', 'yii\\authclient\\clients\\Twitter' => 'profile_image_url', 'app\\modules\\user\\authclients\\VKontakte' => 'photo'], 'company' => ['app\\modules\\user\\authclients\\GitHub' => 'company'], 'url' => ['app\\modules\\user\\authclients\\GitHub' => 'html_url'], 'location' => ['app\\modules\\user\\authclients\\GitHub' => 'location']]];
     $class_name = $client->className();
     $attributes = $client->getUserAttributes();
     $result = [];
     foreach ($mappings as $model_type => $mappings_by_attribute) {
         $result[$model_type] = [];
         foreach ($mappings_by_attribute as $attribute => $maps) {
             if (isset($maps[$class_name])) {
                 $key_in_attributes = $maps[$class_name];
                 $value = null;
                 if (is_array($key_in_attributes)) {
                     $value = [];
                     foreach ($key_in_attributes as $key) {
                         if (isset($attributes[$key])) {
                             $value[] = $attributes[$key];
                         }
                     }
                     if (count($value) > 0) {
                         $value = implode(' ', $value);
                     } else {
                         $value = null;
                     }
                 } else {
                     $value = isset($attributes[$key_in_attributes]) ? $attributes[$key_in_attributes] : null;
                 }
                 if ($value !== null) {
                     $result[$model_type][$attribute] = $value;
                 }
             }
         }
     }
     return $result;
 }