Composable Commerce PHP SDKPHP SDK
  • Namespace
  • Class
  • Tree

Namespaces

  • Commercetools
    • Commons
      • Helper
    • Core
      • Builder
        • Request
        • Update
      • Cache
      • Client
        • Adapter
        • OAuth
      • Error
      • Helper
        • Annotate
        • State
          • Renderer
        • Subscriber
          • Log
      • Model
        • ApiClient
        • Cart
        • CartDiscount
        • Category
        • Channel
        • Common
        • Customer
        • CustomerGroup
        • CustomField
        • CustomObject
        • DiscountCode
        • Extension
        • Inventory
        • Message
        • Order
        • OrderEdit
        • Payment
        • Product
          • Search
        • ProductDiscount
        • ProductSelection
        • ProductType
        • Project
        • Review
        • ShippingMethod
        • ShoppingList
        • State
        • Store
        • Subscription
        • TaxCategory
        • Type
        • Zone
      • Request
        • ApiClients
        • CartDiscounts
          • Command
        • Carts
          • Command
        • Categories
          • Command
        • Channels
          • Command
        • CustomerGroups
          • Command
        • Customers
          • Command
        • CustomField
          • Command
        • CustomObjects
        • DiscountCodes
          • Command
        • Extensions
          • Command
        • GraphQL
        • InStores
        • Inventory
          • Command
        • Me
          • Command
        • Messages
        • OrderEdits
          • Command
          • StagedOrder
            • Command
        • Orders
          • Command
        • Payments
          • Command
        • ProductDiscounts
          • Command
        • Products
          • Command
        • ProductSelections
          • Command
        • ProductTypes
          • Command
        • Project
          • Command
        • Query
        • Reviews
          • Command
        • ShippingMethods
          • Command
        • ShoppingLists
          • Command
        • States
          • Command
        • Stores
          • Command
        • Subscriptions
          • Command
        • TaxCategories
          • Command
        • Types
          • Command
        • Zones
          • Command
      • Response

Classes

  • AbstractJsonDeserializeObject
  • Address
  • AddressCollection
  • Asset
  • AssetCollection
  • AssetDimension
  • AssetDraft
  • AssetDraftCollection
  • AssetSource
  • AssetSourceCollection
  • Attribute
  • AttributeCollection
  • CentPrecisionMoney
  • ClientLogging
  • Collection
  • ContainerAndKey
  • Context
  • CreatedBy
  • DateDecorator
  • DateTimeDecorator
  • DiscountedPrice
  • DiscountValue
  • Enum
  • EnumCollection
  • GeoLocation
  • GeoPoint
  • HighPrecisionMoney
  • Image
  • ImageCollection
  • ImageDimension
  • JsonObject
  • KeyReference
  • KeyResourceIdentifier
  • LastModifiedBy
  • LocalizedEnum
  • LocalizedEnumCollection
  • LocalizedString
  • Money
  • MoneyCollection
  • Price
  • PriceCollection
  • PriceDraft
  • PriceDraftCollection
  • PriceTier
  • PriceTierCollection
  • Reference
  • ReferenceCollection
  • Resource
  • ResourceIdentifier
  • ScopedPrice
  • Set
  • TaxedItemPrice
  • TaxedPrice
  • TaxPortion
  • TaxPortionCollection
  • TimeDecorator

Interfaces

  • ContextAwareInterface
  • JsonDeserializeInterface
  • ObjectTreeInterface
  • ReferenceObjectInterface
  • TypeableInterface

Traits

  • ContextTrait
  • LocaleTrait
  • ObjectTreeTrait
  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 
<?php
/**
 * @author @jenschude <jens.schulze@commercetools.de>
 */

namespace Commercetools\Core\Model\Common;

use Commercetools\Core\Model\ProductType\AttributeDefinitionCollection;

/**
 * @package Commercetools\Core\Model\Common
 * @link https://docs.commercetools.com/http-api-projects-products.html#attribute
 * @method Attribute current()
 * @method AttributeCollection add(Attribute $element)
 */
class AttributeCollection extends Collection
{
    const NAME = 'name';

    protected $type = Attribute::class;

    /**
     * @var AttributeDefinitionCollection
     */
    protected $attributeDefinitions;

    protected function indexRow($offset, $row)
    {
        if ($row instanceof Attribute) {
            $name = $row->getName();
        } else {
            $name = $row[static::NAME];
        }
        $this->addToIndex(static::NAME, $offset, $name);
    }

    public function __get($attributeName)
    {
        $attribute = $this->getByName($attributeName);
        if (!is_null($attribute)) {
            return $attribute->getValue();
        }

        return null;
    }

    public function __call($name, $arguments)
    {
        if (strpos($name, 'get') === 0) {
            $name = lcfirst(substr($name, 3));
        }
        return $this->getByName($name);
    }


    public function offsetGet($offset)
    {
        if (is_string($offset)) {
            return $this->getByName($offset);
        }
        return $this->getAt($offset);
    }

    /**
     * @param $attributeName
     * @return Attribute|null
     */
    public function getByName($attributeName)
    {
        return $this->getBy(static::NAME, $attributeName);
    }

    /**
     * @param AttributeDefinitionCollection $attributeDefinitions
     * @return $this
     */
    public function setAttributeDefinitions(AttributeDefinitionCollection $attributeDefinitions)
    {
        $this->attributeDefinitions = $attributeDefinitions;

        return $this;
    }

    /**
     * @param $offset
     * @return Attribute
     */
    public function getAt($offset)
    {
        /**
         * @var Attribute $attribute;
         */
        $attribute = parent::getAt($offset);
        if (!is_null($this->attributeDefinitions)) {
            $definition = $this->attributeDefinitions->getByName($attribute->getName());
            if (!is_null($definition)) {
                $attribute->setAttributeDefinition($definition);
            }
        }

        return $attribute;
    }
}
PHP SDK API documentation generated by ApiGen