Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
EntityStorage Class Reference

Public Member Functions

 addByIdentifyingFields (FrameworkAbstractModel $object, array $identifyingFields, $storageName)
 
 add ($entity)
 
 get ($id)
 
 getByIdentifyingFields (array $identifyingFields, $storageName)
 
 remove ($id)
 
 has ($id)
 

Data Fields

const GLUE = ''
 

Protected Member Functions

 getHash (array $fields)
 

Protected Attributes

 $registry = []
 
 $storageMapper = []
 

Detailed Description

Class EntityStorage store only one type of entity per instance

Definition at line 14 of file EntityStorage.php.

Member Function Documentation

◆ add()

add (   $entity)

Add entity to registry if entity in it

Parameters
\Magento\Framework\Model\AbstractModel$entity
Returns
void

Definition at line 60 of file EntityStorage.php.

61  {
62  $this->registry[$entity->getId()] = $entity;
63  }
$entity
Definition: element.phtml:22

◆ addByIdentifyingFields()

addByIdentifyingFields ( FrameworkAbstractModel  $object,
array  $identifyingFields,
  $storageName 
)

Adds entity using identifying fields mapping, entity should have an id

Parameters
FrameworkAbstractModel$object
array$identifyingFields
string$storageName
Returns
void
Exceptions

Definition at line 42 of file EntityStorage.php.

43  {
44  if (empty($identifyingFields)) {
45  throw new \Magento\Framework\Exception\InputException(__('Identifying Fields required'));
46  }
47  if (!$object->getId()) {
48  throw new \Magento\Framework\Exception\InputException(__('An ID is needed. Set the ID and try again.'));
49  }
50  $this->storageMapper[$storageName][$this->getHash($identifyingFields)] = $object->getId();
51  $this->registry[$object->getId()] = $object;
52  }
__()
Definition: __.php:13

◆ get()

get (   $id)

Retrieve entity from registry if entity in it

Parameters
int$id
Returns
bool|FrameworkAbstractModel

Definition at line 71 of file EntityStorage.php.

72  {
73  if ($this->has($id)) {
74  return $this->registry[$id];
75  }
76  return false;
77  }
$id
Definition: fieldset.phtml:14

◆ getByIdentifyingFields()

getByIdentifyingFields ( array  $identifyingFields,
  $storageName 
)

Gets entity by identifying fields

Parameters
array$identifyingFields
string$storageName
Returns
bool|FrameworkAbstractModel

Definition at line 86 of file EntityStorage.php.

87  {
88  $hash = $this->getHash($identifyingFields);
89  if (isset($this->storageMapper[$storageName][$hash])) {
90  return $this->get($this->storageMapper[$storageName][$hash]);
91  }
92  return false;
93  }

◆ getHash()

getHash ( array  $fields)
protected

Gets hash using concatenation of identifying fields

Parameters
array$fields
Returns
string

Definition at line 125 of file EntityStorage.php.

126  {
127  $stringForKey = implode(self::GLUE, $fields);
128  return sha1($stringForKey);
129  }
$fields
Definition: details.phtml:14

◆ has()

has (   $id)

Checks if entity is in storage

Parameters
int$id
Returns
bool

Definition at line 114 of file EntityStorage.php.

115  {
116  return isset($this->registry[$id]);
117  }
$id
Definition: fieldset.phtml:14

◆ remove()

remove (   $id)

Remove entity from storage

Parameters
int$id
Returns
void

Definition at line 101 of file EntityStorage.php.

102  {
103  if ($this->has($id)) {
104  unset($this->registry[$id]);
105  }
106  }
$id
Definition: fieldset.phtml:14

Field Documentation

◆ $registry

$registry = []
protected

Definition at line 19 of file EntityStorage.php.

◆ $storageMapper

$storageMapper = []
protected

Definition at line 26 of file EntityStorage.php.

◆ GLUE

const GLUE = ''

Using for array concatenation

Definition at line 31 of file EntityStorage.php.


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