AttributeValueRepository.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Shop\AttributeValues\Repositories;
  3. use App\Shop\Attributes\Attribute;
  4. use App\Shop\AttributeValues\AttributeValue;
  5. use Jsdecena\Baserepo\BaseRepository;
  6. use Illuminate\Support\Collection;
  7. class AttributeValueRepository extends BaseRepository implements AttributeValueRepositoryInterface
  8. {
  9. /**
  10. * AttributeValueRepository constructor.
  11. * @param AttributeValue $attributeValue
  12. */
  13. public function __construct(AttributeValue $attributeValue)
  14. {
  15. parent::__construct($attributeValue);
  16. $this->model = $attributeValue;
  17. }
  18. /**
  19. * @param Attribute $attribute
  20. * @param array $data
  21. * @return AttributeValue
  22. */
  23. public function createAttributeValue(Attribute $attribute, array $data) : AttributeValue
  24. {
  25. $attributeValue = new AttributeValue($data);
  26. $attributeValue->attribute()->associate($attribute);
  27. $attributeValue->save();
  28. return $attributeValue;
  29. }
  30. /**
  31. * Create the attribute value and associate to the attribute
  32. *
  33. * @param Attribute $attribute
  34. * @return AttributeValue
  35. */
  36. public function associateToAttribute(Attribute $attribute) : AttributeValue
  37. {
  38. $this->model->attribute()->associate($attribute);
  39. $this->model->save();
  40. return $this->model;
  41. }
  42. /**
  43. * Remove association from the attribute
  44. */
  45. public function dissociateFromAttribute() : bool
  46. {
  47. return $this->model->delete();
  48. }
  49. /**
  50. * @return Collection
  51. */
  52. public function findProductAttributes() : Collection
  53. {
  54. return $this->model->productAttributes()->get();
  55. }
  56. }