Completed
Push — master ( bdb1b7...0d1a59 )
by dotzero
11s
created

Client::toCamelCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace AmoCRM;
4
5
use AmoCRM\Models\ModelInterface;
6
use AmoCRM\Request\ParamsBag;
7
use AmoCRM\Helpers\Fields;
8
9
/**
10
 * Class Client
11
 *
12
 * Основной класс для получения доступа к моделям amoCRM API
13
 *
14
 * @package AmoCRM
15
 * @author dotzero <[email protected]>
16
 * @link http://www.dotzero.ru/
17
 * @link https://github.com/dotzero/amocrm-php
18
 * @property \AmoCRM\Models\Account $account
19
 * @property \AmoCRM\Models\Call $call
20
 * @property \AmoCRM\Models\Catalog $catalog
21
 * @property \AmoCRM\Models\CatalogElement $catalog_element
22
 * @property \AmoCRM\Models\Company $company
23
 * @property \AmoCRM\Models\Contact $contact
24
 * @property \AmoCRM\Models\Customer $customer
25
 * @property \AmoCRM\Models\CustomersPeriods $customers_periods
26
 * @property \AmoCRM\Models\CustomField $custom_field
27
 * @property \AmoCRM\Models\Lead $lead
28
 * @property \AmoCRM\Models\Links $links
29
 * @property \AmoCRM\Models\Note $note
30
 * @property \AmoCRM\Models\Pipelines $pipelines
31
 * @property \AmoCRM\Models\Task $task
32
 * @property \AmoCRM\Models\Transaction $transaction
33
 * @property \AmoCRM\Models\Unsorted $unsorted
34
 * @property \AmoCRM\Models\WebHooks $webhooks
35
 * @property \AmoCRM\Models\Widgets $widgets
36
 *
37
 * For the full copyright and license information, please view the LICENSE
38
 * file that was distributed with this source code.
39
 */
40
class Client
41
{
42
    /**
43
     * @var Fields|null Экземпляр Fields для хранения номеров полей
44
     */
45
    public $fields = null;
46
47
    /**
48
     * @var ParamsBag|null Экземпляр ParamsBag для хранения аргументов
49
     */
50
    public $parameters = null;
51
52
    /**
53
     * Client constructor
54
     *
55
     * @param string $domain Поддомен или домен amoCRM
56
     * @param string $login Логин amoCRM
57
     * @param string $apikey Ключ пользователя amoCRM
58
     * @param string $proxy Прокси сервер для отправки запроса
59
     */
60 27
    public function __construct($domain, $login, $apikey, $proxy = null)
61
    {
62
        // Разернуть поддомен в полный домен
63 27
        if (strpos($domain, '.') === false) {
64 23
            $domain = sprintf('%s.amocrm.ru', $domain);
65 23
        }
66
67 27
        $this->parameters = new ParamsBag();
68 27
        $this->parameters->addAuth('domain', $domain);
69 27
        $this->parameters->addAuth('login', $login);
70 27
        $this->parameters->addAuth('apikey', $apikey);
71
72 27
        if ($proxy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $proxy of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
73
            $this->parameters->addProxy($proxy);
74
        }
75
76 27
        $this->fields = new Fields();
77 27
    }
78
79
    /**
80
     * Возвращает экземпляр модели для работы с amoCRM API
81
     *
82
     * @param string $name Название модели
83
     * @return ModelInterface
84
     * @throws ModelException
85
     */
86 19
    public function __get($name)
87
    {
88 19
        $classname = '\\AmoCRM\\Models\\' . $this->toCamelCase($name);
89
90 19
        if (!class_exists($classname)) {
91 1
            throw new ModelException('Model not exists: ' . $name);
92
        }
93
94
        // Чистим GET и POST от предыдущих вызовов
95 18
        $this->parameters->clearGet()->clearPost();
96
97 18
        return new $classname($this->parameters);
98
    }
99
100
    /**
101
     * Приведение under_score к CamelCase
102
     *
103
     * @param string $string Строка
104
     * @return string Строка
105
     */
106 19
    private function toCamelCase($string)
107
    {
108 19
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
109
    }
110
}
111