Skip to content

Commit 3a89397

Browse files
Merge pull request #6 from dutchenkoOleg/master
Singleton
2 parents 10bbf85 + 833afd0 commit 3a89397

File tree

3 files changed

+584
-152
lines changed

3 files changed

+584
-152
lines changed

README.md

Lines changed: 57 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Browserizr PHP
22

33
[![license](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/dutchenkoOleg/node-w3c-validator/blob/master/LICENSE)
4-
[![WezomAgency](https://img.shields.io/badge/composer-require-orange.svg)](https://packagist.org/packages/wezom-agency/browserizr)
4+
[![composer](https://img.shields.io/badge/composer-require-orange.svg)](https://packagist.org/packages/wezom-agency/browserizr)
5+
[![npm](https://img.shields.io/badge/npm-install-orange.svg)](https://www.npmjs.com/package/browserizr)
56
[![WezomAgency](https://img.shields.io/badge/wezom-agency-red.svg)](https://github.com/WezomAgency)
67

78
> **Browserizr** is tiny library, that detects your browser
@@ -20,59 +21,45 @@ composer require wezom-agency/browserizr
2021

2122
---
2223

23-
## Initalize
24+
## Prev versions
2425

25-
> _* since 2.x version_
26-
> ___You must initialize the Browserizr before to use it___
26+
> _please read:_
2727
28-
```php
29-
<?php
30-
31-
// your app start code
32-
// ...
33-
use WezomAgency\Browserizr;
34-
35-
// initialize Browserizr
36-
Browserizr::detect();
37-
38-
// use Browserizr in your app
39-
// ...
40-
?>
41-
```
28+
- [old-versions/README-2.x.md](https://github.com/WezomAgency/browserizr-php/blob/master/old-versions/README-2.x.md)
4229

4330
---
4431

4532
## API
4633

4734
### List of built-in tests
4835

49-
1. `Browserizr::$is_android`
50-
1. `Browserizr::$is_android3`
51-
1. `Browserizr::$is_android4`
52-
1. `Browserizr::$is_android5`
53-
1. `Browserizr::$is_android6`
54-
1. `Browserizr::$is_android7`
55-
1. `Browserizr::$is_android8`
56-
1. `Browserizr::$is_blackberry`
57-
1. `Browserizr::$is_blackberry10`
58-
1. `Browserizr::$is_edge`
59-
1. `Browserizr::$is_edge_android`
60-
1. `Browserizr::$is_edge_ios`
61-
1. `Browserizr::$is_ie`
62-
1. `Browserizr::$is_ie8`
63-
1. `Browserizr::$is_ie9`
64-
1. `Browserizr::$is_ie10`
65-
1. `Browserizr::$is_ie11`
66-
1. `Browserizr::$is_ipad`
67-
1. `Browserizr::$is_ipod`
68-
1. `Browserizr::$is_iphone`
69-
1. `Browserizr::$is_windows_phone`
70-
1. `Browserizr::$is_moz`
71-
1. `Browserizr::$is_opera`
72-
1. `Browserizr::$is_safari`
73-
1. `Browserizr::$is_chrome`
74-
1. `Browserizr::$is_mobile`
75-
1. `Browserizr::$is_desktop`
36+
1. `Browserizr::detect()->isAndroid()`
37+
1. `Browserizr::detect()->isAndroid3()`
38+
1. `Browserizr::detect()->isAndroid4()`
39+
1. `Browserizr::detect()->isAndroid5()`
40+
1. `Browserizr::detect()->isAndroid6()`
41+
1. `Browserizr::detect()->isAndroid7()`
42+
1. `Browserizr::detect()->isAndroid8()`
43+
1. `Browserizr::detect()->isBlackberry()`
44+
1. `Browserizr::detect()->isBlackberry10()`
45+
1. `Browserizr::detect()->isEdge()`
46+
1. `Browserizr::detect()->isEdgeAndroid()`
47+
1. `Browserizr::detect()->isEdgeIOS()`
48+
1. `Browserizr::detect()->isIE()`
49+
1. `Browserizr::detect()->isIE8()`
50+
1. `Browserizr::detect()->isIE9()`
51+
1. `Browserizr::detect()->isIE10()`
52+
1. `Browserizr::detect()->isIE11()`
53+
1. `Browserizr::detect()->isIPad()`
54+
1. `Browserizr::detect()->isIPod()`
55+
1. `Browserizr::detect()->isIPhone()`
56+
1. `Browserizr::detect()->isWindowsPhone()`
57+
1. `Browserizr::detect()->isMoz()`
58+
1. `Browserizr::detect()->isOpera()`
59+
1. `Browserizr::detect()->isSafari()`
60+
1. `Browserizr::detect()->isChrome()`
61+
1. `Browserizr::detect()->isMobile()`
62+
1. `Browserizr::detect()->isDesktop()`
7663

7764
_Usage examples:_
7865

@@ -82,26 +69,27 @@ _Usage examples:_
8269
use WezomAgency\Browserizr;
8370

8471
?>
85-
<?php if (Browserizr::$is_chrome) { ?>
72+
<?php if (Browserizr::detect()->isChrome()) { ?>
8673
<div class="alert">Chrome is here, baby!!!</div>
8774
<?php } ?>
8875

8976
```
9077

9178
---
9279

93-
### Static methods
80+
### Generate css classes
9481

95-
#### `add_classes($tests, $css_prefix = "")`
82+
#### `Browserizr::detect()->cssClasses($tests, $cssPrefix = "", $toString = true): array|string`
9683

9784
> Create string with CSS classes
9885
9986
_Parameters:_
10087

10188
Name | Data type | Default value | Description
10289
--- | --- | --- | ---
103-
`$tests` | `string[]` | | array of wanted tests
104-
`$css_prefix` | `string` | `""` | custom prefix for CSS class name
90+
`$tests` | `string[]` | | array of wanted tests, each name - test name without `is` prefix
91+
`$cssPrefix` | `string` | `""` | custom prefix for CSS class name
92+
`$toString` | `bool` | `true` | implode resulted array and return as string;
10593

10694
_Usage examples:_
10795

@@ -112,7 +100,7 @@ use WezomAgency\Browserizr;
112100

113101
?>
114102
<!DOCTYPE html>
115-
<html class="<?= Browserizr::add_classes(['mobile', 'desktop']); ?>">
103+
<html class="<?= Browserizr::detect()->cssClasses(['Mobile', 'Desktop']); ?>">
116104
<head>...</head>
117105
<body>...</body>
118106
</html>
@@ -130,7 +118,7 @@ use WezomAgency\Browserizr;
130118

131119
?>
132120
<!DOCTYPE html>
133-
<html class="<?= Browserizr::add_classes(['mobile', 'desktop'], 'browserizr-'); ?>">
121+
<html class="<?= Browserizr::detect()->cssClasses(['Mobile', 'Desktop'], 'browserizr-'); ?>">
134122
<head>...</head>
135123
<body>...</body>
136124
</html>
@@ -141,29 +129,34 @@ use WezomAgency\Browserizr;
141129

142130
```
143131

144-
#### `detect($custom_user_agent = null)`
132+
```php
133+
<?php
145134

146-
> re-checkout all detections
135+
use WezomAgency\Browserizr;
147136

148-
_Parameters:_
137+
$myCssClasses = Browserizr::detect()->cssClasses(['Mobile', 'Desktop'], '', false);
138+
// ... work with array, e.g. array_push() or array_combine() or whatever you want
149139

150-
Name | Data type | Default value | Description
151-
--- | --- | --- | ---
152-
`$custom_user_agent` | `string` | `$_SERVER['HTTP_USER_AGENT']` | string for tests
140+
?>
141+
```
153142

154-
_Usage examples:_
143+
### Set custom UserAgent
144+
145+
by default Browserizr use `$_SERVER['HTTP_USER_AGENT']`
146+
you can set own string for UserAgent.
147+
148+
> **Note**
149+
> if you needed to change agent - you must do it before use the Browserizr tests
155150
156151
```php
157152
<?php
158153

159154
use WezomAgency\Browserizr;
160155

161-
Browserizr::detect('my_user_agent')
156+
Browserizr::detect()->setUserAgent('my own UserAgent string');
162157

163-
?>
164-
<?php if (Browserizr::$is_edge_ios) { ?>
165-
<div class="alert">Edge is here, baby! Yes, on iOS!</div>
166-
<?php } ?>
158+
// then can test your browser correctly
159+
var_export(Browserizr::detect()->isWindowsPhone());
167160

168161
```
169162

old-versions/README-2.x.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# Browserizr PHP
2+
3+
[![license](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/dutchenkoOleg/node-w3c-validator/blob/master/LICENSE)
4+
[![WezomAgency](https://img.shields.io/badge/composer-require-orange.svg)](https://packagist.org/packages/wezom-agency/browserizr)
5+
[![WezomAgency](https://img.shields.io/badge/wezom-agency-red.svg)](https://github.com/WezomAgency)
6+
7+
> **Browserizr** is tiny library, that detects your browser
8+
> with testing `$_SERVER['HTTP_USER_AGENT']`.
9+
10+
_that is PHP port of the JavaScript library [WezomAgency/Browserizr](https://github.com/WezomAgency/browserizr)
11+
with several limitations..._
12+
13+
---
14+
15+
## Install
16+
17+
```bash
18+
composer require wezom-agency/browserizr
19+
```
20+
21+
---
22+
23+
## Initalize
24+
25+
> _* since 2.x version_
26+
> ___You must initialize the Browserizr before to use it___
27+
28+
```php
29+
<?php
30+
31+
// your app start code
32+
// ...
33+
use WezomAgency\Browserizr;
34+
35+
// initialize Browserizr
36+
Browserizr::detect();
37+
38+
// use Browserizr in your app
39+
// ...
40+
?>
41+
```
42+
43+
---
44+
45+
## API
46+
47+
### List of built-in tests
48+
49+
1. `Browserizr::$is_android`
50+
1. `Browserizr::$is_android3`
51+
1. `Browserizr::$is_android4`
52+
1. `Browserizr::$is_android5`
53+
1. `Browserizr::$is_android6`
54+
1. `Browserizr::$is_android7`
55+
1. `Browserizr::$is_android8`
56+
1. `Browserizr::$is_blackberry`
57+
1. `Browserizr::$is_blackberry10`
58+
1. `Browserizr::$is_edge`
59+
1. `Browserizr::$is_edge_android`
60+
1. `Browserizr::$is_edge_ios`
61+
1. `Browserizr::$is_ie`
62+
1. `Browserizr::$is_ie8`
63+
1. `Browserizr::$is_ie9`
64+
1. `Browserizr::$is_ie10`
65+
1. `Browserizr::$is_ie11`
66+
1. `Browserizr::$is_ipad`
67+
1. `Browserizr::$is_ipod`
68+
1. `Browserizr::$is_iphone`
69+
1. `Browserizr::$is_windows_phone`
70+
1. `Browserizr::$is_moz`
71+
1. `Browserizr::$is_opera`
72+
1. `Browserizr::$is_safari`
73+
1. `Browserizr::$is_chrome`
74+
1. `Browserizr::$is_mobile`
75+
1. `Browserizr::$is_desktop`
76+
77+
_Usage examples:_
78+
79+
```php
80+
<?php
81+
82+
use WezomAgency\Browserizr;
83+
84+
?>
85+
<?php if (Browserizr::$is_chrome) { ?>
86+
<div class="alert">Chrome is here, baby!!!</div>
87+
<?php } ?>
88+
89+
```
90+
91+
---
92+
93+
### Static methods
94+
95+
#### `add_classes($tests, $css_prefix = "")`
96+
97+
> Create string with CSS classes
98+
99+
_Parameters:_
100+
101+
Name | Data type | Default value | Description
102+
--- | --- | --- | ---
103+
`$tests` | `string[]` | | array of wanted tests
104+
`$css_prefix` | `string` | `""` | custom prefix for CSS class name
105+
106+
_Usage examples:_
107+
108+
```php
109+
<?php
110+
111+
use WezomAgency\Browserizr;
112+
113+
?>
114+
<!DOCTYPE html>
115+
<html class="<?= Browserizr::add_classes(['mobile', 'desktop']); ?>">
116+
<head>...</head>
117+
<body>...</body>
118+
</html>
119+
120+
<!-- render output -->
121+
<html class="is-mobile is-not-desktop"><!-- if mobile device -->
122+
<html class="is-not-mobile is-desktop"><!-- if desktop -->
123+
124+
```
125+
126+
```php
127+
<?php
128+
129+
use WezomAgency\Browserizr;
130+
131+
?>
132+
<!DOCTYPE html>
133+
<html class="<?= Browserizr::add_classes(['mobile', 'desktop'], 'browserizr-'); ?>">
134+
<head>...</head>
135+
<body>...</body>
136+
</html>
137+
138+
<!-- render output -->
139+
<html class="browserizr-is-mobile browserizr-is-not-desktop"><!-- if mobile device -->
140+
<html class="browserizr-is-not-mobile browserizr-is-desktop"><!-- if desktop -->
141+
142+
```
143+
144+
#### `detect($custom_user_agent = null)`
145+
146+
> re-checkout all detections
147+
148+
_Parameters:_
149+
150+
Name | Data type | Default value | Description
151+
--- | --- | --- | ---
152+
`$custom_user_agent` | `string` | `$_SERVER['HTTP_USER_AGENT']` | string for tests
153+
154+
_Usage examples:_
155+
156+
```php
157+
<?php
158+
159+
use WezomAgency\Browserizr;
160+
161+
Browserizr::detect('my_user_agent')
162+
163+
?>
164+
<?php if (Browserizr::$is_edge_ios) { ?>
165+
<div class="alert">Edge is here, baby! Yes, on iOS!</div>
166+
<?php } ?>
167+
168+
```
169+
170+
---
171+
172+

0 commit comments

Comments
 (0)