| 1 |
<?php defined('ABSPATH') or die; |
| 2 |
|
| 3 |
/** |
| 4 |
* @package pixcustomify |
| 5 |
* @category core |
| 6 |
* @author Pixelgrade Team |
| 7 |
* @copyright (c) 2013, Pixelgrade |
| 8 |
*/ |
| 9 |
class PixCustomifyMetaImpl implements PixCustomifyMeta { |
| 10 |
|
| 11 |
/** @var array metadat */ |
| 12 |
protected $metadata = array(); |
| 13 |
|
| 14 |
/** |
| 15 |
* @param array metadata |
| 16 |
* @return PixCustomifyMeta |
| 17 |
*/ |
| 18 |
static function instance($metadata) { |
| 19 |
$i = new self; |
| 20 |
$i->metadata = $metadata; |
| 21 |
return $i; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @param string meta key |
| 26 |
* @return boolean true if key exists, false otherwise |
| 27 |
*/ |
| 28 |
function has($key) { |
| 29 |
return isset($this->metadata[$key]); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @param string key |
| 34 |
* @param mixed default |
| 35 |
* @return mixed |
| 36 |
*/ |
| 37 |
function get($key, $default = null) { |
| 38 |
return $this->has($key) ? $this->metadata[$key] : $default; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @param string key |
| 43 |
* @param mixed value |
| 44 |
* @return static $this |
| 45 |
*/ |
| 46 |
function set($key, $value) { |
| 47 |
$this->metadata[$key] = $value; |
| 48 |
return $this; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Set the key if it's not already set. |
| 53 |
* |
| 54 |
* @param string key |
| 55 |
* @param string value |
| 56 |
*/ |
| 57 |
function ensure($key, $value) { |
| 58 |
if ( ! $this->has($key)) { |
| 59 |
$this->set($key, $value); |
| 60 |
} |
| 61 |
|
| 62 |
return $this; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* If the key is currently a non-array value it will be converted to an |
| 67 |
* array maintaning the previous value (along with the new one). |
| 68 |
* |
| 69 |
* @param string name |
| 70 |
* @param mixed value |
| 71 |
* @return static $this |
| 72 |
*/ |
| 73 |
function add($name, $value) { |
| 74 |
|
| 75 |
// Cleanup |
| 76 |
// ------- |
| 77 |
|
| 78 |
if ( ! isset($this->metadata[$name])) { |
| 79 |
$this->metadata[$name] = array(); |
| 80 |
} |
| 81 |
else if ( ! is_array($this->metadata[$name])) |
| 82 |
{ |
| 83 |
$this->metadata[$name] = array($this->metadata[$name]); |
| 84 |
} |
| 85 |
# else: array, no cleanup required |
| 86 |
|
| 87 |
// Register new value |
| 88 |
// ------------------ |
| 89 |
|
| 90 |
$this->metadata[$name][] = $value; |
| 91 |
|
| 92 |
return $this; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @return array all metadata as array |
| 97 |
*/ |
| 98 |
function metadata_array() { |
| 99 |
return $this->metadata; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Shorthand for a calling set on multiple keys. |
| 104 |
* |
| 105 |
* @return static $this |
| 106 |
*/ |
| 107 |
function overwritemeta($overwrites) { |
| 108 |
foreach ($overwrites as $key => $value) { |
| 109 |
$this->set($key, $value); |
| 110 |
} |
| 111 |
|
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
} # class |
| 116 |
|