Skip to content

Commit 13f34d8

Browse files
Merge pull request #6 from monstar-lab-oss/develop
Develop
2 parents d9ad978 + d4dcfdd commit 13f34d8

File tree

3 files changed

+208
-117
lines changed

3 files changed

+208
-117
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
## 📝 Introduction
99

10-
Core of N-Meta, parsing and DTO.
10+
Core of N-Meta, parsing and DTO, A User-Agent header for android and ios apps.
1111

1212
Used to parse the N-Meta headers using following format:
1313

@@ -20,17 +20,17 @@ Description of the requesting platform, expects values `android`, `ios` or `web`
2020
Description of the environment the request is targeted at, normally `staging` or `production`
2121

2222
#### APP_VERSION:
23-
Semantic version sting of the requesting client `x.y.z`
23+
Semantic version sting of the requesting client `x.y.z`. Not required if platform is web.
2424

2525
#### DEVICE_OS:
26-
Version of the operating system the request was made from. Not if platform is web, eg. `10.2`
26+
Version of the operating system the request was made from, eg. `10.2`. Not required if platform is web.
2727

2828
#### DEVICE:
29-
Description of the device making the request, eg. `iphone-x`
29+
Description of the device making the request, eg. `iphone-x`. Not required if platform is web.
3030

3131
## 📦 Installation
3232

33-
To install this package you will need:
33+
To use this package you will need:
3434

3535
* PHP 8.0+
3636

src/NMeta.php

Lines changed: 181 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ class NMeta
7272
*/
7373
protected array $environments;
7474

75+
/**
76+
* Configuration object
77+
*
78+
* @var Config
79+
*/
80+
protected Config $config;
81+
82+
/**
83+
* Header format reference
84+
*
85+
* @var string
86+
*/
87+
protected string $format = 'platform;environment;version;os-version;device'; // ios;local;1.0.0;10.1;iphone-x
88+
7589
/**
7690
* NMeta constructor.
7791
*
@@ -82,122 +96,39 @@ class NMeta
8296
*/
8397
public function __construct(?string $header = null, Config $config = null)
8498
{
85-
if (empty($header)) {
86-
throw new BadRequestException($config->getHeader() . ' header is missing');
87-
}
88-
8999
if (!$config) {
90-
$config = Config::createDefault();
100+
$this->config = Config::createDefault();
101+
} else {
102+
$this->config = $config;
91103
}
92104

93-
$this->platforms = $config->getPlatforms();
94-
$this->environments = $config->getEnvironments();
95-
96-
$format = 'platform;environment;version;os-version;device'; // ios;local;1.0.0;10.1;iphone-x
97-
98-
$headerArr = explode(';', $header);
99-
100-
// Parse platform
101-
if (!isset($headerArr[0]) || !in_array($headerArr[0], $this->platforms)) {
102-
$message = sprintf(
103-
'%s header: Platform is not supported, should be: %s - format: %s',
104-
$config->getHeader(),
105-
implode(',', $this->platforms),
106-
$format
107-
);
108-
109-
throw new BadRequestException($message);
105+
if (empty($header)) {
106+
throw new BadRequestException($this->config->getHeader() . ' header is missing');
110107
}
111108

112-
$this->platform = $headerArr[0];
109+
$this->platforms = $this->config->getPlatforms();
110+
$this->environments = $this->config->getEnvironments();
113111

114-
// Parse env
115-
if (!isset($headerArr[1]) || !in_array($headerArr[1], $this->environments)) {
116-
$message = sprintf(
117-
'%s header: Environment is not supported, should be: %s - format: %s',
118-
$config->getHeader(),
119-
implode(',', $this->environments),
120-
$format
121-
);
122-
123-
throw new BadRequestException($message);
124-
}
112+
$headerArr = explode(';', $header);
125113

126-
$this->environment = $headerArr[1];
114+
$this->parsePlatform($headerArr[0] ?? null);
115+
$this->parseEnvironment($headerArr[1] ?? null);
127116

128117
// Web does not have further requirements, since they have a normal User-Agent header
129118
if ($this->platform == 'web') {
130119
$this->version = sprintf(
131120
'%d.%d.%d',
132-
$this->majorVersion,
133-
$this->minorVersion,
134-
$this->patchVersion
135-
);
136-
return;
137-
}
138-
139-
// Parse Build number
140-
if (!isset($headerArr[2])) {
141-
$message = sprintf(
142-
'Meta header: Missing version - format: %s',
143-
$format
144-
);
145-
146-
throw new BadRequestException($message);
147-
}
148-
149-
$this->version = $headerArr[2];
150-
$versionArr = explode('.', $this->version);
151-
152-
if (count($versionArr) != 3) {
153-
$message = 'Meta header: Invalid app version, invalid amount of segments. Expected semver [x.y.z]';
154-
throw new BadRequestException($message);
155-
}
156-
157-
if (!is_numeric($versionArr[0])) {
158-
$message = 'Meta header: Invalid Major version, expected integer';
159-
throw new BadRequestException($message);
160-
}
161-
162-
$this->majorVersion = $versionArr[0] ?? 0;
163-
164-
if (!is_numeric($versionArr[1])) {
165-
$message = 'Meta header: Invalid Minor version, expected integer';
166-
throw new BadRequestException($message);
167-
}
168-
169-
$this->minorVersion = $versionArr[1] ?? 0;
170-
171-
if (!is_numeric($versionArr[2])) {
172-
$message = 'Meta header: Invalid Patch version, expected integer';
173-
throw new BadRequestException($message);
174-
}
175-
176-
$this->patchVersion = $versionArr[2] ?? 0;
177-
178-
// Parse device os version
179-
if (!isset($headerArr[3])) {
180-
$message = sprintf(
181-
'Meta header: Missing device os version - format: %s',
182-
$format
121+
$this->getMajorVersion(),
122+
$this->getMinorVersion(),
123+
$this->getPatchVersion()
183124
);
184125

185-
throw new BadRequestException($message);
186-
}
187-
188-
$this->deviceOsVersion = $headerArr[3];
189-
190-
// Parse device
191-
if (!isset($headerArr[4])) {
192-
$message = sprintf(
193-
'Meta header: Missing device - format: %s',
194-
$format
195-
);
196-
197-
throw new BadRequestException($message);
126+
return;
198127
}
199128

200-
$this->device = $headerArr[4];
129+
$this->parseBuildVersion($headerArr[2] ?? null);
130+
$this->parseDeviceOsVersion($headerArr[3] ?? null);
131+
$this->parseDevice($headerArr[4] ?? null);
201132
}
202133

203134
/**
@@ -336,4 +267,154 @@ public function toHeaderString(): string
336267
);
337268
}
338269
}
270+
271+
/**
272+
* parsePlatform
273+
*
274+
* @param string|null $platform
275+
* @throws BadRequestException
276+
*/
277+
private function parsePlatform(?string $platform): void
278+
{
279+
if (!isset($platform) || !in_array($platform, $this->platforms)) {
280+
$message = sprintf(
281+
'%s header: Platform is not supported, should be: %s - format: %s',
282+
$this->config->getHeader(),
283+
implode(',', $this->platforms),
284+
$this->format
285+
);
286+
287+
throw new BadRequestException($message);
288+
}
289+
290+
$this->platform = $platform;
291+
}
292+
293+
/**
294+
* parseEnvironment
295+
*
296+
* @param string|null $environment
297+
* @throws BadRequestException
298+
*/
299+
private function parseEnvironment(?string $environment): void
300+
{
301+
if (!isset($environment) || !in_array($environment, $this->environments)) {
302+
$message = sprintf(
303+
'%s header: Environment is not supported, should be: %s - format: %s',
304+
$this->config->getHeader(),
305+
implode(',', $this->environments),
306+
$this->format
307+
);
308+
309+
throw new BadRequestException($message);
310+
}
311+
312+
$this->environment = $environment;
313+
}
314+
315+
/**
316+
* parseBuildVersion
317+
*
318+
* @param string|null $version
319+
* @throws BadRequestException
320+
*/
321+
private function parseBuildVersion(?string $version): void
322+
{
323+
if (!isset($version)) {
324+
$message = sprintf(
325+
'%s header: Missing version - format: %s',
326+
$this->config->getHeader(),
327+
$this->format
328+
);
329+
330+
throw new BadRequestException($message);
331+
}
332+
333+
$this->version = $version;
334+
$versionArr = explode('.', $this->version);
335+
336+
if (count($versionArr) != 3) {
337+
$message = sprintf(
338+
'%s header: Invalid app version, invalid amount of segments. Expected semver [x.y.z]',
339+
$this->config->getHeader()
340+
);
341+
342+
throw new BadRequestException($message);
343+
}
344+
345+
if (!is_numeric($versionArr[0])) {
346+
$message = sprintf(
347+
'%s header: Invalid Major version, expected integer',
348+
$this->config->getHeader()
349+
);
350+
351+
throw new BadRequestException($message);
352+
}
353+
354+
$this->majorVersion = $versionArr[0] ?? 0;
355+
356+
if (!is_numeric($versionArr[1])) {
357+
$message = sprintf(
358+
'%s header: Invalid Minor version, expected integer',
359+
$this->config->getHeader()
360+
);
361+
362+
throw new BadRequestException($message);
363+
}
364+
365+
$this->minorVersion = $versionArr[1] ?? 0;
366+
367+
if (!is_numeric($versionArr[2])) {
368+
$message = sprintf(
369+
'%s header: Invalid Patch version, expected integer',
370+
$this->config->getHeader()
371+
);
372+
373+
throw new BadRequestException($message);
374+
}
375+
376+
$this->patchVersion = $versionArr[2] ?? 0;
377+
}
378+
379+
/**
380+
* parseDeviceOsVersion
381+
*
382+
* @param string|null $version
383+
* @throws BadRequestException
384+
*/
385+
private function parseDeviceOsVersion(?string $version): void
386+
{
387+
if (!isset($version)) {
388+
$message = sprintf(
389+
'%s header: Missing device os version - format: %s',
390+
$this->config->getHeader(),
391+
$this->format
392+
);
393+
394+
throw new BadRequestException($message);
395+
}
396+
397+
$this->deviceOsVersion = $version;
398+
}
399+
400+
/**
401+
* parseDevice
402+
*
403+
* @param string|null $device
404+
* @throws BadRequestException
405+
*/
406+
private function parseDevice(?string $device): void
407+
{
408+
if (!isset($device)) {
409+
$message = sprintf(
410+
'%s header: Missing device - format: %s',
411+
$this->config->getHeader(),
412+
$this->format
413+
);
414+
415+
throw new BadRequestException($message);
416+
}
417+
418+
$this->device = $device;
419+
}
339420
}

0 commit comments

Comments
 (0)