-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 091a144
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]); | ||
} | ||
} | ||
|