1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
<?php
/**
* @author @jenschude <jens.schulze@commercetools.de>
* @created: 09.02.15, 10:48
*/
namespace Commercetools\Core\Model\Product;
use Commercetools\Core\Model\Category\CategoryReferenceCollection;
use Commercetools\Core\Model\Common\JsonObject;
use Commercetools\Core\Model\Common\LocalizedString;
use Commercetools\Core\Model\Common\Reference;
use Commercetools\Core\Model\Common\ReferenceObjectInterface;
use Commercetools\Core\Model\ProductType\ProductTypeReference;
use Commercetools\Core\Model\TaxCategory\TaxCategoryReference;
use Commercetools\Core\Model\Common\DateTimeDecorator;
use Commercetools\Core\Model\Review\ReviewRatingStatistics;
use Commercetools\Core\Model\State\StateReference;
use DateTime;
/**
* @package Commercetools\Core\Model\Product
* @link https://docs.commercetools.com/http-api-projects-productProjections.html#productprojection
* @method string getId()
* @method ProductProjection setId(string $id = null)
* @method int getVersion()
* @method ProductProjection setVersion(int $version = null)
* @method DateTimeDecorator getCreatedAt()
* @method ProductProjection setCreatedAt(DateTime $createdAt = null)
* @method DateTimeDecorator getLastModifiedAt()
* @method ProductProjection setLastModifiedAt(DateTime $lastModifiedAt = null)
* @method ProductTypeReference getProductType()
* @method ProductProjection setProductType(ProductTypeReference $productType = null)
* @method LocalizedString getName()
* @method ProductProjection setName(LocalizedString $name = null)
* @method LocalizedString getDescription()
* @method ProductProjection setDescription(LocalizedString $description = null)
* @method LocalizedString getSlug()
* @method ProductProjection setSlug(LocalizedString $slug = null)
* @method CategoryReferenceCollection getCategories()
* @method ProductProjection setCategories(CategoryReferenceCollection $categories = null)
* @method LocalizedString getMetaTitle()
* @method ProductProjection setMetaTitle(LocalizedString $metaTitle = null)
* @method LocalizedString getMetaDescription()
* @method ProductProjection setMetaDescription(LocalizedString $metaDescription = null)
* @method LocalizedString getMetaKeywords()
* @method ProductProjection setMetaKeywords(LocalizedString $metaKeywords = null)
* @method bool getHasStagedChanges()
* @method ProductProjection setHasStagedChanges(bool $hasStagedChanges = null)
* @method bool getPublished()
* @method ProductProjection setPublished(bool $published = null)
* @method ProductVariant getMasterVariant()
* @method ProductProjection setMasterVariant(ProductVariant $masterVariant = null)
* @method ProductVariantCollection getVariants()
* @method ProductProjection setVariants(ProductVariantCollection $variants = null)
* @method TaxCategoryReference getTaxCategory()
* @method ProductProjection setTaxCategory(TaxCategoryReference $taxCategory = null)
* @method LocalizedSearchKeywords getSearchKeywords()
* @method ProductProjection setSearchKeywords(LocalizedSearchKeywords $searchKeywords = null)
* @method ReviewRatingStatistics getReviewRatingStatistics()
* @method ProductProjection setReviewRatingStatistics(ReviewRatingStatistics $reviewRatingStatistics = null)
* @method StateReference getState()
* @method ProductProjection setState(StateReference $state = null)
* @method array getCategoryOrderHints()
* @method ProductProjection setCategoryOrderHints(array $categoryOrderHints = null)
* @method string getKey()
* @method ProductProjection setKey(string $key = null)
*/
class ProductProjection extends JsonObject implements ReferenceObjectInterface
{
public function fieldDefinitions()
{
return [
'id' => [static::TYPE => 'string'],
'version' => [static::TYPE => 'int'],
'createdAt' => [
static::TYPE => DateTime::class,
static::DECORATOR => DateTimeDecorator::class
],
'lastModifiedAt' => [
static::TYPE => DateTime::class,
static::DECORATOR => DateTimeDecorator::class
],
'productType' => [static::TYPE => ProductTypeReference::class],
'name' => [static::TYPE => LocalizedString::class],
'description' => [static::TYPE => LocalizedString::class, static::OPTIONAL => true],
'slug' => [static::TYPE => LocalizedString::class],
'categories' => [static::TYPE => CategoryReferenceCollection::class],
'categoryOrderHints' => [static::TYPE => 'array', static::OPTIONAL => true],
'metaTitle' => [static::TYPE => LocalizedString::class, static::OPTIONAL => true],
'metaDescription' => [static::TYPE => LocalizedString::class, static::OPTIONAL => true],
'metaKeywords' => [static::TYPE => LocalizedString::class, static::OPTIONAL => true],
'hasStagedChanges' => [static::TYPE => 'bool', static::OPTIONAL => true],
'published' => [static::TYPE => 'bool', static::OPTIONAL => true],
'masterVariant' => [static::TYPE => ProductVariant::class],
'variants' => [static::TYPE => ProductVariantCollection::class],
'taxCategory' => [static::TYPE => TaxCategoryReference::class, static::OPTIONAL => true],
'searchKeywords' => [static::TYPE => LocalizedSearchKeywords::class, static::OPTIONAL => true],
'reviewRatingStatistics' => [static::TYPE => ReviewRatingStatistics::class, static::OPTIONAL => true],
'state' => [static::TYPE => StateReference::class, static::OPTIONAL => true],
'key' => [static::TYPE => 'string', static::OPTIONAL => true],
];
}
/**
* @param $variantId
* @return ProductVariant|null
*/
public function getVariantById($variantId)
{
if ($variantId == $this->getMasterVariant()->getId()) {
return $this->getMasterVariant();
}
return $this->getVariants()->getById($variantId);
}
/**
* @param $sku
* @return ProductVariant
*/
public function getVariantBySku($sku)
{
if ($sku == $this->getMasterVariant()->getSku()) {
return $this->getMasterVariant();
}
return $this->getVariants()->getBySku($sku);
}
/**
* @return ProductVariantCollection
*/
public function getAllVariants()
{
$variants = $this->getRaw('variants', []);
array_unshift($variants, $this->getRaw('masterVariant'));
return ProductVariantCollection::fromArray($variants, $this->getContextCallback());
}
public function getReferenceIdentifier()
{
return $this->getId();
}
/**
* @return ProductReference
*/
public function getReference()
{
$reference = ProductReference::ofId($this->getReferenceIdentifier(), $this->getContextCallback());
return $reference;
}
}