Page.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Webkul\BagistoApi\Models;
  3. use ApiPlatform\Metadata\ApiProperty;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use ApiPlatform\Metadata\Get;
  6. use ApiPlatform\Metadata\GetCollection;
  7. use ApiPlatform\Metadata\GraphQl\Query;
  8. use ApiPlatform\Metadata\GraphQl\QueryCollection;
  9. use Webkul\BagistoApi\Resolver\PageByUrlKeyResolver;
  10. use Webkul\BagistoApi\State\CursorAwareCollectionProvider;
  11. use Webkul\BagistoApi\State\PageProvider;
  12. use Webkul\CMS\Models\Page as BasePage;
  13. #[ApiResource(
  14. routePrefix: '/api/shop',
  15. shortName: 'page',
  16. operations: [
  17. new Get(
  18. provider: PageProvider::class,
  19. ),
  20. new GetCollection(
  21. provider: PageProvider::class,
  22. ),
  23. ],
  24. graphQlOperations: [
  25. new Query(resolver: \Webkul\BagistoApi\Resolver\BaseQueryItemResolver::class),
  26. new QueryCollection(provider: CursorAwareCollectionProvider::class),
  27. new QueryCollection(
  28. name: 'pageByUrlKey',
  29. args: [
  30. 'urlKey' => [
  31. 'type' => 'String!',
  32. 'description' => 'The URL key of the page',
  33. ],
  34. ],
  35. paginationEnabled: false,
  36. resolver: PageByUrlKeyResolver::class,
  37. ),
  38. ],
  39. )]
  40. class Page extends BasePage
  41. {
  42. /**
  43. * Get unique page identifier for API Platform
  44. */
  45. #[ApiProperty(identifier: true, writable: false)]
  46. public function getId(): int
  47. {
  48. return (int) $this->id;
  49. }
  50. /**
  51. * Get layout
  52. */
  53. #[ApiProperty(writable: false, readable: true)]
  54. public function getLayout(): ?string
  55. {
  56. return $this->layout;
  57. }
  58. /**
  59. * Get created at
  60. */
  61. #[ApiProperty(writable: false, readable: true)]
  62. public function getCreatedAt(): ?\DateTime
  63. {
  64. return $this->created_at;
  65. }
  66. /**
  67. * Get updated at
  68. */
  69. #[ApiProperty(writable: false, readable: true)]
  70. public function getUpdatedAt(): ?\DateTime
  71. {
  72. return $this->updated_at;
  73. }
  74. /**
  75. * Get current locale translation for API
  76. */
  77. #[ApiProperty(readable: true, writable: false, description: 'Current locale translation')]
  78. public function getCurrentTranslation(): ?\Illuminate\Database\Eloquent\Model
  79. {
  80. return $this->translations->firstWhere('locale', app()->getLocale())
  81. ?? $this->translations->first();
  82. }
  83. }