Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
urlmaker.php
1<?php
2
4
7
13{
22 public function make(array $params, string $url, array $required = [], array $encode = []): string
23 {
24 $this->checkRequiredFields($params, $required);
25 $params = $this->encodeFields($params, $encode);
26 return $this->buildQuery($params, $url);
27 }
28
34 protected function buildQuery(array $params, string $url): string
35 {
36 $query = '';
37
38 foreach($params as $key => $value)
39 {
40 if($query <> '')
41 {
42 $query .= '&';
43 }
44
45 $query .= $key.'='.$value;
46 }
47
48 return $url.'?'.$query;
49 }
50
56 protected function checkRequiredFields(array $params, array $requiredFields): void
57 {
58 foreach($requiredFields as $field)
59 {
60 if(!isset($params[$field]))
61 {
62 throw new ArgumentNullException('data['.$field.']');
63 }
64 }
65 }
66
73 protected function encodeFields(array $params, array $fieldsToEncode): array
74 {
75 foreach($fieldsToEncode as $field)
76 {
77 if(!isset($params[$field]))
78 {
79 throw new ArgumentNullException('Field "'.$field.'" is absent');
80 }
81
82 if(ToUpper(SITE_CHARSET) !== 'UTF-8')
83 {
84 $params[$field] = urlencode(
85 Encoding::convertEncoding(
86 $params[$field],
87 SITE_CHARSET,
88 'UTF-8'
89 ));
90 }
91 }
92
93 return $params;
94 }
95}
encodeFields(array $params, array $fieldsToEncode)
Definition urlmaker.php:73
checkRequiredFields(array $params, array $requiredFields)
Definition urlmaker.php:56
make(array $params, string $url, array $required=[], array $encode=[])
Definition urlmaker.php:22