Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
wpscholar committed Mar 17, 2020
0 parents commit d63d599
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "wp-forge/data-store",
"description": "A PHP utility class for storing and accessing data.",
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Micah Wood",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"WP_Forge\\DataStore\\": "includes"
}
},
"require": {
"wp-forge/helpers": "^1.0"
}
}
115 changes: 115 additions & 0 deletions includes/DataStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace WP_Forge\DataStore;

use WP_Forge\Helpers\Arr;

/**
* Class DataStore
*
* @package WP_Forge\DataStore
*/
class DataStore {

/**
* In-memory data store.
*
* @var array
*/
protected $data = [];

/**
* Delete a value by key.
*
* @param string $key
*
* @return $this
*/
public function forget( $key ) {
Arr::forget( $this->data, $key );

return $this;
}

/**
* Get a value by key.
*
* @param string $key
* @param null $default
*
* @return mixed
*/
public function get( $key, $default = null ) {
return Arr::get( $this->data, $key, $default );
}

/**
* Check for the existence of a key.
*
* @param string $key
*
* @return bool
*/
public function has( $key ) {
return Arr::has( $this->data, $key );
}

/**
* Push multiple entries into the data store at once.
*
* @param array $data
*
* @return $this
*/
public function put( array $data ) {
$this->data = array_merge_recursive( $this->data, $data );

return $this;
}

/**
* Reset/overwrite the data store.
*
* @param array $data
*
* @return $this
*/
public function reset( array $data = [] ) {
$this->data = $data;

return $this;
}

/**
* Set a value.
*
* @param string $key
* @param mixed $value
*
* @return $this
*/
public function set( $key, $value ) {
Arr::set( $this->data, $key, $value );

return $this;
}

/**
* Return all the data as an array.
*
* @return array
*/
public function toArray() {
return $this->data;
}

/**
* Return all the data as JSON.
*
* @return string|false JSON string on success, false on failure.
*/
public function toJson() {
return json_encode( (object) $this->data, JSON_PRETTY_PRINT );
}

}
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Data Store
A PHP utility class for storing and accessing data.

## Install
```$xslt
composer install wp-forge/data-store
```

0 comments on commit d63d599

Please sign in to comment.