QuoteCollectionPoint.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\VersionControl\AbstractDb;
  8. use Temando\Shipping\Api\Data\Delivery\QuoteCollectionPointInterface;
  9. use Temando\Shipping\Setup\SetupSchema;
  10. /**
  11. * Temando Quote Collection Point Resource Model
  12. *
  13. * @package Temando\Shipping\Model
  14. * @author Benjamin Heuer <benjamin.heuer@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 QuoteCollectionPoint 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. QuoteCollectionPointInterface::OPENING_HOURS => [
  29. [],
  30. [],
  31. ],
  32. QuoteCollectionPointInterface::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_COLLECTION_POINT, QuoteCollectionPointInterface::ENTITY_ID);
  45. }
  46. /**
  47. * Perform actions after object load
  48. *
  49. * @param AbstractModel|QuoteCollectionPointInterface $object
  50. * @return AbstractDb
  51. */
  52. protected function _afterLoad(AbstractModel $object)
  53. {
  54. $street = $object->getStreet();
  55. if (is_string($street)) {
  56. $exploded = explode("\n", $street);
  57. $object->setData(QuoteCollectionPointInterface::STREET, $exploded);
  58. }
  59. // as of v1.3.0 the serialized data structure changed
  60. $openingHours = $object->getOpeningHours();
  61. if (!array_key_exists('general', $openingHours)) {
  62. $openingHours['general'] = $openingHours;
  63. $openingHours['specific'] = [];
  64. $object->setData(QuoteCollectionPointInterface::OPENING_HOURS, $openingHours);
  65. }
  66. // cast values for type safety
  67. $distance = $object->getDistance() ? (int) $object->getDistance() : null;
  68. $object->setData(QuoteCollectionPointInterface::DISTANCE, $distance);
  69. $object->setData(QuoteCollectionPointInterface::SELECTED, (bool) $object->isSelected());
  70. return parent::_afterLoad($object);
  71. }
  72. /**
  73. * Perform actions before object save
  74. *
  75. * @param AbstractModel|QuoteCollectionPointInterface $object
  76. * @return AbstractDb
  77. */
  78. protected function _beforeSave(AbstractModel $object)
  79. {
  80. $street = $object->getStreet();
  81. if (is_array($street)) {
  82. $imploded = implode("\n", $street);
  83. $object->setData(QuoteCollectionPointInterface::STREET, $imploded);
  84. }
  85. return parent::_beforeSave($object);
  86. }
  87. /**
  88. * Perform actions after object save
  89. *
  90. * @param AbstractModel|QuoteCollectionPointInterface $object
  91. * @return AbstractDb
  92. */
  93. protected function _afterSave(AbstractModel $object)
  94. {
  95. $street = $object->getStreet();
  96. if (is_string($street)) {
  97. $exploded = explode("\n", $street);
  98. $object->setData(QuoteCollectionPointInterface::STREET, $exploded);
  99. }
  100. return parent::_afterSave($object);
  101. }
  102. }