Advertisement
jsonbaby92

Untitled

Feb 13th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.45 KB | None | 0 0
  1. <?php
  2.  
  3. class Design
  4. {
  5.     public $id = null;
  6.     protected $attributes = [
  7.         'id' => '',
  8.         'name' => '',
  9.     ];
  10.  
  11.     /**
  12.      * Set fields and return design instance.
  13.      * @param $design_data
  14.      * @return $this
  15.      */
  16.     public function saveDesignData($design_data)
  17.     {
  18.         $this->attributes['name'] = $design_data['name'];
  19.         // Make other save stuff.
  20.         return $this;
  21.     }
  22.  
  23.     /**
  24.      * Save shipping into database (default Eloquent Model save).
  25.      * @return $this
  26.      */
  27.     public function save()
  28.     {
  29.         // Assume design saved to database and we have id.
  30.         // But now we set id just here for example.
  31.         $this->attributes['id'] = rand(0, 10000);
  32.         return $this;
  33.     }
  34.    
  35.     public function getAttributes()
  36.     {
  37.         return $this->attributes;  
  38.     }
  39.    
  40.     public function getId()
  41.     {
  42.         return $this->attributes['id'];
  43.     }
  44. }
  45.  
  46.  
  47. class Shipping
  48. {
  49.  
  50.     protected $attributes = [
  51.         'id' => '',
  52.         'type_of_delivery' => '',
  53.         'address' => '',
  54.         'design_id' => '',
  55.     ];
  56.  
  57.     public function getId()
  58.     {
  59.         return $this->attributes['id'];
  60.     }
  61.    
  62.     public function getAttributes()
  63.     {
  64.         return $this->attributes;  
  65.     }
  66.  
  67.     /**
  68.      * Set fields and return shipping instance.
  69.      * @param $shipping_data
  70.      * @return $this
  71.      */
  72.     public function saveShippingData($shipping_data)
  73.     {
  74.         $this->attributes['type_of_delivery'] = $shipping_data['type_of_delivery'];
  75.         $this->attributes['address'] = $shipping_data['address'];
  76.         return $this;
  77.     }
  78.  
  79.     /**
  80.      * Save shipping into database (default Eloquent Model save).
  81.      * @return mixed
  82.      */
  83.     public function save()
  84.     {
  85.         // Assume shipping saved to database and we have id.
  86.         // But now we set id just here for example.
  87.         $this->attributes['id'] = rand(0, 10000);
  88.         return $this;
  89.     }
  90.  
  91.     /**
  92.      * @param $id
  93.      * @return $this
  94.      */
  95.     public function associateDesign($id)
  96.     {
  97.         $this->attributes['design_id'] = $id;
  98.         return $this;
  99.     }
  100. }
  101.  
  102. class UniqueKeyIdPairsTempStoreHelper
  103. {
  104.     protected $uniqueKeyIdPairs = [];
  105.  
  106.     /**
  107.      * Add key/id pair.
  108.      * @param $uniqueKey
  109.      * @param $id
  110.      */
  111.     public function addUniqueKeyIdPair($uniqueKey, $id)
  112.     {
  113.         $this->uniqueKeyIdPairs[$uniqueKey] = $id;
  114.     }
  115.  
  116.     /**
  117.      * Get id by unique key.
  118.      * @param $uniqueKey
  119.      * @return mixed|null
  120.      */
  121.     public function getIdByUniqueKey($uniqueKey)
  122.     {
  123.         return !empty($this->uniqueKeyIdPairs[$uniqueKey]) ? $this->uniqueKeyIdPairs[$uniqueKey] : null;
  124.     }
  125. }
  126. // Init database.
  127. $database = [];
  128. /**
  129.  * Assume we create Order and its Designs in OrderController.
  130.  */
  131. $design_post = [
  132.     'design1' => [
  133.         'name' => 'Design #1',
  134.         'designUniqueKey' => 'Iasfuhh2323lk4n1',
  135.         // Other design data from frontend.
  136.     ],
  137.     'design2' => [
  138.         'name' => 'Design #2',
  139.         'designUniqueKey' => 'asf54fuhh2323lk4n2',
  140.         // Other design data from frontend.
  141.     ]
  142. ];
  143.  
  144. // Before loop design creation make a new instance of temp unique key and ids store.
  145. $tempDesignIdsStore = new UniqueKeyIdPairsTempStoreHelper();
  146.  
  147. // Loop design post and save each design into database.
  148. foreach ($design_post as $design_data) {
  149.     $design = new Design(); // Create design instance
  150.  
  151.     /*
  152.      * Bunch of code here. Set design fields, create relations(finishings, garments, etc.)
  153.      */
  154.     $design->saveDesignData($design_data);
  155.  
  156.     $design->save(); // Call save method and design will be stored in database.
  157.    
  158.    
  159.    
  160.     $database['designs'][$design->getId()] = $design->getAttributes();
  161.     /*
  162.      *  Now we can access design id and save it to temp store.
  163.      *  $design_data will store uniqueKey for new designs on estimate form. (ask frontend develop to do it).
  164.      */
  165.     $tempDesignIdsStore->addUniqueKeyIdPair($design_data['designUniqueKey'], $design->getId()); //
  166. }
  167.  
  168. /*
  169.  * Now you can use $tempDesignIdsStore below for other part of Order saving proccess.
  170.  * See example.
  171.  */
  172.  
  173. $shippings_post = [
  174.     'shipping1' => [
  175.         'address' => 'Ukraine, Kharkiv, XTZ',
  176.         'type_of_delivery' => 'shipping',
  177.         // More fields for shipping.
  178.         // ...
  179.         'designUniqueKey' => 'Iasfuhh2323lk4n1'
  180.     ],
  181.     'shipping2' => [
  182.         'address' => 'Ukraine, Kharkiv, Moskov ave.',
  183.         'type_of_delivery' => 'shipping',
  184.         // More fields for shipping.
  185.         // ...
  186.         'designUniqueKey' => 'asf54fuhh2323lk4n2'
  187.     ],
  188.     'shipping3' => [
  189.         'address' => 'United States, Florida, Some Street',
  190.         'type_of_delivery' => 'pickup',
  191.         // More fields for shipping.
  192.         // ...
  193.         'designUniqueKey' => 'Iasfuhh2323lk4n1'
  194.     ]
  195. ];
  196.  
  197. foreach ($shippings_post as $shipping_data) {
  198.     // Get design from temp store by unique key attached on estimate form on shipping step.
  199.     $design_id = $tempDesignIdsStore->getIdByUniqueKey($shipping_data['designUniqueKey']);
  200.  
  201.  
  202.     /*
  203.      * Create shipping address,
  204.      * Create shipping,
  205.      * Use design id from temp store to associate shipping and design
  206.      */
  207.     $shipping = new Shipping();
  208.     $shipping->saveShippingData($shipping_data);
  209.     $shipping->save();
  210.    
  211.     $shipping->associateDesign($design_id);
  212.  
  213.     $database['shippings'][$shipping->getId()] = $shipping->getAttributes();
  214. }
  215.  
  216. print_r($database);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement