| 1 |
<?php defined('ABSPATH') or die; |
| 2 |
|
| 3 |
/* This file is property of Pixel Grade Media. You may NOT copy, or redistribute |
| 4 |
* it. Please see the license that came with your copy for more information. |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* A HTMLElement is a HTMLTag with meta support integrated into it. A normal |
| 9 |
* HTMLTag only cares for it's attributes meta and nothing else, but more |
| 10 |
* specialized tags such as forms or form fields require misc metadata to be |
| 11 |
* attached on the object itself. |
| 12 |
* |
| 13 |
* @package pixcustomify |
| 14 |
* @category core |
| 15 |
* @author Pixel Grade Team |
| 16 |
* @copyright (c) 2013, Pixel Grade Media |
| 17 |
*/ |
| 18 |
class PixCustomifyHTMLElementImpl extends PixCustomifyHTMLTagImpl implements PixCustomifyHTMLElement { |
| 19 |
|
| 20 |
/** @var array configuration values */ |
| 21 |
protected $meta = null; |
| 22 |
|
| 23 |
/** |
| 24 |
* @param array config |
| 25 |
*/ |
| 26 |
static function instance($config = null) { |
| 27 |
$i = new self; |
| 28 |
$i->configure($config); |
| 29 |
return $i; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Apply configuration. |
| 34 |
*/ |
| 35 |
protected function configure($config = null) { |
| 36 |
// gurantee configuration |
| 37 |
$config !== null or $config = array(); |
| 38 |
|
| 39 |
// invoke htmltag instance configuration |
| 40 |
if (isset($config['attrs'])) { |
| 41 |
parent::configure($config['attrs']); |
| 42 |
unset($config['attrs']); |
| 43 |
} |
| 44 |
else { // no html attributes set |
| 45 |
parent::configure(array()); |
| 46 |
} |
| 47 |
|
| 48 |
// setup meta fields |
| 49 |
$this->meta = pixcustomify::instance('PixCustomifyMeta', $config); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
// Meta |
| 54 |
// ------------------------------------------------------------------------ |
| 55 |
|
| 56 |
/** |
| 57 |
* @param string meta key |
| 58 |
* @return boolean true if key exists, false otherwise |
| 59 |
*/ |
| 60 |
function hasmeta($key) { |
| 61 |
return $this->meta->has($key); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @return mixed value or default |
| 66 |
*/ |
| 67 |
function getmeta($key, $default = null) { |
| 68 |
return $this->meta->get($key, $default); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return static $this |
| 73 |
*/ |
| 74 |
function setmeta($key, $value) { |
| 75 |
$this->meta->set($key, $value); |
| 76 |
return $this; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Set the key if it's not already set. |
| 81 |
* |
| 82 |
* @param string key |
| 83 |
* @param string value |
| 84 |
*/ |
| 85 |
function ensuremeta($key, $value) { |
| 86 |
$this->meta->ensure($key, $value); |
| 87 |
return $this; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* If the key is currently a non-array value it will be converted to an |
| 92 |
* array maintaning the previous value (along with the new one). |
| 93 |
* |
| 94 |
* @param string name |
| 95 |
* @param mixed value |
| 96 |
* @return static $this |
| 97 |
*/ |
| 98 |
function addmeta($name, $value) { |
| 99 |
$this->meta->add($name, $value); |
| 100 |
return $this; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @return PixCustomifyMeta |
| 105 |
*/ |
| 106 |
function meta() { |
| 107 |
return $this->meta; |
| 108 |
} |
| 109 |
|
| 110 |
} # class |
| 111 |
|