Update.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 array|null $params
  14. * @param array|string|null $opts
  15. *
  16. * @return \Stripe\ApiResource The updated resource.
  17. */
  18. public static function update($id, $params = null, $opts = null)
  19. {
  20. self::_validateParams($params);
  21. $url = static::resourceUrl($id);
  22. list($response, $opts) = static::_staticRequest('post', $url, $params, $opts);
  23. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  24. $obj->setLastResponse($response);
  25. return $obj;
  26. }
  27. /**
  28. * @param array|string|null $opts
  29. *
  30. * @return \Stripe\ApiResource The saved resource.
  31. */
  32. public function save($opts = null)
  33. {
  34. $params = $this->serializeParameters();
  35. if (count($params) > 0) {
  36. $url = $this->instanceUrl();
  37. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  38. $this->refreshFrom($response, $opts);
  39. }
  40. return $this;
  41. }
  42. }