QuotePickupLocation.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\ResourceModel\Delivery;
  6. use Magento\Framework\Model\AbstractModel;
  7. use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
  8. use Temando\Shipping\Api\Data\Delivery\QuotePickupLocationInterface;
  9. use Temando\Shipping\Setup\SetupSchema;
  10. /**
  11. * Temando Quote Pickup Location Resource Model
  12. *
  13. * @package Temando\Shipping\Model
  14. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  15. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link https://www.temando.com/
  17. */
  18. class QuotePickupLocation extends AbstractDb
  19. {
  20. /**
  21. * Serializable fields declaration
  22. * - serialized: JSON object
  23. * - unserialized: associative array
  24. *
  25. * @var mixed[]
  26. */
  27. protected $_serializableFields = [
  28. QuotePickupLocationInterface::OPENING_HOURS => [
  29. [],
  30. [],
  31. ],
  32. QuotePickupLocationInterface::SHIPPING_EXPERIENCES => [
  33. [],
  34. [],
  35. ]
  36. ];
  37. /**
  38. * Resource initialization
  39. *
  40. * @return void
  41. */
  42. protected function _construct()
  43. {
  44. $this->_init(SetupSchema::TABLE_QUOTE_PICKUP_LOCATION, QuotePickupLocationInterface::ENTITY_ID);
  45. }
  46. /**
  47. * @param AbstractModel|QuotePickupLocationInterface $object
  48. * @param int $value
  49. * @param string|null $field
  50. * @return $this
  51. * @throws \Exception
  52. */
  53. public function load(AbstractModel $object, $value, $field = null)
  54. {
  55. parent::load($object, $value);
  56. $street = $object->getStreet();
  57. if (is_string($street)) {
  58. $exploded = explode("\n", $street);
  59. $object->setData(QuotePickupLocationInterface::STREET, $exploded);
  60. }
  61. // as of v1.3.0 the serialized data structure changed
  62. $openingHours = $object->getOpeningHours();
  63. if (!array_key_exists('general', $openingHours)) {
  64. $openingHours['general'] = $openingHours;
  65. $openingHours['specific'] = [];
  66. $object->setData(QuotePickupLocationInterface::OPENING_HOURS, $openingHours);
  67. }
  68. // cast values for type safety
  69. $distance = $object->getDistance() ? (int) $object->getDistance() : null;
  70. $object->setData(QuotePickupLocationInterface::DISTANCE, $distance);
  71. $object->setData(QuotePickupLocationInterface::SELECTED, (bool) $object->isSelected());
  72. return $this;
  73. }
  74. /**
  75. * @param AbstractModel|QuotePickupLocationInterface $object
  76. * @return $this
  77. * @throws \Exception
  78. */
  79. public function save(AbstractModel $object)
  80. {
  81. $street = $object->getStreet();
  82. if (is_array($street)) {
  83. $imploded = implode("\n", $street);
  84. $object->setData(QuotePickupLocationInterface::STREET, $imploded);
  85. }
  86. parent::save($object);
  87. if (is_string($street)) {
  88. $exploded = explode("\n", $street);
  89. $object->setData(QuotePickupLocationInterface::STREET, $exploded);
  90. }
  91. return $this;
  92. }
  93. }