Created
November 24, 2012 13:13
-
-
Save rossriley/4139637 to your computer and use it in GitHub Desktop.
Trait for defaults and settings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait Configuration{ | |
protected static $_defaults; | |
protected $_settings; | |
public function settings($key){ | |
if(isset($this->_settings[$key]) && is_callable($this->_settings[$key])) return call_user_func($this->_settings[$key]); | |
if(isset($this->_settings[$key])) return $this->_settings[$key]; | |
if(isset(self::$_defaults[$key]) && is_callable(self::$_defaults[$key])) return call_user_func(self::$_defaults[$key]); | |
if(isset(self::$_defaults[$key])) return self::$_defaults[$key]; | |
return false; | |
} | |
static public function defaults($defaults) { | |
if(!is_array($defaults) && !$defaults instanceof ArrayAccess) { | |
throw new \Exception("Supplied default settings could not be used. Array or Array Object required"); | |
} | |
self::$_defaults = $defaults; | |
} | |
public function configure($settings) { | |
if(!is_array($settings) && !$settings instanceof ArrayAccess) { | |
throw new \Exception("Supplied settings could not be used. Array or Array Object required"); | |
} | |
$this->_settings = $settings; | |
} | |
public function __get($value) { | |
return $this->settings($value); | |
} | |
} | |
abstract class Model{ | |
use Configuration; | |
public function __construct($settings) { | |
$this->configure($settings); | |
} | |
} | |
class ExampleModel extends Model { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment