PHP code example of migears / utils

1. Go to this page and download the library: Download migears/utils library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

migears / utils example snippets


use MiGears\Utils\URL;

// A string carrying its own scheme parses as-is
$url = URL::parse('https://example.com/path?page=1');

$url->scheme();             // 'https'
$url->host();               // 'example.com'
$url->path();               // '/path'
$url->query();              // ['page' => '1']
$url->queryParam('page');   // '1'
$url->isHttps();            // true

// http() and https() force their scheme
URL::https('example.com/path');    // https://example.com/path
URL::http('https://example.com');  // http://example.com/

// Without a scheme and without a factory, the string is rejected
URL::parse('example.com/path');    // throws InvalidArgumentException

$secure = URL::parse('https://user:[email protected]/');

$secure->user();   // 'user'
$secure->pass();   // 'secret'
echo $secure;      // 'https://user:[email protected]/'

$secure->withUser(null)->toString();   // 'https://example.com/'

new URL(scheme: '1a', host: 'example.com');            // throws — not a valid scheme
new URL(scheme: 'https', host: 'exa mple.com');        // throws — a host cannot carry a space
new URL(scheme: 'https', host: 'a.com', port: 70000);  // throws — not a destination port
(new URL(scheme: 'https', host: 'a.com'))->withPath('/a b/c')->toString();
// 'https://a.com/a%20b/c'

$url = URL::https('example.com')
    ->withPath('/users')
    ->withQuery(['page' => 1, 'limit' => 20])
    ->withFragment('top')
    ->appendPath('search');

echo $url;  // 'https://example.com/users/search?page=1&limit=20#top'

URL::parse('https://example.com/?a.b=1&c%20d=2')->query();
// ['a.b' => '1', 'c d' => '2']

use MiGears\Utils\Paginator;

$paginator = Paginator::create(
    pageSize: 10,
    currentPage: 2,
    total: 53,
    items: [...],
);

$paginator->totalPages();   // 6
$paginator->hasPrev();      // true
$paginator->hasNext();      // true
$paginator->prevPage();     // 1
$paginator->nextPage();     // 3
$paginator->offset();       // 10
$paginator->toArray();      // for JSON response

use MiGears\Utils\FlowPaginator;

$paginator = FlowPaginator::first(20, 'id');

// After fetching data...
$next = $paginator->nextPage($items, $hasMore);

$next->nextCursor();        // cursor for the next page
$next->hasMore();           // whether there are more pages
$next->toArray();           // for JSON response

use MiGears\Utils\URL;

// 自带 scheme 的字符串按原样解析
$url = URL::parse('https://example.com/path?page=1');

$url->scheme();             // 'https'
$url->host();               // 'example.com'
$url->path();               // '/path'
$url->query();              // ['page' => '1']
$url->queryParam('page');   // '1'
$url->isHttps();            // true

// http() 与 https() 强制指定协议
URL::https('example.com/path');    // https://example.com/path
URL::http('https://example.com');  // http://example.com/

// 既没有 scheme 也没走工厂方法,直接拒绝
URL::parse('example.com/path');    // 抛出 InvalidArgumentException

$secure = URL::parse('https://user:[email protected]/');

$secure->user();   // 'user'
$secure->pass();   // 'secret'
echo $secure;      // 'https://user:[email protected]/'

$secure->withUser(null)->toString();   // 'https://example.com/'

new URL(scheme: '1a', host: 'example.com');            // 抛出 — 不是合法 scheme
new URL(scheme: 'https', host: 'exa mple.com');        // 抛出 — host 不能含空格
new URL(scheme: 'https', host: 'a.com', port: 70000);  // 抛出 — 不是可用的目标端口
(new URL(scheme: 'https', host: 'a.com'))->withPath('/a b/c')->toString();
// 'https://a.com/a%20b/c'

$url = URL::https('example.com')
    ->withPath('/users')
    ->withQuery(['page' => 1, 'limit' => 20])
    ->withFragment('top')
    ->appendPath('search');

echo $url;  // 'https://example.com/users/search?page=1&limit=20#top'

URL::parse('https://example.com/?a.b=1&c%20d=2')->query();
// ['a.b' => '1', 'c d' => '2']

use MiGears\Utils\Paginator;

$paginator = Paginator::create(
    pageSize: 10,
    currentPage: 2,
    total: 53,
    items: [...],
);

$paginator->totalPages();   // 6
$paginator->hasPrev();      // true
$paginator->hasNext();      // true
$paginator->prevPage();     // 1
$paginator->nextPage();     // 3
$paginator->offset();       // 10
$paginator->toArray();      // 用于 JSON 响应

use MiGears\Utils\FlowPaginator;

$paginator = FlowPaginator::first(20, 'id');

// 获取数据后...
$next = $paginator->nextPage($items, $hasMore);

$next->nextCursor();        // 下一页的游标
$next->hasMore();           // 是否还有更多页
$next->toArray();           // 用于 JSON 响应