1. Go to this page and download the library: Download k-gun/xo 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/ */
k-gun / xo example snippets
use xo\Collection;
use function xo\set;
// Declare a typed array as Poll
class Poll extends Collection {
// Define a custom method
public function getResults(): array {
// Use copy(), so do not modify Poll instance
return $this->copy()->map(function ($option) {
// Use set() shortcut and sum avarage of option values
return set($option)->sumAvg(2);
})->sort('asort', function ($a, $b) {
// Use reverse sort preserving keys
return $a < $b;
})->toArray();
}
}
// Create an instance of Poll
$poll = new Poll([
// Fill with options
'option_1' => [2, 2, 1],
'option_2' => [5, 1, 5],
'option_3' => [3, 5, 2]
]);
var_export($poll->getResults()); //=> ['option_2' => 3.67, 'option_3' => 3.33, 'option_1' => 1.67]
$array = new xo\TypedArray('IntArray', [1, 2, 3], 'int');
$array->sum(); //=> 6
$array->append(4);
$array->sum(); //=> 10
// this will throw a xo\exception\MethodException
// 'cos append() does not take string value
$array->append('4');
$map = new xo\AnyArray(['a' => 1, 'b' => 2]);
$map->sum(); //=> 3
// add new item
$map->append(3);
$map->sum(); //=> 6
$map = new xo\Map(['a' => 1, 'b' => 2]);
$map->sum(); //=> 3
// this will throw a xo\exception\MethodException
// 'cos append() does not take a key but only value
$map->append(3);
// add new item with key,value pairs using set(), or push() as well
$map->set('c', 3);
$map->sum(); //=> 6
$map = new xo\Set(1, 2);
$map->sum(); //=> 3
// add new item without key
$map->append(3);
// or with key,value pairs using set(), or push() as well
$map->set(2, 3);
$map->sum(); //=> 6
$map = new xo\Tuple(1, 2);
$map->sum(); //=> 3
// this will throw a xo\exception\MethodException
// 'cos append() is not allowed for Tuple objects
$map->append(3);
class IntArray extends xo\TypedArray {
public function __construct(array $items = null) {
parent::__construct('IntArray', $items, 'int');
}
}
$array = new IntArray();
$array->append(1);
$array->append(2);
$array->append('3'); //=> error
class Poll extends xo\TypedArray {
public function __construct(array $items = null) {
parent::__construct('Poll', $items, 'array');
}
public function getResults(): array {
return $this->copy()->map(function ($option) {
return round(array_sum($option) / count($option), 2);
// or
// return xo\set($option)->sumAvg(2);
// return (new xo\Set($option))->sumAvg(2);
})->sort('asort', function ($a, $b) {
return $a < $b;
})->toArray();
}
}
$poll = new Poll();
$poll->put('option_1', [2, 2, 1]);
$poll->put('option_2', [5, 1, 5]);
$poll->put('option_3', [3, 5, 2]);
var_export($poll->getResults()); //=> ['option_2' => 3.67, 'option_3' => 3.33, 'option_1' => 1.67]
class Poll extends xo\AnyArray {
public function getResults(): array {
return $this->copy()->map(function ($option) {
return round(array_sum($option) / count($option), 2);
})->sort('asort', function ($a, $b) {
return $a < $b;
})->toArray();
}
}
class User {
private $id;
public function __construct(int $id = null) {
$this->id = $id;
}
}
class Users extends xo\TypedArray {
public function __construct(array $items = null) {
parent::__construct('Users', $items, User::class);
}
}
$users = new Users();
$users->add(new User(1));
$users->add(new User(2));
$users->add(new User(null));
var_dump($users->copy()->filter(function (User $user) {
return !is_null($user->id);
})->count()); //=> int(2)
// this will throw a xo\exception\ValueException
// 'cos Users accepts User type values only
$users->add('boom!');
class Users extends xo\Set {
public function __construct(array $items = null) {
parent::__construct($items, User::class);
}
}
$users = new Users();
$users->add(new User(1));
$users->add(new User(2));
$users->add(new User(null));
...
// or
class Users extends xo\Map {
public function __construct(array $items = null) {
parent::__construct($items, User::class);
}
}
$users = new Users();
$users->put(1, new User(1));
$users->put(2, new User(2));
$users->put('null', new User(null));
...
class xo\AnyArray extends xo\TypedArray {}
protected static array $notAllowedMethods = []
public __construct(array $items = null, bool $readOnly = false)
public search(any $value): int|string|null
public searchLast(any $value): int|string|null
public indexOf(any $value): ?int
public lastIndexOf(any $value): ?int
public has(any $value): bool
public hasKey(int|string $key): bool
public set(int|string $key, any $value, int &$size = null): self
public get(int|string $key, any $valueDefault = null, bool &$found = null): ?any
public add(any $value): self
public remove(any $value, bool &$found = null): self
public removeAt(int|string $key, bool &$found = null): self
public removeAll(array $values, int &$count = null): self
public append(any $value, int &$size = null): self
public prepend(any $value, int &$size = null): self
public pop(int &$size = null): ?any
public unpop(any $value, int &$size = null): self
public shift(int &$size = null): ?any
public unshift(any $value, int &$size = null): self
public put(int|string $key, any $value): self
public push(int|string $key, any $value): self
public pull(int|string $key, any $valueDefault = null, bool &$found = null): ?any
public replace(any $value, any $replaceValue, bool &$found = null): self
public replaceAt(int|string $key, any $replaceValue, bool &$found = null): self
public flip(): self
throws xo\ArrayException
public pad(int $times, any $value, int $offset = null): self
class xo\Map extends xo\TypedArray {}
protected static array $notAllowedMethods = ['flip', 'add', 'append', 'prepend', 'unpop',
'unshift', 'flip', 'pad']
public __construct(array $items = null, string $itemsType = null,
bool $readOnly = false, bool $allowNulls = false)
public search(any $value): int|string|null
public searchLast(any $value): int|string|null
public indexOf(any $value): ?int
public lastIndexOf(any $value): ?int
public has(any $value): bool
public hasKey(string $key): bool
public set(string $key, any $value, int &$size = null): self
public get(string $key, any $valueDefault = null, bool &$found = null): ?any
public remove(any $value, bool &$found = null): self
public removeAt(string $key, bool &$found = null): self
public removeAll(array $values, int &$count = null): self
public pop(int &$size = null): ?any
public shift(int &$size = null): ?any
public put(string $key, any $value): self
public push(string $key, any $value): self
public pull(string $key, any $valueDefault = null, bool &$found = null): ?any
public replace(any $value, any $replaceValue, bool &$found = null): self
public replaceAt(string $key, any $replaceValue, bool &$found = null): self
class xo\Set extends xo\TypedArray {}
protected static array $notAllowedMethods = ['flip']
public __construct(array $items = null, string $itemsType = null,
bool $readOnly = false, bool $allowNulls = false)
public search(any $value): int|string|null
public searchLast(any $value): int|string|null
public indexOf(any $value): ?int
public lastIndexOf(any $value): ?int
public has(any $value): bool
public hasKey(int $key): bool
public set(int $key, any $value, int &$size = null): self
public get(int $key, any $valueDefault = null, bool &$found = null): ?any
public add(any $value): self
public remove(any $value, bool &$found = null): self
public removeAt(int $key, bool &$found = null): self
public removeAll(array $values, int &$count = null): self
public append(any $value, int &$size = null): self
public prepend(any $value, int &$size = null): self
public pop(int &$size = null): ?any
public unpop(any $value, int &$size = null): self
public shift(int &$size = null): ?any
public unshift(any $value, int &$size = null): self
public put(int $key, any $value): self
public push(int $key, any $value): self
public pull(int $key, any $valueDefault = null, bool &$found = null): ?any
public replace(any $value, any $replaceValue, bool &$found = null): self
public replaceAt(int $key, any $replaceValue, bool &$found = null): self
public pad(int $times, any $value, int $offset = null): self
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.