Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide additional functionality for handling widget configuration
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace Benrowe\Laravel\Widgets; | ||
|
||
use Arrilot\Widgets\AbstractWidget as BaseAbstractWidget; | ||
|
||
/** | ||
* | ||
*/ | ||
abstract class AbstractWidget extends BaseAbstractWidget | ||
{ | ||
/** | ||
* Constructor | ||
* Store the configuration & initialise the widget | ||
* | ||
* @param array $config | ||
*/ | ||
public function __construct($config = []) | ||
{ | ||
foreach ($config as $key => $value) { | ||
$this->addCfg($key, $value); | ||
} | ||
$this->init(); | ||
} | ||
|
||
/** | ||
* Custom initilisation for the widget | ||
* | ||
* @return boolean | ||
*/ | ||
protected function init() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Retrieve the configuration value from the widget, based | ||
* on the supplied key | ||
* | ||
* @param string $key | ||
* @param mixed $default | ||
* @return mixed | ||
*/ | ||
public function cfg($key, $default = null) | ||
{ | ||
if (array_key_exists($key, $this->config)) { | ||
return $this->config[$key]; | ||
} | ||
if ($this->isConfigProperty($key)) { | ||
return $this->$key; | ||
} | ||
return $default; | ||
} | ||
|
||
/** | ||
* Add a configuration into the widget | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return nil | ||
*/ | ||
public function addCfg($key, $value) | ||
{ | ||
if ($this->isConfigProperty($key)) { | ||
$this->$key = $value; | ||
return; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Determine if the public property exists, and is public | ||
* | ||
* @param string $propertyName | ||
* @return boolean | ||
*/ | ||
private function isConfigProperty($propertyName) | ||
{ | ||
try { | ||
$reflect = new \ReflectionClass($this); | ||
$property = $reflect->getProperty($propertyName); | ||
return $property->isPublic() && !$property->isStatic(); | ||
} catch (\ReflectionException $e) { | ||
return false; | ||
} | ||
|
||
} | ||
} |