public
Last active

Managing complex datastructures with a class

  • Download Gist
class-collectionmanager.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
<?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;
 
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.