Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Data Class Reference
Inheritance diagram for Data:
DataInterface Data Config Config Data Data Data Config EsConfig Data Config Data Scoped Data Data Data Data Data Data Config Config Config Data Data Data

Public Member Functions

 __construct (ReaderInterface $reader, CacheInterface $cache, $cacheId, SerializerInterface $serializer=null)
 
 merge (array $config)
 
 get ($path=null, $default=null)
 
 reset ()
 

Protected Member Functions

 initData ()
 

Protected Attributes

 $_reader
 
 $_cache
 
 $_cacheId
 
 $cacheTags = []
 
 $_data = []
 

Detailed Description

Represents loaded and cached configuration data, should be used to gain access to different types

@SuppressWarnings(PHPMD.NumberOfChildren) @api

Since
100.0.2

Definition at line 18 of file Data.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ReaderInterface  $reader,
CacheInterface  $cache,
  $cacheId,
SerializerInterface  $serializer = null 
)

Constructor

Parameters
ReaderInterface$reader
CacheInterface$cache
string$cacheId
SerializerInterface | null$serializer

Definition at line 83 of file Data.php.

88  {
89  $this->reader = $reader;
90  $this->cache = $cache;
91  $this->cacheId = $cacheId;
92  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
93  $this->initData();
94  }

Member Function Documentation

◆ get()

get (   $path = null,
  $default = null 
)

Get config value by key

Parameters
string$path
mixed$default
Returns
array|mixed|null

Implements DataInterface.

Definition at line 132 of file Data.php.

133  {
134  if ($path === null) {
135  return $this->_data;
136  }
137  $keys = explode('/', $path);
139  foreach ($keys as $key) {
140  if (is_array($data) && array_key_exists($key, $data)) {
141  $data = $data[$key];
142  } else {
143  return $default;
144  }
145  }
146  return $data;
147  }

◆ initData()

initData ( )
protected

Initialise data for configuration

Returns
void

Definition at line 101 of file Data.php.

102  {
103  $data = $this->cache->load($this->cacheId);
104  if (false === $data) {
105  $data = $this->reader->read();
106  $this->cache->save($this->serializer->serialize($data), $this->cacheId, $this->cacheTags);
107  } else {
108  $data = $this->serializer->unserialize($data);
109  }
110 
111  $this->merge($data);
112  }
merge(array $config)
Definition: Data.php:120

◆ merge()

merge ( array  $config)

Merge config data to the object

Parameters
array$config
Returns
void

Implements DataInterface.

Definition at line 120 of file Data.php.

121  {
122  $this->_data = array_replace_recursive($this->_data, $config);
123  }
$config
Definition: fraud_order.php:17

◆ reset()

reset ( )

Clear cache data

Returns
void

Definition at line 154 of file Data.php.

155  {
156  $this->cache->remove($this->cacheId);
157  }

Field Documentation

◆ $_cache

$_cache
protected

Definition at line 32 of file Data.php.

◆ $_cacheId

$_cacheId
protected

Definition at line 39 of file Data.php.

◆ $_data

$_data = []
protected

Definition at line 53 of file Data.php.

◆ $_reader

$_reader
protected

Definition at line 25 of file Data.php.

◆ $cacheTags

$cacheTags = []
protected

Definition at line 46 of file Data.php.


The documentation for this class was generated from the following file: