-
Notifications
You must be signed in to change notification settings - Fork 0
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 d63d599
Showing
3 changed files
with
141 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,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" | ||
} | ||
} |
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,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 ); | ||
} | ||
|
||
} |
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,7 @@ | ||
# Data Store | ||
A PHP utility class for storing and accessing data. | ||
|
||
## Install | ||
```$xslt | ||
composer install wp-forge/data-store | ||
``` |