\donbidon\Core\RegistryCommon

Static and non-static registry functionality.

Supports plain access rights and storing values by references.

use \donbidon\Core\Registry\Common;

$registry = new Common[
    'key_1'     => "value_1",
    'key_2'     => "value_2",
    'ref_1'     => "~~> ref_2",
    'ref_2'     => "~~> ref_value",
    'ref_value' => "final reference value",
], Common::ACTION_ALL & ~Common::ACTION_MODIFY);
var_dump($registry->exists('key_1'));
var_dump($registry->exists('key_3'));
var_dump($registry->get('key_3', "default value"));
var_dump($registry->get('ref_1'));
var_dump($registry->get('ref_2'));
$registry->set('key_3', "value_3");
$registry->set('key_1', "value_11");

outputs

bool(true)
bool(false)
string(13) "default value"
string(21) "final reference value"
string(21) "final reference value"

Fatal error: Uncaught RuntimeException: ACTION_MODIFY: no permissions for key 'key_1'

Summary

Methods
Properties
Constants
getInstance()
_set()
_exists()
_isEmpty()
_get()
_delete()
_override()
__construct()
set()
exists()
isEmpty()
get()
delete()
override()
newFromKey()
No public properties found
ACTION_NONE
ACTION_CREATE
ACTION_MODIFY
ACTION_DELETE
ACTION_OVERRIDE
ACTION_ALL
REFERENCES
ALL_INCLUSIVE
checkPermissions()
getByRef()
isRef()
$instance
$scope
$options
$checkRefs
$refPattern
N/A
No private methods found
No private properties found
N/A

Constants

ACTION_NONE

ACTION_NONE = 0

Disallow all actions

ACTION_CREATE

ACTION_CREATE = 1

Allow to create new keys

ACTION_MODIFY

ACTION_MODIFY = 2

Allow to modify existing keys

ACTION_DELETE

ACTION_DELETE = 4

Allow to delete keys

ACTION_OVERRIDE

ACTION_OVERRIDE = 8

Allow to override scope

ACTION_ALL

ACTION_ALL = 15

Allow all actions

REFERENCES

REFERENCES = 256

Support references

ALL_INCLUSIVE

ALL_INCLUSIVE = 271

All actions amd references allowed.

Properties

$instance

$instance : self

Static registry instance

Type

self

$scope

$scope : array

Scope

Type

array

$options

$options : integer

Initial options

Type

integer

$checkRefs

$checkRefs : boolean

Flag specifying to check references

Type

boolean

$refPattern

$refPattern : string

Reference regexp pattern

Type

string

Methods

getInstance()

getInstance(array  $scope = array(), integer  $options = self::ACTION_ALL) : static

Returns static registry instance.

Parameters

array $scope
integer $options

Returns

static

_set()

_set(string  $key, mixed  $value) : void

Short alias for self::getInstance()->set().

Parameters

string $key
mixed $value

Throws

\ReflectionException

Risen from self::set()

_exists()

_exists(string  $key) : boolean

Short alias for self::getInstance()->exists().

Parameters

string $key

Returns

boolean

_isEmpty()

_isEmpty(string  $key) : boolean

Short alias for self::getInstance()->isEmpty().

Parameters

string $key

Returns

boolean

_get()

_get(string  $key = null, mixed  $default = null, boolean  $throw = true) : mixed

Short alias for self::getInstance()->get().

Parameters

string $key
mixed $default
boolean $throw

Throw exception if no default value passed and key doesn't exist

Throws

\ReflectionException

Risen from self::get().

Returns

mixed

_delete()

_delete(string  $key) : void

Short alias for self::getInstance()->delete().

Parameters

string $key

Throws

\ReflectionException

Risen from self::delete().

_override()

_override(array  $scope) : void

Short alias for self::getInstance()->override().

Parameters

array $scope

Throws

\ReflectionException

Risen from self::override().

__construct()

__construct(array  $scope = array(), integer  $options = self::ALL_INCLUSIVE) 

Constructor.

Example:

use donbidon\Core\Registry\Common;

// Create registry allowing to add new keys only
$registry = new Common(
    [
        'key_1' => "value_1",
    ],
    Common::ACTION_CREATE
);

$registry->set('key_2', "value_2"); // Ok

// RuntimeException having Registry:ACTION_CREATE
// code will be thrown.
$registry->set('key_2', "value_2*");

Parameters

array $scope
integer $options

Combination of the following flags:

  • self::ACTION_CREATE,
  • self::ACTION_DELETE,
  • self::ACTION_MODIFY,
  • self::ACTION_OVERRIDE.

set()

set(string  $key, mixed  $value) : void

Sets scope value.

Parameters

string $key
mixed $value

Throws

\ReflectionException

Risen from self::checkPermissions().

exists()

exists(string  $key) : boolean

Returns true if scope exists, false otherwise.

Parameters

string $key

Returns

boolean

isEmpty()

isEmpty(string  $key) : boolean

Returns true if scope value is empty, false otherwise.

Parameters

string $key

Returns

boolean

get()

get(string  $key = null, mixed  $default = null, boolean  $throw = true) : mixed

Returns scope value.

Parameters

string $key

If not passed, whole scope will be returned

mixed $default
boolean $throw

Throw exception if no default value passed and key doesn't exist

Throws

\ReflectionException

Risen from self::checkPermissions().

\RuntimeException

If key doesn't exist.

Returns

mixed

delete()

delete(string  $key) : void

Deletes scope key.

Parameters

string $key

Throws

\ReflectionException

Risen from self::checkPermissions().

override()

override(array  $scope) : void

Overrides scope.

Parameters

array $scope

Throws

\ReflectionException

Risen from self::checkPermissions().

newFromKey()

newFromKey(string  $key) : static

Returns new registry from value of key.

Replaces all references by its values.

Parameters

string $key

Throws

\ReflectionException

Risen from self::get().

Returns

static

checkPermissions()

checkPermissions(string  $key, integer  $action) : void

Check action permissions.

Parameters

string $key
integer $action

Throws

\ReflectionException

Risen from Friendlification::getConstNameByValue().

\RuntimeException

If doesn't have permissions for passed action.

getByRef()

getByRef(string  $key, mixed  $value) : void

Gets value by reference if possible.

Parameters

string $key
mixed $value

Throws

\ReflectionException

Risen from self::get().

\RuntimeException

If cyclic or invalid reference detected.

isRef()

isRef(  $value, boolean  $modify = true) : boolean

Validates if passed value is reference.

Parameters

$value
boolean $modify

Flag specifying to modify $value (to cut "~~> " prefix)

Returns

boolean