This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Types): add request to get type by key
Closes #169
- Loading branch information
Jens Schulze
committed
Feb 15, 2016
1 parent
e66bd4b
commit 2b34ae9
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
/** | ||
* @author @jayS-de <jens.schulze@commercetools.de> | ||
*/ | ||
|
||
namespace Commercetools\Core\Request\Types; | ||
|
||
use Commercetools\Core\Client\HttpMethod; | ||
use Commercetools\Core\Client\HttpRequest; | ||
use Commercetools\Core\Model\Common\Context; | ||
use Commercetools\Core\Request\AbstractApiRequest; | ||
use Commercetools\Core\Model\Type\Type; | ||
use Commercetools\Core\Request\ExpandTrait; | ||
use Commercetools\Core\Request\PageTrait; | ||
use Commercetools\Core\Request\QueryTrait; | ||
use Commercetools\Core\Request\StagedTrait; | ||
use Commercetools\Core\Response\ApiResponseInterface; | ||
use Commercetools\Core\Response\ResourceResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* @package Commercetools\Core\Request\Types | ||
* @method Type mapResponse(ApiResponseInterface $response) | ||
*/ | ||
class TypeByKeyGetRequest extends AbstractApiRequest | ||
{ | ||
protected $resultClass = '\Commercetools\Core\Model\Type\Type'; | ||
|
||
use QueryTrait; | ||
use StagedTrait; | ||
use PageTrait; | ||
use ExpandTrait; | ||
|
||
/** | ||
* @param string $key | ||
* @param Context $context | ||
*/ | ||
public function __construct($key, Context $context = null) | ||
{ | ||
parent::__construct(TypesEndpoint::endpoint(), $context); | ||
if (!is_null($key)) { | ||
$this->where(sprintf('key="%1$s"', $key)); | ||
} | ||
$this->limit(1); | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param Context $context | ||
* @return static | ||
*/ | ||
public static function ofKey($key, Context $context = null) | ||
{ | ||
return new static($key, $context); | ||
} | ||
|
||
/** | ||
* @return HttpRequest | ||
* @internal | ||
*/ | ||
public function httpRequest() | ||
{ | ||
return new HttpRequest(HttpMethod::GET, $this->getPath()); | ||
} | ||
|
||
/** | ||
* @param ResponseInterface $response | ||
* @return ResourceResponse | ||
*/ | ||
public function buildResponse(ResponseInterface $response) | ||
{ | ||
return new ResourceResponse($response, $this, $this->getContext()); | ||
} | ||
|
||
/** | ||
* @param array $result | ||
* @param Context $context | ||
* @return Type|null | ||
*/ | ||
public function mapResult(array $result, Context $context = null) | ||
{ | ||
if (!empty($result['results'])) { | ||
$data = current($result['results']); | ||
return Type::fromArray($data, $context); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* @author @jayS-de <jens.schulze@commercetools.de> | ||
*/ | ||
|
||
namespace Commercetools\Core\Request\Types; | ||
|
||
use Commercetools\Core\Model\Common\Context; | ||
use Commercetools\Core\Request\AbstractDeleteByKeyRequest; | ||
use Commercetools\Core\Request\AbstractDeleteRequest; | ||
use Commercetools\Core\Model\Type\Type; | ||
use Commercetools\Core\Response\ApiResponseInterface; | ||
|
||
/** | ||
* @package Commercetools\Core\Request\Types | ||
* @link https://dev.commercetools.com/http-api-projects-types.html#delete-type-by-key | ||
* @method Type mapResponse(ApiResponseInterface $response) | ||
*/ | ||
class TypeDeleteByKeyRequest extends AbstractDeleteByKeyRequest | ||
{ | ||
protected $resultClass = '\Commercetools\Core\Model\Type\Type'; | ||
|
||
/** | ||
* @param string $id | ||
* @param int $version | ||
* @param Context $context | ||
*/ | ||
public function __construct($id, $version, Context $context = null) | ||
{ | ||
parent::__construct(TypesEndpoint::endpoint(), $id, $version, $context); | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param int $version | ||
* @param Context $context | ||
* @return static | ||
*/ | ||
public static function ofKeyAndVersion($key, $version, Context $context = null) | ||
{ | ||
return new static($key, $version, $context); | ||
} | ||
} |