Skip to content

Commit

Permalink
Update getSetting method, to work both with arrays and objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Dec 31, 2015
1 parent 62cb60f commit 58d42ad
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/Phramework.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
namespace Phramework;

use \Phramework\Models\Util;
use \Phramework\Models\Request;
use \Phramework\Extensions\StepCallback;

Expand All @@ -40,11 +39,12 @@
* </ul>
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @version 1.0.0
* @version 1.1.0
* @link https://nohponex.gr Developer's website
* @todo Clean GET callback
* @todo Add translation class
* @todo Add allowed origin settings and implementation
* @todo Add setting for timezone
*/
class Phramework
{
Expand Down Expand Up @@ -113,7 +113,7 @@ public function __construct(
true
)) {
throw new \Phramework\Exceptions\ServerException(
'Class is not implementing \Phramework\URIStrategy\IURIStrategy'
'Class is not implementing Phramework\URIStrategy\IURIStrategy'
);
}
self::$URIStrategy = $URIStrategyObject;
Expand Down Expand Up @@ -149,7 +149,7 @@ public static function setTranslation($translationObject)
true
)) {
throw new \Exception(
'Class is not implementing \Phramework\Extensions\Translation'
'Class is not implementing Phramework\Extensions\Translation'
);
}

Expand Down Expand Up @@ -648,26 +648,39 @@ public static function getCallback()
/**
* Get a setting value
* @param string $key The requested setting key
* @param string|NULL $secondLevel
* @param string|null $secondLevel
* @param mixed $defaultValue [optional] Default value is the setting is missing.
* @return Mixed Returns the value of setting, NULL when not found
* @return Mixed Returns the value of setting, null when not found
*/
public static function getSetting($key, $secondLevel = null, $defaultValue = null)
{
$settings = self::$settings;

//Work with arrays
if (is_object($settings)) {
$settings = (array)$settings;
}

//Use default value if setting is not defined
if (!isset(self::$settings[$key])
|| ($secondLevel !== null
&& !isset(self::$settings[$key][$secondLevel])
)
) {
if (!array_key_exists($key, $settings)) {
return $defaultValue;
}

//If second level setting access is set
if ($secondLevel !== null) {
return self::$settings[$key][$secondLevel];
//Work with arrays
if (is_object($settings[$key])) {
$settings[$key] = (array)$settings[$key];
}

if (!array_key_exists($secondLevel, $settings[$key])) {
return $defaultValue;
}

return $settings[$key][$secondLevel];
}

return self::$settings[$key];
return $settings[$key];
}

/**
Expand All @@ -678,7 +691,7 @@ public static function setViewer($viewerClass)
{
if (!is_subclass_of($viewerClass, \Phramework\Viewers\IViewer::class, true)) {
throw new \Exception(
'Class is not implementing \Phramework\Viewers\IViewer'
'Class is not implementing Phramework\Viewers\IViewer'
);
}
self::$viewer = $viewerClass;
Expand Down

0 comments on commit 58d42ad

Please sign in to comment.