Skip to content

Commit

Permalink
Add support to include related entities
Browse files Browse the repository at this point in the history
  • Loading branch information
sweikert committed Mar 31, 2022
1 parent 94ac51f commit 5aa661c
Show file tree
Hide file tree
Showing 22 changed files with 613 additions and 127 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ $oro->setAccessToken($res->access_token);
$product = $oro->products->get(100);
```

#### Get a single product with related entities included
```php
$oro->products->get(1, ['include' => 'names,descriptions']);
```

#### Get a list of products
```php
$products = $oro->products->page();
Expand Down
2 changes: 1 addition & 1 deletion examples/products/get_product_names.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
try {
require "../initialize.php";

$product = $oro->products->get(116);
$product = $oro->products->get(1);

echo '<b>' . $product->type . ' ' . $product->id . '</b><br>';

Expand Down
22 changes: 22 additions & 0 deletions examples/products/get_product_with_includes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

try {
require "../initialize.php";

$product = $oro->products->get(1, ['include' => 'names,descriptions']);

echo '<b>' . $product->type . ' ' . $product->id . '</b><br><br>';

foreach ($product->included as $include) {
echo $include->type . "<br>";
echo $include->id . "<br>";

foreach ($include->attributes as $key => $val) {
echo $key . ": " . $val . "<br>";
}

echo "<br>";
}
} catch (\Digitalprint\Oro\Api\Exceptions\ApiException $e) {
echo "API call failed: " . $e->getMessage();
}
16 changes: 6 additions & 10 deletions src/Endpoints/AddressEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,24 @@ class AddressEndpoint extends CollectionEndpointAbstract
protected string $resourcePath = "api/addresses";

/**
* Get the object that is used by this API. Every API uses one type of object.
*
* @param array $included
* @return Address
*/
protected function getResourceObject(): Address
protected function getResourceObject(array $included = []): Address
{
return new Address($this->client);
return new Address($this->client, $included);
}

/**
* Get the collection object that is used by this API. Every API uses one type of collection object.
*
* @param stdClass $links
*
* @param array $included
* @return AddressCollection
*/
protected function getResourceCollectionObject(stdClass $links): AddressCollection
protected function getResourceCollectionObject(stdClass $links, array $included = []): AddressCollection
{
return new AddressCollection($this->client, $links);
return new AddressCollection($this->client, $links, $included);
}


/**
* @param array|null $data
* @return Address
Expand Down
13 changes: 12 additions & 1 deletion src/Endpoints/AddresstypeEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Digitalprint\Oro\Api\Endpoints;

use BadMethodCallException;
use Digitalprint\Oro\Api\Exceptions\ApiException;
use Digitalprint\Oro\Api\Resources\Addresstype;
use Digitalprint\Oro\Api\Resources\BaseCollection;

class AddresstypeEndpoint extends EndpointAbstract
{
Expand All @@ -13,13 +15,22 @@ class AddresstypeEndpoint extends EndpointAbstract
protected string $resourcePath = "api/addresstypes";

/**
* @param array $included
* @return Addresstype
*/
protected function getResourceObject(): Addresstype
protected function getResourceObject(array $included = []): Addresstype
{
return new Addresstype($this->client);
}

/**
* @return BaseCollection
*/
protected function getResourceCollectionObject(): BaseCollection
{
throw new BadMethodCallException('not implemented');
}

/**
* @param string $addresstypeId
* @param array $filter
Expand Down
17 changes: 13 additions & 4 deletions src/Endpoints/AsyncoperationEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@

namespace Digitalprint\Oro\Api\Endpoints;

use BadMethodCallException;
use Digitalprint\Oro\Api\Exceptions\ApiException;
use Digitalprint\Oro\Api\Resources\Asyncoperation;
use Digitalprint\Oro\Api\Resources\BaseCollection;

class AsyncoperationEndpoint extends EndpointAbstract
{
protected string $resourcePath = "api/asyncoperations";

/**
* Get the object that is used by this API. Every API uses one type of object.
*
* @param array $included
* @return Asyncoperation
*/
protected function getResourceObject(): Asyncoperation
protected function getResourceObject(array $included = []): Asyncoperation
{
return new Asyncoperation($this->client);
return new Asyncoperation($this->client, $included);
}

/**
* @return BaseCollection
*/
protected function getResourceCollectionObject(): BaseCollection
{
throw new BadMethodCallException('not implemented');
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Endpoints/AuthorizationEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ protected function getResourceCollectionObject(): BaseCollection
}

/**
* @param array $included
* @return Authorization
*/
protected function getResourceObject(): Authorization
protected function getResourceObject(array $included = []): Authorization
{
return new Authorization($this->client);
return new Authorization($this->client, $included);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Endpoints/CollectionEndpointAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
abstract class CollectionEndpointAbstract extends EndpointAbstract
{
/**
* Get the collection object that is used by this API endpoint. Every API endpoint uses one type of collection object.
*
* @param stdClass $links
*
* @param array $included
* @return BaseCollection
*/
abstract protected function getResourceCollectionObject(stdClass $links): BaseCollection;
abstract protected function getResourceCollectionObject(stdClass $links, array $included = []): BaseCollection;
}
17 changes: 8 additions & 9 deletions src/Endpoints/EndpointAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function rest_create(array $body): BaseResource
$this->parseRequestBody($body)
);

return ResourceFactory::createFromApiResult($result->data, $this->getResourceObject());
return ResourceFactory::createFromApiResult($result->data, $this->getResourceObject($result->included ?? []));
}

/**
Expand All @@ -125,7 +125,7 @@ protected function rest_read(string $id, array $filter): BaseResource
$this->getResourcePath() . "/" . $id . $this->buildQueryString($filter)
);

return ResourceFactory::createFromApiResult($result->data, $this->getResourceObject());
return ResourceFactory::createFromApiResult($result->data, $this->getResourceObject($result->included ?? []));
}

/**
Expand All @@ -149,10 +149,10 @@ protected function rest_list(int $number = null, int $size = null, array $filter
);

/** @var BaseCollection $collection */
$collection = $this->getResourceCollectionObject($result->links);
$collection = $this->getResourceCollectionObject($result->links, $result->included ?? []);

foreach ($result->data as $dataResult) {
$collection[] = ResourceFactory::createFromApiResult($dataResult, $this->getResourceObject());
$collection[] = ResourceFactory::createFromApiResult($dataResult, $this->getResourceObject($result->included ?? []));
}

return $collection;
Expand All @@ -176,7 +176,7 @@ protected function rest_update(array $body = []): ?BaseResource
return null;
}

return ResourceFactory::createFromApiResult($result->data, $this->getResourceObject());
return ResourceFactory::createFromApiResult($result->data, $this->getResourceObject($result->included ?? []));
}

/**
Expand All @@ -197,15 +197,14 @@ protected function rest_delete(array $filter = []): ?BaseResource
return null;
}

return ResourceFactory::createFromApiResult($result->data, $this->getResourceObject());
return ResourceFactory::createFromApiResult($result->data, $this->getResourceObject($result->included ?? []));
}

/**
* Get the object that is used by this API endpoint. Every API endpoint uses one type of object.
*
* @param array $included
* @return BaseResource
*/
abstract protected function getResourceObject(): BaseResource;
abstract protected function getResourceObject(array $included = []): BaseResource;

/**
* @param string $resourcePath
Expand Down
18 changes: 6 additions & 12 deletions src/Endpoints/ProductEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use JsonException;
use stdClass;

/**
*
*/
class ProductEndpoint extends CollectionEndpointAbstract
{
/**
Expand All @@ -19,25 +16,22 @@ class ProductEndpoint extends CollectionEndpointAbstract
protected string $resourcePath = "api/products";

/**
* Get the object that is used by this API. Every API uses one type of object.
*
* @param array $included
* @return Product
*/
protected function getResourceObject(): Product
protected function getResourceObject(array $included = []): Product
{
return new Product($this->client);
return new Product($this->client, $included);
}

/**
* Get the collection object that is used by this API. Every API uses one type of collection object.
*
* @param stdClass $links
*
* @param array $included
* @return ProductCollection
*/
protected function getResourceCollectionObject(stdClass $links): ProductCollection
protected function getResourceCollectionObject(stdClass $links, array $included = []): ProductCollection
{
return new ProductCollection($this->client, $links);
return new ProductCollection($this->client, $links, $included);
}

/**
Expand Down
18 changes: 6 additions & 12 deletions src/Endpoints/ProductdescriptionEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use JsonException;
use stdClass;

/**
*
*/
class ProductdescriptionEndpoint extends CollectionEndpointAbstract
{
/**
Expand All @@ -19,25 +16,22 @@ class ProductdescriptionEndpoint extends CollectionEndpointAbstract
protected string $resourcePath = "api/productdescriptions";

/**
* Get the object that is used by this API. Every API uses one type of object.
*
* @param array $included
* @return Productdescription
*/
protected function getResourceObject(): Productdescription
protected function getResourceObject(array $included = []): Productdescription
{
return new Productdescription($this->client);
return new Productdescription($this->client, $included);
}

/**
* Get the collection object that is used by this API. Every API uses one type of collection object.
*
* @param stdClass $links
*
* @param array $included
* @return ProductdescriptionCollection
*/
protected function getResourceCollectionObject(stdClass $links): ProductdescriptionCollection
protected function getResourceCollectionObject(stdClass $links, array $included = []): ProductdescriptionCollection
{
return new ProductdescriptionCollection($this->client, $links);
return new ProductdescriptionCollection($this->client, $links, $included);
}

/**
Expand Down
18 changes: 6 additions & 12 deletions src/Endpoints/ProductimageEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use JsonException;
use stdClass;

/**
*
*/
class ProductimageEndpoint extends CollectionEndpointAbstract
{
/**
Expand All @@ -19,25 +16,22 @@ class ProductimageEndpoint extends CollectionEndpointAbstract
protected string $resourcePath = "api/productimages";

/**
* Get the object that is used by this API. Every API uses one type of object.
*
* @param array $included
* @return Productimage
*/
protected function getResourceObject(): Productimage
protected function getResourceObject(array $included = []): Productimage
{
return new Productimage($this->client);
return new Productimage($this->client, $included);
}

/**
* Get the collection object that is used by this API. Every API uses one type of collection object.
*
* @param stdClass $links
*
* @param array $included
* @return ProductimageCollection
*/
protected function getResourceCollectionObject(stdClass $links): ProductimageCollection
protected function getResourceCollectionObject(stdClass $links, array $included = []): ProductimageCollection
{
return new ProductimageCollection($this->client, $links);
return new ProductimageCollection($this->client, $links, $included);
}

/**
Expand Down
Loading

0 comments on commit 5aa661c

Please sign in to comment.