Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
frostealth committed Aug 29, 2014
0 parents commit 091a144
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store
thumbs.db
*.class
*.pyc
*.pyo
*.swp
.project
.settings
.idea
composer.lock
vendor/*
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
PHP Data Storage
================

Example
-----
```php
<?php

$storage = new frostealth\Storage\Data(); // or $storage = new frostealth\Storage\Data($array);
$storage->set('login', '[email protected]');
// ...

if ($storage->has('login')) {
$login = $storage->get('login');
$storage->remove('login');
}

$storage->clear();

```
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "frostealth/php-data-storage",
"description": "PHP Data Storage",
"version" : "1.0.0",
"authors": [
{
"name": "Kudinov Ivan",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"frostealth\\Storage\\": "src/"
}
},
"require": {
"php": ">=5.4.0"
}
}
101 changes: 101 additions & 0 deletions src/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace frostealth\Storage;

/**
* Class Data
*
* @package frostealth\Storage
*/
class Data
{
/** @var array */
protected $data;

/**
* @param array $data
*/
public function __construct(array $data = [])
{
$this->fill($data);
}

/**
* @param string $key
* @param mixed $value
*/
public function set($key, $value)
{
$this->data[$key] = $value;
}

/**
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get($key, $default = null)
{
return $this->has($key) ? $this->data[$key] : $default;
}

/**
* @param string $key
*
* @return bool
*/
public function has($key)
{
return array_key_exists($key, $this->data);
}

/**
* @return array
*/
public function keys()
{
return array_keys($this->data);
}

/**
* @param string $key
*/
public function remove($key)
{
unset($this->data[$key]);
}

/**
* @param array $data
* @param bool $recursive
*/
public function replace(array $data, $recursive = false)
{
$function = $recursive ? 'array_replace_recursive' : 'array_replace';
$result = $function($this->data, $data);
$this->fill($result);
}

/**
* @param array $data
*/
public function fill(array $data)
{
$this->data = $data;
}

/**
* @return array
*/
public function all()
{
return $this->data;
}

public function clear()
{
$this->fill([]);
}
}

0 comments on commit 091a144

Please sign in to comment.