Here’s my situation:
When calling the endpoint, I receive the following data:
{
"ean": "0744963634260",
"price": "400.000000",
"update_at": "2023-10-30 18:39:02"
}
The data is output as a JSON object.
My problems:
- I need to ensure the data is output under the “products” element. For example:
{
"products": [
{
"ean": "0744963634260",
"price": "400.000000",
"update_at": "2023-10-30 18:39:02"
}
]
}
- I’m unable to output multiple elements.
My code:
appcodeDevRestApiModelDataProduct.php:
namespace DevRestApiModelData;
use DevRestApiApiDataProductInterface;
class Product implements ProductInterface
{
private $products = [];
private $ean;
private $price;
private $updateAt;
public function getProducts(): array
{
return $this->products;
}
public function setProducts(array $products)
{
$this->products = $products;
}
public function getEan(): ?string
{
return $this->ean;
}
public function setEan(?string $ean)
{
$this->ean = $ean;
}
public function getPrice(): ?string
{
return $this->price;
}
public function setPrice(?string $price)
{
$this->price = $price;
}
public function getUpdateAt(): ?string
{
return $this->updateAt;
}
public function setUpdateAt(?string $updateAt)
{
$this->updateAt = $updateAt;
}
}
appcodeDevRestApiApiDataProductInterface.php:
namespace DevRestApiApiData;
interface ProductInterface
{
/**
* Get the array of products.
*
* @return array
*/
public function getProducts(): array;
/**
* Get ean of the product.
*
* @return string|null
*/
public function getEan(): ?string;
/**
* Get price of the product.
*
* @return string|null
*/
public function getPrice(): ?string;
/**
* Get update time of the product.
*
* @return string|null
*/
public function getUpdateAt(): ?string;
}
appcodeDevRestApiModelApiProductRepository.php:
namespace DevRestApiModelApi;
use DevRestApiApiProductRepositoryInterface;
use DevRestApiApiDataProductInterface;
use MagentoCatalogModelResourceModelProductCollectionFactory;
use MagentoStoreModelStoreManagerInterface;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoFrameworkAppRequestInterface;
use MagentoFrameworkControllerResultJsonFactory;
/**
* Class ProductRepository
*/
class ProductRepository implements ProductRepositoryInterface
{
// A lot of variables
/**
* @var MagentoFrameworkControllerResultJsonFactory
*/
public function __construct(
CollectionFactory $productCollectionFactory,
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig,
MagentoDirectoryModelCountry $country,
MagentoDirectoryModelCountryFactory $countryFactory
) {
$this->productCollectionFactory = $productCollectionFactory;
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
$this->_country = $country;
$this->_countryFactory = $countryFactory;
}
public function execute(): ProductInterface
{
$actualToken = $this->scopeConfig->getValue('priceinfo_module/general/token_text',
MagentoStoreModelScopeInterface::SCOPE_STORE);
$authorizationHeader = $_SERVER['HTTP_AUTHORIZATION'];
if (preg_match('/Bearers+(.*)/', $authorizationHeader, $matches)) {
$token = $matches[1];
}
$requestBody = file_get_contents('php://input');
$requestData = json_decode($requestBody, true);
$details = isset($requestData['details']) ? $requestData['details'] : null;
$method = isset($requestData['method']) ? $requestData['method'] : null;
$offset = isset($requestData['offset']) ? $requestData['offset'] : null;
$count = isset($requestData['count']) ? $requestData['count'] : null;
$productsData = [];
if ($method == 'getProducts' && $actualToken == $token) {
$productCollection = $this->productCollectionFactory->create();
$productCollection->addAttributeToSelect([
array('*')
]);
$productCollection->setPageSize($count);
$productCollection->setCurPage($offset);
if ($details == 0) {
foreach ($productCollection as $product) {
$productData = new DevRestApiModelDataProduct();
$productData->setEan($product->getEan());
$productData->setPrice($product->getPrice());
$productData->setUpdateAt($product->getUpdatedAt());
// $productsData[] = $productData;
}
}
return $productData;
} else {
return new DevRestApiModelDataProduct();
}
}
}
I tried to solve 2 problem by adding the following:
foreach ($productCollection as $product) {
$productData = new DevRestApiModelDataProduct();
$productData->setEan($product->getEan());
$productData->setPrice($product->getPrice());
$productData->setUpdateAt($product->getUpdatedAt());
$productsData[] = $productData;
}
But I can’t get the array to output exactly that way, since the output is expected to be an object.
Are you able to suggest how my problems can be solved?
Looking forward to hearing from you.