Skip to content

Commit

Permalink
HTTP20 functions are now static
Browse files Browse the repository at this point in the history
  • Loading branch information
Simbiat committed Aug 9, 2022
1 parent 59d78fa commit c8c0fc5
Show file tree
Hide file tree
Showing 18 changed files with 401 additions and 400 deletions.
7 changes: 6 additions & 1 deletion doc/Atom.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
## Atom
```php
(new \Simbiat\HTTP20\Atom)->Atom(string $title, array $entries, string $id = '', string $textType = 'text', array $feed_settings = []);
\Simbiat\HTTP20\Atom::Atom(string $title, array $entries, string $id = '', string $textType = 'text', array $feed_settings = []);
```
Function to generate Atom feed as per https://validator.w3.org/feed/docs/atom.html specification.

`$title` - string that will be used for `<title>` tag in the feed.

`$id` - string, that will be used as `id`. It needs to be a URI, thus if it will be empty, will use `(isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']`, that is current address.

`$textType` is the text type, that will be added as attribute to some tags as per specification. Supported types are `text`, `html`, `xhtml`.

`$feed_settings` - array with optional settings for the feed. Maximum will look like this:
```php
[
Expand Down Expand Up @@ -47,6 +51,7 @@ Function to generate Atom feed as per https://validator.w3.org/feed/docs/atom.ht
]
```
For details on elements above (what they mean, recommendations, etc.), please, refer to Atom specification: names in `array` correspond to respective tags in feed.

`$entries` - array of elements (arrays), that will populate the feed. Mandatory value is `link`, instead of `id` as in specification, because `id` is expected to be a URI regardless (although it will be a modified one). `title` and `updated` are also mandatory. Maximum for each element will look like this:
```php
[
Expand Down
14 changes: 12 additions & 2 deletions doc/Common.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Common
Assortment of functions, that are used by classes inside the library, but can also be used directly. They are all called as
```php
(new \Simbiat\HTTP20\Common)->nameOfFunction();
\Simbiat\HTTP20\Common::nameOfFunction();
```

## valueToTime
Expand All @@ -30,8 +30,10 @@ Function to prepare ID for Atom feed as suggested on http://web.archive.org/web/
```php
zEcho(string $string, string $cacheStrat = '', bool $exit = true);
```
A function for outputting data to web-browser while attempting to use compression, if available, and providing `Content-Length` header. In terms of compression, it will check whether `zlib` extension is loaded, then check if `zlib.output_compression` is `'On'`. If `zlib` is enabled, but compression is not enabled globally, it will use `ob_gzhandler` and add header, if not - just use the buffer and send the data. If `zlib` is not loaded, will not use compression, but will use buffer to provide proper header. The goal of the function is more standardization of the output, in case you have different settings on different environments for some reason.
A function for outputting data to web-browser while attempting to use compression, if available, and providing `Content-Length` header. In terms of compression, it will check whether `brotli` or `zlib` extension is loaded, then check if `zlib.output_compression` is `'On'` (for `zlib`). If `zlib` is enabled, but compression is not enabled globally, it will use `ob_gzhandler` and add header, if not - just use the buffer and send the data. If `zlib` is not loaded, will not use compression, but will use buffer to provide proper header. The goal of the function is more standardization of the output, in case you have different settings on different environments for some reason.

`$cacheStrat` is an optional caching strategy to apply (as described for [cacheControl](doc/Headers.md#cachecontrol))

`$exit` allows to cancel automatic exit of the script (default), in case you want to do some more processing even after the page is pushed to client.

## emailValidator
Expand All @@ -57,19 +59,27 @@ Simple boolean function to check that string is a valid language code. I was not
htmlToRFC3986(string $string, bool $full = true);
```
Function does the same as `rawurlencode` (which converts characters to strings like `%20`), but only for selected characters, that are restricted in HTML/XML. Useful for URIs that can have these characters and need to be used in HTML/XML and thus can't use `htmlentities` for escaping but break HTML/XML otherwise.

`$full` set to `true` means that all of them (`'"&<>`) will be converted (useful when text is inside a tag). If `false` only `<` and `&` are converted (useful when inside attribute value). If `false` is used - be careful with quotes inside the string you provide, because they can invalidate your HTML/XML.

## reductor
```php
reductor($files, string $type, bool $minify = false, string $toFile = '', string $cacheStrat = '');
```
Function to merge CSS/JS files to reduce number of connections to your server, yet allow you to keep the files separate for easier development. It also allows you to minify the result for extra size saving, but be careful with that.

Minification is based on https://gist.github.com/Rodrigo54/93169db48194d470188f

`$files` can be a string, a path to file, a path to folder or an array of file paths.

`$type` can be anything, technically, but currently `css`, `js` or `html` are supported.

`$minify` trigger the minification if set to `true`. It's set to `false` by default, because minification is known to cause some issues, especially with HTML, so you need to be careful with this.

`$toFile` allows to output the data to a file, instead of to browser. Useful if you do not want to do this dynamically, but would rather prepare the files beforehand.

`$cacheStrat` is an optional caching strategy to apply (as described for [cacheControl](doc/Headers.md#cachecontrol))

`$exit` if set to `true` will exit the script, if to `false` - return an `int` representing the HTTP status code, unless text, font or some image/application MIME types is encountered: in this case `zEcho` will be used, which will exit the code regardless.

## forceClose
Expand Down
17 changes: 15 additions & 2 deletions doc/HTML.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# HTML
Functions, that generate useful HTML code.

##timeline
## timeline
```php
timeline(array $items, string $format = 'Y-m-d', bool $asc = false, string $lang = 'en', int $brLimit = 0);
```
Expand Down Expand Up @@ -41,9 +41,13 @@ Generates a timeline, sample of which (using [sample CSS](/src/timeline_sample.c
],
```
`format` - how to format time of each event, which is shown near the center delimiter. Needs to be a valid string, that can be parsed by `date()`.

`asc` - whether to sort the elements in ascending (`true`) or descending (`false`, default) order.

`lang` - if [SandClock](https://github.com/Simbiat/sand-clock) library is available this can be used to set language to `Elapsed time` field in respective events.

`brLimit` - option to allow up to the number of `<br>` elements between events. 1 `<br>` equals 1 month. Can be used to spread out the elements along the line to provide visual representation of time between events. `0` disables the feature.

Some clarifications about logic:
1. Elements with just `startTime` are considered "ongoing". Such elements will have extra class `timeline_current`, so that you can give them some extra style. If there are any "finished" events in the timeline, the "ongoing" ones will have shortcuts in beginning of the timeline.
2. Elements with just `endTime` or with identical `startTime` and `endTime` can be considered as "onetime" and will appear only on the right side with `timeline_right` class.
Expand All @@ -56,7 +60,9 @@ Some clarifications about logic:
breadcrumbs(array $items, bool $links = false, bool $headers = false);
```
Generates breadcrumbs for your website in Microdata format (as per https://schema.org/BreadcrumbList) wrapping it in proper `<nav>` tag with `id` attributes for `<li>`, `<a>`, `<span>` tags, as well as useful `aria` attributes, where applicable. `id` values are structures in a way, that allows you to style items depending on their "level" (for example always hide first element, since it's supposed to be your home page): first item will always have an `id` ending with `0` and the last one - with `1`.

`$items` is an array of the items (arrays) you plan to present as breadcrumbs. Each array needs to have `href` and `name` elements, otherwise it will be skipped.

`$links` - if set to `false`, you will get just a string of the requested breadcrumbs, but if set to `true`, this will also generate values for `rel="home index top begin prefetch"` and `rel="up prefetch"` required for `Links()` [function](Headers.md#links), and thus function will return an array like this:
```php
[
Expand All @@ -65,16 +71,21 @@ Generates breadcrumbs for your website in Microdata format (as per https://schem
]
```
You can then manually send the `'links'` array to `Links()` function to generate respective tags or headers.

`$headers` is checked only if `$links` is `true`. If `$headers` is also `true`, then it will directly send the `Link` header(s), and the return array value of `'links'` will have pre-generated set of `<link>` tags. While neither the headers, nor the tags are required, they may assist with navigation or performance improvement for the client (due to `prefetch`).

## pagination
```php
pagination(int $current, int $total, int $maxNumerics = 5, array $nonNumerics = ['first' => '<<', 'prev' => '<', 'next' => '>', 'last' => '>>', 'first_text' => 'First page', 'prev_text' => 'Previous page', 'next_text' => 'Next page', 'last_text' => 'Last page', 'page_text' => 'Page '], string $prefix = '', bool $links = false, bool $headers = false)
```
Generates pagination as `<ol>` list wrapped in `<nav>` with proper `id` and `aria` attributes.

`$current` - current page number.

`$total` - total number of pages.

`$maxNumerics` - maximum number of numeric links, that is those pages, that show actual numbers, and not 'First'/'Previous'/'Next'/'Last'. This number includes the current page.

`$nonNumerics` is an array of default text values to style 'First', 'Previous', 'Next' and 'Last' pages (with some default values):
```php
[
Expand All @@ -99,12 +110,14 @@ Generates pagination as `<ol>` list wrapped in `<nav>` with proper `id` and `ari
]
```
`$prefix` is an optional prefix for the links used in `href` attribute. Generally you will be ok with an empty string (default) and respective relative links, but in some cases, you may want to change that, for example, if your pages are using links like `#1` or `?page=1`. You can use that setting to adjust accordingly.
$links` - if set to `false`, you will get just a string of the requested pagination, but if set to `true`, this will also generate values for `rel="first prefetch"`, `rel="prev prefetch"`, `rel="next prefetch"` and `rel="last prefetch"` required for `Links()` [function](Headers.md#links), and thus function will return an array like this:

`$links` - if set to `false`, you will get just a string of the requested pagination, but if set to `true`, this will also generate values for `rel="first prefetch"`, `rel="prev prefetch"`, `rel="next prefetch"` and `rel="last prefetch"` required for `Links()` [function](Headers.md#links), and thus function will return an array like this:
```php
[
'pagination' => 'string_of_pagination',
'links' => [...],
]
```
You can then manually send the `'links'` array to `Links()` function to generate respective tags or headers.

`$headers` is checked only if `$links` is `true`. If `$headers` is also `true`, then it will directly send the `Link` header(s), and the return array value of `'links'` will have pre-generated set of `<link>` tags. While neither the headers, nor the tags are required, they may assist with navigation or performance improvement for the client (due to `prefetch`).
Loading

0 comments on commit c8c0fc5

Please sign in to comment.