Esempio n. 1
0
 public function __construct(array $data, $isNew = true)
 {
     if (empty($data['id'])) {
         $data['id'] = strtoupper(hash('crc32b', uniqid()));
     }
     parent::__construct($data, $isNew);
     // The default status for new orders is 'new'.
     if (empty($data['statusHistory'])) {
         $this->setStatus(self::STATUS_NEW);
     }
     // The default price for new orders is the basket price.
     if (empty($data['price'])) {
         $this->price = $this->customer->getBasket()->getPrice();
     }
     // The default weight for new orders is the basket weight.
     if (empty($data['weight'])) {
         $this->weight = $this->customer->getBasket()->getWeight();
     }
 }
Esempio n. 2
0
 /**
  * @param array $data there needs to be an 'id' parameter and either a
  * 'location' or a 'source' parameters; 'location' points to an existing
  * file or resource (such as an URL), which will only be referred to,
  * whereas 'source' points to a location that needs to be imported, such as
  * a temporary file, a URL, or a string with the image binary content.
  * @throws \RuntimeException
  */
 public function __construct(array $data, $isNew = true)
 {
     if (empty($data['id'])) {
         throw new \RuntimeException('An ID is needed to create a new image.');
     }
     $source = @$data['source'];
     $location = @$data['location'];
     if ($location) {
         $this->isLocationTemporary = FALSE;
     } elseif ($source) {
         // If a source is specified, the image is new or is meant to replace
         // an existing one.
         // a temporary location to be moved later to the persistence layer,
         // if need be.
         // Create a new temporary location.
         $this->location = self::getNewLocation();
         if (is_resource($source)) {
             $result = !!file_put_contents($this->location, $source);
             // Close the resource.
             fclose($source);
         } elseif (is_readable($source)) {
             // The source is a readable path, so the image is copied to a
             // temporary location.
             copy($source, $this->location);
         } else {
             // If the source is not a file, it is assumed that the image is
             // a string or a stream. It is then committed to a temporary
             // file.
             file_put_contents($this->location, $source);
         }
     } else {
         // If neither source nor location have been set, the image cannot
         // be created.
         throw new \RuntimeException('Either a source or a location are needed to create an image.');
     }
     // Remove the source from the data, in case it is a string holding the
     // image, which we don't want to keep in memory. At any rate, the image
     // is now referred to by $this->location.
     unset($data['source']);
     // Set the image data.
     parent::__construct($data, $isNew);
 }