Example #1
0
 /**
  * Initializes the session bag. This method must not be called directly, the class calls it when its internal data is accesed
  *
  * @throws Exception
  */
 public function initialize()
 {
     /* Ensure session object is present */
     if (is_object($this->_session) === false) {
         if (is_object($this->_dependencyInjector) === false) {
             $dependencyInjector = DI::getDefault();
             if (is_object($dependencyInjector) === false) {
                 throw new Exception('A dependency injection object is required to access the \'session\' service');
             }
         } else {
             $dependencyInjector = $this->_dependencyInjector;
         }
         $session = $dependencyInjector->getShared('session');
         if (is_object($session) === false || $session instanceof AdapterInterface === false) {
             throw new Exception('Invalid session service.');
         }
         $this->_session = $session;
     }
     $session = $this->_session;
     if (!is_object($session)) {
         $dependencyInjector = $this->_dependencyInjector;
         if (!is_object($dependencyInjector)) {
             $dependencyInjector = Di::getDefault();
         }
         $session = $dependencyInjector->getShared('session');
         $this->_session = $session;
     }
     $data = $session->get($this->_name);
     if (!is_array($data)) {
         $data = [];
     }
     $this->_data = $data;
     $this->_initialized = true;
 }
Example #2
0
 /**
  * Unserializes the object from a serialized string
  *
  * @param string $data
  * @throws Exception
  */
 public function unserialize($data)
 {
     if (!is_string($data)) {
         throw new Exception('Invalid serialization data');
     }
     $attributes = unserialize($data);
     if (is_array($attributes)) {
         /**
          * Obtain the default DI
          */
         $dependencyInjector = DI::getDefault();
         if (is_object($dependencyInjector) === false) {
             throw new Exception('A dependency injector container is required to obtain the services related to the ODM');
         }
         /**
          * Update the dependency injector
          */
         $this->_dependencyInjector = $dependencyInjector;
         /**
          * Gets the default collectionManager service
          */
         $manager = $dependencyInjector->getShared('collectionManager');
         if (!is_object($manager)) {
             throw new Exception("The injected service 'collectionManager' is not valid");
         }
         /**
          * Update the models manager
          */
         $this->_collectionManager = $manager;
         /**
          * Update the objects attributes
          */
         foreach ($attributes as $key => $value) {
             $this->{$key} = $value;
         }
     }
 }