Completed
Push — master ( 4d38ee...409412 )
by dotzero
02:01
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\Request\ParamsBag;
6
use AmoCRM\Helpers\Fields;
7
8
/**
9
 * Class Client
10
 *
11
 * Основной класс для получения доступа к моделям amoCRM API
12
 *
13
 * @package AmoCRM
14
 * @author dotzero <[email protected]>
15
 * @link http://www.dotzero.ru/
16
 * @link https://github.com/dotzero/amocrm-php
17
 * @property \AmoCRM\Models\Account $account
18
 * @property \AmoCRM\Models\Company $company
19
 * @property \AmoCRM\Models\Contact $contact
20
 * @property \AmoCRM\Models\CustomersPeriods $customers_periods
21
 * @property \AmoCRM\Models\Lead $lead
22
 * @property \AmoCRM\Models\Note $note
23
 * @property \AmoCRM\Models\Task $task
24
 * @property \AmoCRM\Models\Pipelines $pipelines
25
 * @property \AmoCRM\Models\Unsorted $unsorted
26
 * @property \AmoCRM\Models\Widgets $widgets
27
 * @property \AmoCRM\Models\WebHooks $webhooks
28
 *
29
 * For the full copyright and license information, please view the LICENSE
30
 * file that was distributed with this source code.
31
 */
32
class Client
33
{
34
    /**
35
     * @var Fields|null Экземпляр Fields для хранения номеров полей
36
     */
37
    public $fields = null;
38
39
    /**
40
     * @var ParamsBag|null Экземпляр ParamsBag для хранения аргументов
41
     */
42
    public $parameters = null;
43
44
    /**
45
     * Client constructor
46
     *
47
     * @param string $domain Поддомен amoCRM
48
     * @param string $login Логин amoCRM
49
     * @param string $apikey Ключ пользователя amoCRM
50
     */
51 12
    public function __construct($domain, $login, $apikey)
52
    {
53 12
        $this->parameters = new ParamsBag();
54 12
        $this->parameters->addAuth('domain', $domain);
55 12
        $this->parameters->addAuth('login', $login);
56 12
        $this->parameters->addAuth('apikey', $apikey);
57
58 12
        $this->fields = new Fields();
59 12
    }
60
61
    /**
62
     * Возращает экземпляр модели для работы с amoCRM API
63
     *
64
     * @param string $name Название модели
65
     * @return mixed
66
     * @throws ModelException
67
     */
68 7
    public function __get($name)
69
    {
70 7
        $classname = '\\AmoCRM\\Models\\' . $this->toCamelCase($name);
71
72 7
        if (!class_exists($classname)) {
73 1
            throw new ModelException('Model not exists: ' . $name);
74
        }
75
76
        // Чистим GET и POST от предыдущих вызовов
77 6
        $this->parameters->clearGet()->clearPost();
78
79 6
        return new $classname($this->parameters);
80
    }
81
82
    /**
83
     * Приведение under_score к CamelCase
84
     *
85
     * @param string $string Строка
86
     * @return string Строка
87
     */
88 7
    private function toCamelCase($string)
89
    {
90 7
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
91
    }
92
}
93