Overview
  • Namespace
  • Class

Namespaces

  • Klarna
    • Rest
      • Checkout
      • OrderManagement
      • Transport
        • Exception

Interfaces

  • Klarna\Rest\Transport\Exception\ConnectorException
  1 <?php
  2 /**
  3  * Copyright 2014 Klarna AB
  4  *
  5  * Licensed under the Apache License, Version 2.0 (the "License");
  6  * you may not use this file except in compliance with the License.
  7  * You may obtain a copy of the License at
  8  *
  9  * http://www.apache.org/licenses/LICENSE-2.0
 10  *
 11  * Unless required by applicable law or agreed to in writing, software
 12  * distributed under the License is distributed on an "AS IS" BASIS,
 13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  * See the License for the specific language governing permissions and
 15  * limitations under the License.
 16  *
 17  * File containing the UserAgent class.
 18  */
 19 
 20 namespace Klarna\Rest\Transport;
 21 
 22 use GuzzleHttp\ClientInterface;
 23 
 24 /**
 25  * HTTP user agent.
 26  */
 27 class UserAgent implements UserAgentInterface
 28 {
 29     /**
 30      * Name of the SDK
 31      */
 32     const NAME = 'Klarna.kco_rest_php';
 33 
 34     /**
 35      * Version of the SDK.
 36      */
 37     const VERSION = '2.2.0';
 38 
 39     /**
 40      * Components of the user agent.
 41      *
 42      * @var array
 43      */
 44     protected $fields = [];
 45 
 46 
 47     /**
 48      * Sets the specified field.
 49      *
 50      * @param string $key     Component key, e.g. 'Language'
 51      * @param string $name    Component name, e.g. 'PHP'
 52      * @param string $version Version identifier, e.g. '5.4.10'
 53      * @param array  $options Additional information
 54      *
 55      * @return self
 56      */
 57     public function setField($key, $name, $version = '', array $options = [])
 58     {
 59         $field = [
 60             'name' => $name
 61         ];
 62 
 63         if (!empty($version)) {
 64             $field['version'] = $version;
 65         }
 66 
 67         if (!empty($options)) {
 68             $field['options'] = $options;
 69         }
 70 
 71         $this->fields[$key] = $field;
 72 
 73         return $this;
 74     }
 75 
 76     /**
 77      * Serialises the user agent.
 78      *
 79      * @return string
 80      */
 81     public function __toString()
 82     {
 83         $parts = [];
 84 
 85         foreach ($this->fields as $key => $value) {
 86             $component = "{$key}/{$value['name']}";
 87             if (!empty($value['version'])) {
 88                 $component .= "_{$value['version']}";
 89             }
 90 
 91             $parts[] = $component;
 92 
 93             if (empty($value['options'])) {
 94                 continue;
 95             }
 96 
 97             $opts = implode('; ', $value['options']);
 98             $parts[] = "({$opts})";
 99         }
100 
101         return implode(' ', $parts);
102     }
103 
104     /**
105      * Creates the default user agent.
106      *
107      * @return self
108      */
109     public static function createDefault()
110     {
111         $agent = new static();
112 
113         $options = ['Guzzle/' . ClientInterface::VERSION];
114         if (extension_loaded('curl')) {
115             $options[] = 'curl/' . curl_version()['version'];
116         }
117 
118         return $agent
119             ->setField('Library', static::NAME, static::VERSION, $options)
120             ->setField('OS', php_uname('s'), php_uname('r'))
121             ->setField('Language', 'PHP', phpversion());
122     }
123 }
124 
API documentation generated by ApiGen