Update.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. * @deprecated The `save` method is deprecated and will be removed in a
  37. * future major version of the library. Use the static method `update`
  38. * on the resource instead.
  39. */
  40. public function save($opts = null)
  41. {
  42. $params = $this->serializeParameters();
  43. if (\count($params) > 0) {
  44. $url = $this->instanceUrl();
  45. list($response, $opts) = $this->_request('post', $url, $params, $opts, ['save']);
  46. $this->refreshFrom($response, $opts);
  47. }
  48. return $this;
  49. }
  50. }