AllSoapAndRest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestModule1\Service\V2;
  7. use Magento\TestModule1\Service\V2\Entity\Item;
  8. use Magento\TestModule1\Service\V2\Entity\ItemFactory;
  9. class AllSoapAndRest implements \Magento\TestModule1\Service\V2\AllSoapAndRestInterface
  10. {
  11. /**
  12. * @var ItemFactory
  13. */
  14. protected $itemFactory;
  15. /**
  16. * @param ItemFactory $itemFactory
  17. */
  18. public function __construct(ItemFactory $itemFactory)
  19. {
  20. $this->itemFactory = $itemFactory;
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function item($id)
  26. {
  27. return $this->itemFactory->create()->setId($id)->setName('testProduct1')->setPrice('1');
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function items($filters = [], $sortOrder = 'ASC')
  33. {
  34. $result = [];
  35. $firstItem = $this->itemFactory->create()->setId(1)->setName('testProduct1')->setPrice('1');
  36. $secondItem = $this->itemFactory->create()->setId(2)->setName('testProduct2')->setPrice('2');
  37. /** Simple filtration implementation */
  38. if (!empty($filters)) {
  39. /** @var \Magento\Framework\Api\Filter $filter */
  40. foreach ($filters as $filter) {
  41. if ('id' == $filter->getField() && $filter->getValue() == 1) {
  42. $result[] = $firstItem;
  43. } elseif ('id' == $filter->getField() && $filter->getValue() == 2) {
  44. $result[] = $secondItem;
  45. }
  46. }
  47. } else {
  48. /** No filter is specified. */
  49. $result = [$firstItem, $secondItem];
  50. }
  51. return $result;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function create($name)
  57. {
  58. return $this->itemFactory->create()->setId(rand())->setName($name)->setPrice('10');
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function update(Item $entityItem)
  64. {
  65. return $this->itemFactory->create()
  66. ->setId($entityItem->getId())
  67. ->setName('Updated' . $entityItem->getName())
  68. ->setPrice('5');
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function delete($id)
  74. {
  75. return $this->itemFactory->create()->setId($id)->setName('testProduct1')->setPrice('1');
  76. }
  77. }