Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New SettingsInterface #2505

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion modules/ppcp-wc-gateway/src/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Class Settings
*/
class Settings implements ContainerInterface {
class Settings implements SettingsGetterInterface, ContainerInterface {

const KEY = 'woocommerce-ppcp-settings';
const CONNECTION_TAB_ID = 'ppcp-connection';
Expand Down Expand Up @@ -103,6 +103,19 @@ public function has( $id ) {
return array_key_exists( $id, $this->settings );
}

/**
* {@inheritDoc}
*
* Replaces `$settings->has() ? $settings->get() : $default` chains.
*/
public function get_value( string $key, $default = null ) {
try {
return $this->get( $key );
} catch ( NotFoundException $e ) {
return $default;
}
}

/**
* Sets a value.
*
Expand Down
30 changes: 30 additions & 0 deletions modules/ppcp-wc-gateway/src/Settings/SettingsGetterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Interface for settings-accessor classes.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Settings
*/

declare( strict_types = 1 );

namespace WooCommerce\PayPalCommerce\WcGateway\Settings;

/**
* Interface for accessing plugin settings (read-only).
*
* Implements a WordPress-like settings access signature: `get_value()` is inspired by the
* `get_option()` core method.
*
* In contrast to the `ContainerInterface`, this interface will never throw an Exception.
*/
interface SettingsGetterInterface {
/**
* Get a single value from stored settings.
*
* @param string $key The key to retrieve.
* @param mixed $default The default value if the key is not found.
*
* @return mixed The value or the default.
*/
public function get_value( string $key, $default = null );
}
Loading