Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
YAML storage class providing simple inser/modify/delete operations.
<?php
/*
# Author: Tad DeVries
# Email: taddevries@gmail.com
# FileName: yamlStorage.class.php
#
# Description:
# This class provides a simple mechanism for inserting/modifying/deleting
# information in a YAML file.
#
#
# Copyright (C) 2012 Tad DeVries
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# License Info: http://www.gnu.org/licenses/gpl-2.0.html
#
#
*/
class yamlStorage {
private $yamlFile = "";
private $yamlArray = "";
//
// Class Constructor
//
public function __construct( $file ) {
$this->yamlFile = $file;
if( file_exists( $this->yamlFile ) ) {
echo "File found: {$this->yamlFile}<br/>\n";
$this->readFile();
}
else {
touch( $this->yamlFile );
echo "File created: {$this->yamlFile}\n";
}
}
//
// Class Destructor
//
public function __destruct() {
//destructor operations
}
//
// Read Function
//
private function readFile() {
$this->yamlArray = yaml_parse_file( $this->yamlFile );
}
//
// Write Function
//
private function writeFile() {
//the yaml_emit_file function is not fully implemented yet
//as of 5.4 it still produces an error when called
//yaml_emit_file( $this->yamlFile, $data );
$yamlData = yaml_emit ( $this->yamlArray );
$fp = fopen ( $this->yamlFile, 'w' );
fwrite ( $fp, $yamlData );
fclose ( $fp );
}
//
// Data Parsing Function
// Input Expected: (String) "key[.key.key[:value]]"
//
private function parseData($data) {
$parsedString = explode(":", $data);
if(isset($parsedString[1]))
$value = $parsedString[1];
else
$value = "";
$parsedKeys = explode(".", $parsedString[0]);
$keysCount = count($parsedKeys);
$dataArray = array( $parsedKeys[$keysCount-1] => $value);
for($i=$keysCount-2; $i>=0; $i--) {
$dataArray = array($parsedKeys[$i] => $dataArray);
}
return $dataArray;
}
//
// Multidimensional Array Diff
//
private function check_diff_multi($array1, $array2) {
$result = array();
foreach($array1 as $key => $val) {
if(isset($array2[$key])){
if(is_array($val) && is_array($array2[$key])) {
$result[$key] = $this->check_diff_multi($val, $array2[$key]);
}
} else {
$result[$key] = $val;
}
}
return $result;
}
//
// Insert Function
//
public function insert($data) {
$dataArray = $this->parseData($data);
if(array_key_exists(key($dataArray), $this->yamlArray)) {
echo "Key Already exists<br/>\n";
}
else {
$this->yamlArray = $this->yamlArray + $dataArray;
$this->writeFile();
}
}
//
// Modify Function
//
public function modify($data) {
$dataArray = $this->parseData($data);
if(array_key_exists(key($dataArray), $this->yamlArray)) {
$this->yamlArray = array_replace_recursive($this->yamlArray, $dataArray);
$this->writeFile();
}
else {
echo "Key Not Found<br/>\n";
}
}
//
// Delete Function
//
public function delete($data) {
$dataArray = $this->parseData($data);
$this->yamlArray = $this->check_diff_multi($this->yamlArray, $dataArray);
$this->writeFile();
}
//
// Get Function
// Not fully implemented yet
//
public function get($data) {
//$dataArray = $this->parseData($data);
//$temp = $this->check_diff_multi($dataArray, $this->yamlArray);
//print_r($temp);
}
//
// Display Function
//
public function display() {
echo "<code>\n";
print_r( $this->yamlArray );
echo "</code><br/>\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.