Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EntityStorage.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Sales\Model;
8 
9 use Magento\Framework\Model\AbstractModel as FrameworkAbstractModel;
10 
15 {
19  protected $registry = [];
20 
26  protected $storageMapper = [];
27 
31  const GLUE = '';
32 
42  public function addByIdentifyingFields(FrameworkAbstractModel $object, array $identifyingFields, $storageName)
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  }
53 
60  public function add($entity)
61  {
62  $this->registry[$entity->getId()] = $entity;
63  }
64 
71  public function get($id)
72  {
73  if ($this->has($id)) {
74  return $this->registry[$id];
75  }
76  return false;
77  }
78 
86  public function getByIdentifyingFields(array $identifyingFields, $storageName)
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  }
94 
101  public function remove($id)
102  {
103  if ($this->has($id)) {
104  unset($this->registry[$id]);
105  }
106  }
107 
114  public function has($id)
115  {
116  return isset($this->registry[$id]);
117  }
118 
125  protected function getHash(array $fields)
126  {
127  $stringForKey = implode(self::GLUE, $fields);
128  return sha1($stringForKey);
129  }
130 }
$id
Definition: fieldset.phtml:14
$fields
Definition: details.phtml:14
__()
Definition: __.php:13
getByIdentifyingFields(array $identifyingFields, $storageName)
$entity
Definition: element.phtml:22
addByIdentifyingFields(FrameworkAbstractModel $object, array $identifyingFields, $storageName)