OpenIdExtensionAttributeExchange.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2010 by Alexander V. Solomatin                          *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00018     final class OpenIdExtensionAttributeExchange implements OpenIdExtension
00019     {
00020         const NAMESPACE_1_0     = 'http://openid.net/srv/ax/1.0';
00021         const PARAM_EMAIL       = 'email';
00022         const PARAM_FIRSTNAME   = 'firstname';
00023         const PARAM_LASTNAME    = 'lastname';
00024         const PARAM_COUNTRY     = 'country';
00025         const PARAM_LANGUAGE    = 'language';
00026         
00027         private $params         = array();
00028         private $country        = null;
00029         private $email          = null;
00030         private $firstname      = null;
00031         private $lastname       = null;
00032         private $language       = null;
00033         
00037         public static function create()
00038         {
00039             return new self();
00040         }
00041         
00045         public function addParamsToModel(Model $model)
00046         {
00047             $model->
00048                 set('openid.ns.ax', self::NAMESPACE_1_0)->
00049                 set('openid.ax.mode', 'fetch_request')->
00050                 set( 'openid.ax.required', implode(',', $this->params))->
00051                 set(
00052                     'openid.ax.type.country',
00053                     'http://axschema.org/contact/country/home'
00054                 )->
00055                 set(
00056                     'openid.ax.type.email',
00057                     'http://axschema.org/contact/email'
00058                 )->
00059                 set(
00060                     'openid.ax.type.firstname',
00061                     'http://axschema.org/namePerson/first'
00062                 )->
00063                 set(
00064                     'openid.ax.type.lastname',
00065                     'http://axschema.org/namePerson/last'
00066                 )->
00067                 set(
00068                     'openid.ax.type.language',
00069                     'http://axschema.org/pref/language'
00070                 );
00071         }
00072         
00077         public function parseResponce(HttpRequest $request, array $params)
00078         {
00079             if (!($prefix = $this->getPrefix($params))) {
00080                 return;
00081             }
00082             
00083             foreach ($this->params as $param) {
00084                 $this->$param = null;
00085                 if (isset($params[$prefix.$param])) {
00086                     $this->$param = $params[$prefix.$param];
00087                 }
00088             }
00089         }
00090         
00094         public function addParam($paramName)
00095         {
00096             $this->params []= $paramName;
00097             
00098             return $this;
00099         }
00100         
00104         public function dropParams()
00105         {
00106             $this->params = array();
00107             
00108             return $this;
00109         }
00110         
00111         public function getPrefix(array $params)
00112         {
00113             foreach ($params as $paramName => $val) {
00114                 if ($val == self::NAMESPACE_1_0) {
00115                     return 'openid.'.str_replace('openid.ns_', '', $paramName).'_value_';
00116                 }
00117             }
00118             
00119             return null;
00120         }
00121         
00122         public function getCountry()
00123         {
00124             return $this->country;
00125         }
00126         
00127         public function getEmail()
00128         {
00129             return $this->email;
00130         }
00131         
00132         public function getFirstname()
00133         {
00134             return $this->firstname;
00135         }
00136         
00137         public function getLastname()
00138         {
00139             return $this->lastname;
00140         }
00141         
00142         public function getLanguage()
00143         {
00144             return $this->language;
00145         }
00146     }
00147 ?>