phplibrary/src/collection.php
2017-02-09 14:57:40 +03:00

100 lines
1.9 KiB
PHP

<?php
/**
* Коллекция
*
* package core
*/
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] : $default;
}
public function getInt($key, $default = 0)
{
return intval($this->get($key, $default));
}
public function getString($key, $default = '')
{
return ((string) $this->get($key, $default));
}
public function getNat($key, $default = 1)
{
$result = intval($this->get($key, $default));
return (($result > 0) ? $result : $default);
}
public function clear()
{
$this->data = array();
}
public function offsetSet($key, $value)
{
$this->data[$key] = $value;
}
public function offsetExists($key)
{
return isset($this->data[$key]);
}
public function offsetUnset($key)
{
unset($this->data[$key]);
}
public function offsetGet($key)
{
return isset($this->data[$key]) ? $this->data[$key] : null;
}
}