Skip to content

Instantly share code, notes, and snippets.

@RalfAlbert
Last active December 10, 2015 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RalfAlbert/4393733 to your computer and use it in GitHub Desktop.
Save RalfAlbert/4393733 to your computer and use it in GitHub Desktop.
Managing complex datastructures with a class
<?php
class CollectionManagement
{
/**
* Option key
* @var string
*/
const OPTION_KEY = 'collection_settings';
/**
* Collections from DB
* @var array
*/
public $collections = array();
/**
* Results of methods
* @var boolean|array
*/
public $result = null;
/**
* Get the collections from db on creating an instance
*/
public function __construct(){
$this->get_collections_from_db();
}
/**
* Write collections to db on destruction
*/
public function __destruct(){
$this->store_colletion_to_db();
}
/**
* Reset the internal result, get collections from db if not already done
*/
protected function reset(){
if( empty( $this->collections ) )
$this->get_collections_from_db();
$this->result = false;
return ( ! empty( $this->collections ) );
}
/**
* Get a collection by it's id
* @param string $id ID of the collection
* @param string $first_match Whether to return the first match or the last match
* @return boolean|array False if the collection was not found, else the (first|last) matching collection
*/
protected function seek_collection_by_id( $id = '', $first_match = true ){
$result = false;
foreach( $this->collections as $collection ){
if( isset( $collection[$id] ) && $id === $collection[$id] ){
$result = $collection;
if( true === $first_match )
break;
}
}
return $result;
}
/**
* Store collections in db
* @return boolean Always true
*/
public function store_colletions_to_db(){
update_option( self::OPTION_KEY, $this->collections );
return true;
}
/**
* Get the collections from db
* @return boolean Always true
*/
public function get_collections_from_db(){
$this->collections = get_option( self::OPTION_KEY );
return true;
}
/**
* Update a collection
* @param array $collection
* @return boolean True on success, false on error
*/
public function update_collection( $collection = array() ){
if( ! is_array( $collection ) || empty( $collection ) )
return false;
$this->reset();
$this->collections[$collection['id']] = $collection;
// maybe store collections immediately
//$this->store_colletions_to_db();
$this->result = isset( $this->collections[$collection['id']] );
return $this->result;
}
/**
* Get a collection by it's id
* @param string $id
* @return boolean|array False if the collection was not found, else the collection
*/
public function get_collection_by_id( $id = '' ){
if( empty( $id ) )
return false;
$this->reset();
$this->result = $this->seek_collection_by_id( $id );
return $this->result;
}
/**
* Get the elements of a collection by the collection id
* @param string $id
* @return boolean|array False if the elemnts could not be found, else array with the collection elements
*/
public function get_elements_by_id( $id = '' ){
if( empty( $id ) )
return false;
$this->reset();
$collection = $this->seek_collection_by_id( $id );
$this->result = $collection['elements'];
return $this->result;
}
/**
* Insert an element-array into a collection identified by it's id
* @param string $id
* @param array $element
* @return string|int The id of the inserted element
*/
public function insert_element_by_id( $id = '', $element = array() ){
if( empty( $id ) || empty( $element ) )
return false;
$element = array_merge(
array( 'id' => 0, 'name' => '', 'url' => '' ),
$element
);
$this->reset();
$collection = $this->seek_collection_by_id( $id );
// check elements if is an array
if( ! is_array( $collection['elements'] ) )
$collection['elements'] = array();
// prevent using an id twice
$ids = array();
foreach( $collection['elements'] as $single_element ){
if( isset( $single_element['id'] ) && ! empty( $single_element['id'] ) )
array_push( $ids, $single_element['id'] );
}
// if the id already exists, add an random number to prevent double ids
if( in_array( $element['id'], $ids ) )
$element['id'] .= rand( 1, 999 );
array_push( $collection['elements'], $element );
$this->update_collection( $collection );
$this->result = $element['id'];
return $this->result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment