phplibrary/src/Collection.php

101 lines
1.9 KiB
PHP

<?php
namespace ctiso;
/**
* Коллекция
*
*/
class Collection implements \ArrayAccess
{
/**
* Holds collective request data
*
* @var array
*/
protected $data = array();
/**
* Преобразование массива в коллекцию
*
* @param array $data
*/
public function import(array $data)
{
$this->data = array_merge($this->data, $data);
return true;
}
/**
* Преобразование коллекции в массив
*/
public function export()
{
return $this->data;
}
/**
* Store "request data" in GPC order.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function set($key, $value)
{
$this->data[$key] = $value;
}
/**
* Read stored "request data" by referencing a key.
*
* @param string $key
*
* @return mixed
*/
public function get($key, $default = null)
{
return isset($this->data[$key]) && $this->data[$key] != '' ? $this->data[$key] : $default;
}
public function getInt($key, $default = 0)
{
return (int)$this->get($key, $default);
}
public function getString($key, $default = '')
{
return (string)$this->get($key, $default);
}
public function getNat($key, $default = 1)
{
$result = (int)$this->get($key, $default);
return (($result > 0) ? $result : $default);
}
public function clear()
{
$this->data = [];
}
public function offsetSet($key, $value): void
{
$this->data[$key] = $value;
}
public function offsetExists($key): bool
{
return isset($this->data[$key]);
}
public function offsetUnset($key): void
{
unset($this->data[$key]);
}
public function offsetGet($key): mixed
{
return $this->data[$key] ?? null;
}
}