Update.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Stripe\ApiOperations;
  3. /**
  4. * Trait for updatable resources. Adds an `update()` static method and a
  5. * `save()` method to the class.
  6. *
  7. * This trait should only be applied to classes that derive from StripeObject.
  8. */
  9. trait Update
  10. {
  11. /**
  12. * @param string $id the ID of the resource to update
  13. * @param null|array $params
  14. * @param null|array|string $opts
  15. *
  16. * @throws \Stripe\Exception\ApiErrorException if the request fails
  17. *
  18. * @return static the updated resource
  19. */
  20. public static function update($id, $params = null, $opts = null)
  21. {
  22. self::_validateParams($params);
  23. $url = static::resourceUrl($id);
  24. list($response, $opts) = static::_staticRequest('post', $url, $params, $opts);
  25. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  26. $obj->setLastResponse($response);
  27. return $obj;
  28. }
  29. /**
  30. * @param null|array|string $opts
  31. *
  32. * @throws \Stripe\Exception\ApiErrorException if the request fails
  33. *
  34. * @return static the saved resource
  35. */
  36. public function save($opts = null)
  37. {
  38. $params = $this->serializeParameters();
  39. if (\count($params) > 0) {
  40. $url = $this->instanceUrl();
  41. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  42. $this->refreshFrom($response, $opts);
  43. }
  44. return $this;
  45. }
  46. }