[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/stripe-php/lib/Stripe/ -> ApiResource.php (source)

   1  <?php
   2  
   3  abstract class Stripe_ApiResource extends Stripe_Object
   4  {
   5    protected static function _scopedRetrieve($class, $id, $apiKey=null)
   6    {
   7      $instance = new $class($id, $apiKey);
   8      $instance->refresh();
   9      return $instance;
  10    }
  11  
  12    /**
  13     * @returns Stripe_ApiResource The refreshed resource.
  14     */
  15    public function refresh()
  16    {
  17      $requestor = new Stripe_ApiRequestor($this->_apiKey);
  18      $url = $this->instanceUrl();
  19  
  20      list($response, $apiKey) = $requestor->request(
  21          'get',
  22          $url,
  23          $this->_retrieveOptions
  24      );
  25      $this->refreshFrom($response, $apiKey);
  26      return $this;
  27    }
  28  
  29    /**
  30     * @param string $class
  31     *
  32     * @returns string The name of the class, with namespacing and underscores
  33     *    stripped.
  34     */
  35    public static function className($class)
  36    {
  37      // Useful for namespaces: Foo\Stripe_Charge
  38      if ($postfixNamespaces = strrchr($class, '\\')) {
  39        $class = substr($postfixNamespaces, 1);
  40      }
  41      // Useful for underscored 'namespaces': Foo_Stripe_Charge
  42      if ($postfixFakeNamespaces = strrchr($class, 'Stripe_')) {
  43        $class = $postfixFakeNamespaces;
  44      }
  45      if (substr($class, 0, strlen('Stripe')) == 'Stripe') {
  46        $class = substr($class, strlen('Stripe'));
  47      }
  48      $class = str_replace('_', '', $class);
  49      $name = urlencode($class);
  50      $name = strtolower($name);
  51      return $name;
  52    }
  53  
  54    /**
  55     * @param string $class
  56     *
  57     * @returns string The endpoint URL for the given class.
  58     */
  59    public static function classUrl($class)
  60    {
  61      $base = self::_scopedLsb($class, 'className', $class);
  62      return "/v1/$base}s";
  63    }
  64  
  65    /**
  66     * @returns string The full API URL for this API resource.
  67     */
  68    public function instanceUrl()
  69    {
  70      $id = $this['id'];
  71      $class = get_class($this);
  72      if ($id === null) {
  73        $message = "Could not determine which URL to request: "
  74                 . "$class instance has invalid ID: $id";
  75        throw new Stripe_InvalidRequestError($message, null);
  76      }
  77      $id = Stripe_ApiRequestor::utf8($id);
  78      $base = $this->_lsb('classUrl', $class);
  79      $extn = urlencode($id);
  80      return "$base/$extn";
  81    }
  82  
  83    private static function _validateCall($method, $params=null, $apiKey=null)
  84    {
  85      if ($params && !is_array($params)) {
  86        $message = "You must pass an array as the first argument to Stripe API "
  87                 . "method calls.  (HINT: an example call to create a charge "
  88                 . "would be: \"StripeCharge::create(array('amount' => 100, "
  89                 . "'currency' => 'usd', 'card' => array('number' => "
  90                 . "4242424242424242, 'exp_month' => 5, 'exp_year' => 2015)))\")";
  91        throw new Stripe_Error($message);
  92      }
  93  
  94      if ($apiKey && !is_string($apiKey)) {
  95        $message = 'The second argument to Stripe API method calls is an '
  96                 . 'optional per-request apiKey, which must be a string.  '
  97                 . '(HINT: you can set a global apiKey by '
  98                 . '"Stripe::setApiKey(<apiKey>)")';
  99        throw new Stripe_Error($message);
 100      }
 101    }
 102  
 103    protected static function _scopedAll($class, $params=null, $apiKey=null)
 104    {
 105      self::_validateCall('all', $params, $apiKey);
 106      $requestor = new Stripe_ApiRequestor($apiKey);
 107      $url = self::_scopedLsb($class, 'classUrl', $class);
 108      list($response, $apiKey) = $requestor->request('get', $url, $params);
 109      return Stripe_Util::convertToStripeObject($response, $apiKey);
 110    }
 111  
 112    protected static function _scopedCreate($class, $params=null, $apiKey=null)
 113    {
 114      self::_validateCall('create', $params, $apiKey);
 115      $requestor = new Stripe_ApiRequestor($apiKey);
 116      $url = self::_scopedLsb($class, 'classUrl', $class);
 117      list($response, $apiKey) = $requestor->request('post', $url, $params);
 118      return Stripe_Util::convertToStripeObject($response, $apiKey);
 119    }
 120  
 121    protected function _scopedSave($class, $apiKey=null)
 122    {
 123      self::_validateCall('save');
 124      $requestor = new Stripe_ApiRequestor($apiKey);
 125      $params = $this->serializeParameters();
 126  
 127      if (count($params) > 0) {
 128        $url = $this->instanceUrl();
 129        list($response, $apiKey) = $requestor->request('post', $url, $params);
 130        $this->refreshFrom($response, $apiKey);
 131      }
 132      return $this;
 133    }
 134  
 135    protected function _scopedDelete($class, $params=null)
 136    {
 137      self::_validateCall('delete');
 138      $requestor = new Stripe_ApiRequestor($this->_apiKey);
 139      $url = $this->instanceUrl();
 140      list($response, $apiKey) = $requestor->request('delete', $url, $params);
 141      $this->refreshFrom($response, $apiKey);
 142      return $this;
 143    }
 144  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1