Page.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace smladeoye\paystack;
  3. use yii\base\Component;
  4. class Page extends Component
  5. {
  6. /** @var array holds the default page operation configuration */
  7. private $page = array(
  8. 'baseUrl'=>'/page',
  9. 'slugAvailabilityUrl'=>'/check_slug_availability',
  10. 'beforeSend'=>array(),
  11. 'afterSend'=>array()
  12. );
  13. /*Constructor method to setup paystack component, page operation configurations
  14. * @param $paystack, Paystack instance
  15. *@param config, Yii2 default object configuration array
  16. */
  17. public function __construct(Paystack $paystack, $config = [])
  18. {
  19. $this->attachBehavior('Resources',array('class'=> Resources::className()));
  20. $this->setPaystack($paystack);
  21. $this->page = array_replace($this->page,$paystack->page);
  22. $this->setConfig($this->page);
  23. parent::__construct($config);
  24. }
  25. /** create a page
  26. * @param $name string|array array
  27. * @return $this
  28. */
  29. public function create($name = null)
  30. {
  31. $options = array();
  32. if (is_array($name))
  33. {
  34. $this->setRequestOptions($name);
  35. }
  36. else
  37. {
  38. if ($name)
  39. $options['name'] = $name;
  40. $this->setRequestOptions($options);
  41. }
  42. $this->sendRequest(Paystack::OP_PAGE_CREATE,Paystack::METHOD_POST);
  43. $this->setResponseOptions();
  44. return $this;
  45. }
  46. /** fetch all pages
  47. * @param $page string|integer
  48. * @param $per_page string|integer
  49. * @return $this
  50. */
  51. public function fetchAll($page = null,$per_page = null)
  52. {
  53. $options = array();
  54. if (is_array($page))
  55. {
  56. $this->setRequestOptions($page);
  57. }
  58. else
  59. {
  60. if ($page)
  61. $options['page'] = $page;
  62. if ($per_page)
  63. $options['perPage'] = $per_page;
  64. $this->setRequestOptions($options);
  65. }
  66. $this->sendRequest(Paystack::OP_PLAN_LIST);
  67. $this->setResponseOptions();
  68. return $this;
  69. }
  70. /** fetch a particular page
  71. * @param $id string|integer page id or slug
  72. * @return $this
  73. */
  74. public function fetch($id = null)
  75. {
  76. $this->acceptArray(false);
  77. $this->setRequestOptions($id);
  78. $this->sendRequest(Paystack::OP_PAGE_FETCH);
  79. $this->setResponseOptions();
  80. return $this;
  81. }
  82. /** update a particular page info
  83. * @param $id string|integer page id or slug
  84. * @param $options array, other parameters
  85. * @throws InvalidArgumentException when page_id is not provided
  86. * @return $this
  87. */
  88. public function update($page_id,$options = null)
  89. {
  90. if (is_array($page_id) || empty($page_id))
  91. throw new InvalidArgumentException('Invalid argument supplied for page id/slug, id must be integer or string');
  92. $options['id'] = $page_id;
  93. $this->setRequestOptions($options);
  94. $this->sendRequest(Paystack::OP_PLAN_UPDATE,Paystack::METHOD_PUT);
  95. $this->setResponseOptions();
  96. return $this;
  97. }
  98. /** check the availability of a particular page before deciding to create
  99. * @param $id string|integer transaction id or reference
  100. * @return $this
  101. */
  102. public function checkAvailability($id = null)
  103. {
  104. $this->acceptArray(false);
  105. $this->setRequestOptions($id);
  106. $this->sendRequest(Paystack::OP_PAGE_AVAILABILITY);
  107. $this->setResponseOptions();
  108. return $this;
  109. }
  110. }