<?php
class Metabox
{
	public static $boxes = array();

	/**
	 * @static
	 * @param $type
	 * @param $options
	 * @return Metabox
	 */
	public static function factory($type, $options = null) {
		$parts = explode('_', $type);
		array_walk(
			$parts,
			function(&$item){
				$item = ucfirst($item);
			}
		);

		$class = 'Metabox_'.implode('_', $parts);

		if(class_exists($class)) {
			if( ! isset($options['type']) ) {
				$options['type'] = $type;
			}

			return new $class($options);
		}

		return false;
	}

	/**
	 * @static
	 * @param $key
	 * @return Metabox
	 */
	public static function get($key) {
		return (isset(self::$boxes[$key]) ? self::$boxes[$key] : null);
	}
	public static function attr($box, $attr) {
		return (isset(self::$boxes[$box]->$attr) ? self::$boxes[$box]->$attr : null);
	}
}