classes/XLite/Model/AttributeValue/AttributeValueText.php line 101

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XLite\Model\AttributeValue;
  7. use ApiPlatform\Core\Annotation as ApiPlatform;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use XLite\API\Endpoint\AttributeValue\Text\DTO\AttributeValueTextInput as Input;
  10. use XLite\API\Endpoint\AttributeValue\Text\DTO\AttributeValueTextOutput as Output;
  11. use XLite\Controller\API\AttributeValue\Text\Post as PostController;
  12. use XLite\Core\Translation\Label;
  13. /**
  14.  * Attribute value (text)
  15.  *
  16.  * @ORM\Entity
  17.  * @ORM\Table  (name="attribute_values_text",
  18.  *      indexes={
  19.  *          @ORM\Index (name="product_id", columns={"product_id"}),
  20.  *          @ORM\Index (name="attribute_id", columns={"attribute_id"})
  21.  *      }
  22.  * )
  23.  * @ApiPlatform\ApiResource(
  24.  *     attributes={"pagination_enabled": false},
  25.  *     compositeIdentifier=false,
  26.  *     shortName="Textarea Attribute Value",
  27.  *     itemOperations={
  28.  *          "get"={
  29.  *              "method"="GET",
  30.  *              "path"="/products/{product_id}/attributes_text/{attribute_id}/values.{_format}",
  31.  *              "identifiers"={"id"},
  32.  *              "input"=Input::class,
  33.  *              "output"=Output::class,
  34.  *              "requirements"={"product_id"="\d+", "attribute_id"="\d+"},
  35.  *              "identifiers"={"product_id", "attribute_id"},
  36.  *              "openapi_context"={
  37.  *                  "summary"="Retrieve a value from a product textarea attribute",
  38.  *                  "parameters"={
  39.  *                     {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  40.  *                     {"name"="attribute_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  41.  *                  }
  42.  *              }
  43.  *          },
  44.  *          "put"={
  45.  *              "method"="PUT",
  46.  *              "path"="/products/{product_id}/attributes_text/{attribute_id}/values.{_format}",
  47.  *              "identifiers"={"id"},
  48.  *              "input"=Input::class,
  49.  *              "output"=Output::class,
  50.  *              "requirements"={"product_id"="\d+", "attribute_id"="\d+"},
  51.  *              "identifiers"={"product_id", "attribute_id"},
  52.  *              "openapi_context"={
  53.  *                  "summary"="Update a value of a product textarea attribute",
  54.  *                  "parameters"={
  55.  *                     {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  56.  *                     {"name"="attribute_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  57.  *                  }
  58.  *              }
  59.  *          },
  60.  *          "delete"={
  61.  *              "method"="DELETE",
  62.  *              "path"="/products/{product_id}/attributes_text/{attribute_id}/values.{_format}",
  63.  *              "identifiers"={"id"},
  64.  *              "input"=Input::class,
  65.  *              "output"=Output::class,
  66.  *              "requirements"={"product_id"="\d+", "attribute_id"="\d+"},
  67.  *              "identifiers"={"product_id", "attribute_id"},
  68.  *              "openapi_context"={
  69.  *                  "summary"="Delete a value from a product textarea attribute",
  70.  *                  "parameters"={
  71.  *                     {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  72.  *                     {"name"="attribute_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  73.  *                  }
  74.  *              }
  75.  *          }
  76.  *     },
  77.  *     collectionOperations={
  78.  *          "post"={
  79.  *              "method"="POST",
  80.  *              "path"="/products/{product_id}/attributes_text/{attribute_id}/values.{_format}",
  81.  *              "identifiers"={"id"},
  82.  *              "input"=Input::class,
  83.  *              "output"=Output::class,
  84.  *              "controller"=PostController::class,
  85.  *              "requirements"={"product_id"="\d+", "attribute_id"="\d+"},
  86.  *              "openapi_context"={
  87.  *                  "summary"="Add a value to a product textarea attribute",
  88.  *                  "parameters"={
  89.  *                     {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  90.  *                     {"name"="attribute_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  91.  *                  }
  92.  *              }
  93.  *          }
  94.  *     }
  95.  * )
  96.  */
  97. class AttributeValueText extends \XLite\Model\AttributeValue\AAttributeValue
  98. {
  99.     /**
  100.      * @var \XLite\Model\Product
  101.      *
  102.      * @ORM\ManyToOne (targetEntity="XLite\Model\Product", inversedBy="attributeValueT")
  103.      * @ORM\JoinColumn (name="product_id", referencedColumnName="product_id", onDelete="CASCADE")
  104.      */
  105.     protected $product;
  106.     /**
  107.      * Editable flag
  108.      *
  109.      * @var boolean
  110.      *
  111.      * @ORM\Column (type="boolean")
  112.      */
  113.     protected $editable false;
  114.     /**
  115.      * @var \Doctrine\Common\Collections\Collection
  116.      *
  117.      * @ORM\OneToMany (targetEntity="XLite\Model\AttributeValue\AttributeValueTextTranslation", mappedBy="owner", cascade={"all"})
  118.      */
  119.     protected $translations;
  120.     /**
  121.      * Return diff
  122.      * todo: add test
  123.      *
  124.      * @param array $oldValues Old values
  125.      * @param array $newValues New values
  126.      *
  127.      * @return array
  128.      */
  129.     public static function getDiff(array $oldValues, array $newValues)
  130.     {
  131.         $diff = [];
  132.         if ($newValues) {
  133.             foreach ($newValues as $attributeId => $value) {
  134.                 if (
  135.                     !isset($oldValues[$attributeId])
  136.                     || $value != $oldValues[$attributeId]
  137.                 ) {
  138.                     $diff[$attributeId] = $value;
  139.                 }
  140.             }
  141.         }
  142.         return $diff;
  143.     }
  144.     /**
  145.      * Return attribute value as string
  146.      *
  147.      * @return string|Label
  148.      */
  149.     public function asString()
  150.     {
  151.         /** @see \XLite\Model\AttributeValue\AttributeValueTextTranslation */
  152.         return $this->getValue();
  153.     }
  154.     /**
  155.      * @return \XLite\Model\Product
  156.      */
  157.     public function getProduct()
  158.     {
  159.         return $this->product;
  160.     }
  161.     /**
  162.      * @param \XLite\Model\Product $product
  163.      */
  164.     public function setProduct(\XLite\Model\Product $product null)
  165.     {
  166.         $this->product $product;
  167.     }
  168.     /**
  169.      * @return boolean
  170.      */
  171.     public function getEditable()
  172.     {
  173.         return $this->editable;
  174.     }
  175.     /**
  176.      * @param boolean $editable
  177.      */
  178.     public function setEditable($editable)
  179.     {
  180.         $this->editable $editable;
  181.     }
  182.     // {{{ Translation Getters / setters
  183.     /**
  184.      * @return string
  185.      */
  186.     public function getValue()
  187.     {
  188.         return $this->getTranslationField(__FUNCTION__);
  189.     }
  190.     /**
  191.      * @param string $value
  192.      *
  193.      * @return \XLite\Model\Base\Translation
  194.      */
  195.     public function setValue($value)
  196.     {
  197.         return $this->setTranslationField(__FUNCTION__$value);
  198.     }
  199.     // }}}
  200. }