[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  class Stripe_Object implements ArrayAccess
   4  {
   5    /**
   6     * @var Stripe_Util_Set Attributes that should not be sent to the API because
   7     *    they're not updatable (e.g. API key, ID).
   8     */
   9    public static $permanentAttributes;
  10    /**
  11     * @var Stripe_Util_Set Attributes that are nested but still updatable from
  12     *    the parent class's URL (e.g. metadata).
  13     */
  14    public static $nestedUpdatableAttributes;
  15  
  16    public static function init()
  17    {
  18      self::$permanentAttributes = new Stripe_Util_Set(array('_apiKey', 'id'));
  19      self::$nestedUpdatableAttributes = new Stripe_Util_Set(array('metadata'));
  20    }
  21  
  22    protected $_apiKey;
  23    protected $_values;
  24    protected $_unsavedValues;
  25    protected $_transientValues;
  26    protected $_retrieveOptions;
  27  
  28    public function __construct($id=null, $apiKey=null)
  29    {
  30      $this->_apiKey = $apiKey;
  31      $this->_values = array();
  32      $this->_unsavedValues = new Stripe_Util_Set();
  33      $this->_transientValues = new Stripe_Util_Set();
  34  
  35      $this->_retrieveOptions = array();
  36      if (is_array($id)) {
  37        foreach ($id as $key => $value) {
  38          if ($key != 'id') {
  39            $this->_retrieveOptions[$key] = $value;
  40          }
  41        }
  42        $id = $id['id'];
  43      }
  44  
  45      if ($id !== null) {
  46        $this->id = $id;
  47      }
  48    }
  49  
  50    // Standard accessor magic methods
  51    public function __set($k, $v)
  52    {
  53      if ($v === "") {
  54        throw new InvalidArgumentException(
  55            'You cannot set \''.$k.'\'to an empty string. '
  56            .'We interpret empty strings as NULL in requests. '
  57            .'You may set obj->'.$k.' = NULL to delete the property'
  58        );
  59      }
  60  
  61      if (self::$nestedUpdatableAttributes->includes($k)
  62          && isset($this->$k) && is_array($v)) {
  63        $this->$k->replaceWith($v);
  64      } else {
  65        // TODO: may want to clear from $_transientValues (Won't be user-visible).
  66        $this->_values[$k] = $v;
  67      }
  68      if (!self::$permanentAttributes->includes($k))
  69        $this->_unsavedValues->add($k);
  70    }
  71    public function __isset($k)
  72    {
  73      return isset($this->_values[$k]);
  74    }
  75    public function __unset($k)
  76    {
  77      unset($this->_values[$k]);
  78      $this->_transientValues->add($k);
  79      $this->_unsavedValues->discard($k);
  80    }
  81    public function __get($k)
  82    {
  83      if (array_key_exists($k, $this->_values)) {
  84        return $this->_values[$k];
  85      } else if ($this->_transientValues->includes($k)) {
  86        $class = get_class($this);
  87        $attrs = join(', ', array_keys($this->_values));
  88        $message = "Stripe Notice: Undefined property of $class instance: $k. "
  89                 . "HINT: The $k attribute was set in the past, however. "
  90                 . "It was then wiped when refreshing the object "
  91                 . "with the result returned by Stripe's API, "
  92                 . "probably as a result of a save(). The attributes currently "
  93                 . "available on this object are: $attrs";
  94        error_log($message);
  95        return null;
  96      } else {
  97        $class = get_class($this);
  98        error_log("Stripe Notice: Undefined property of $class instance: $k");
  99        return null;
 100      }
 101    }
 102  
 103    // ArrayAccess methods
 104    public function offsetSet($k, $v)
 105    {
 106      $this->$k = $v;
 107    }
 108  
 109    public function offsetExists($k)
 110    {
 111      return array_key_exists($k, $this->_values);
 112    }
 113  
 114    public function offsetUnset($k)
 115    {
 116      unset($this->$k);
 117    }
 118    public function offsetGet($k)
 119    {
 120      return array_key_exists($k, $this->_values) ? $this->_values[$k] : null;
 121    }
 122  
 123    public function keys()
 124    {
 125      return array_keys($this->_values);
 126    }
 127  
 128    /**
 129     * This unfortunately needs to be public to be used in Util.php
 130     *
 131     * @param string $class
 132     * @param array $values
 133     * @param string|null $apiKey
 134     *
 135     * @return Stripe_Object The object constructed from the given values.
 136     */
 137    public static function scopedConstructFrom($class, $values, $apiKey=null)
 138    {
 139      $obj = new $class(isset($values['id']) ? $values['id'] : null, $apiKey);
 140      $obj->refreshFrom($values, $apiKey);
 141      return $obj;
 142    }
 143  
 144    /**
 145     * @param array $values
 146     * @param string|null $apiKey
 147     *
 148     * @return Stripe_Object The object of the same class as $this constructed
 149     *    from the given values.
 150     */
 151    public static function constructFrom($values, $apiKey=null)
 152    {
 153      return self::scopedConstructFrom(__CLASS__, $values, $apiKey);
 154    }
 155  
 156    /**
 157     * Refreshes this object using the provided values.
 158     *
 159     * @param array $values
 160     * @param string $apiKey
 161     * @param boolean $partial Defaults to false.
 162     */
 163    public function refreshFrom($values, $apiKey, $partial=false)
 164    {
 165      $this->_apiKey = $apiKey;
 166  
 167      // Wipe old state before setting new.  This is useful for e.g. updating a
 168      // customer, where there is no persistent card parameter.  Mark those values
 169      // which don't persist as transient
 170      if ($partial) {
 171        $removed = new Stripe_Util_Set();
 172      } else {
 173        $removed = array_diff(array_keys($this->_values), array_keys($values));
 174      }
 175  
 176      foreach ($removed as $k) {
 177        if (self::$permanentAttributes->includes($k))
 178          continue;
 179        unset($this->$k);
 180      }
 181  
 182      foreach ($values as $k => $v) {
 183        if (self::$permanentAttributes->includes($k) && isset($this[$k]))
 184          continue;
 185  
 186        if (self::$nestedUpdatableAttributes->includes($k) && is_array($v)) {
 187          $this->_values[$k] = Stripe_Object::scopedConstructFrom(
 188              'Stripe_AttachedObject', $v, $apiKey
 189          );
 190        } else {
 191          $this->_values[$k] = Stripe_Util::convertToStripeObject($v, $apiKey);
 192        }
 193  
 194        $this->_transientValues->discard($k);
 195        $this->_unsavedValues->discard($k);
 196      }
 197    }
 198  
 199    /**
 200     * @return array A recursive mapping of attributes to values for this object,
 201     *    including the proper value for deleted attributes.
 202     */
 203    public function serializeParameters()
 204    {
 205      $params = array();
 206      if ($this->_unsavedValues) {
 207        foreach ($this->_unsavedValues->toArray() as $k) {
 208          $v = $this->$k;
 209          if ($v === NULL) {
 210            $v = '';
 211          }
 212          $params[$k] = $v;
 213        }
 214      }
 215  
 216      // Get nested updates.
 217      foreach (self::$nestedUpdatableAttributes->toArray() as $property) {
 218        if (isset($this->$property)
 219            && $this->$property instanceOf Stripe_Object) {
 220          $params[$property] = $this->$property->serializeParameters();
 221        }
 222      }
 223      return $params;
 224    }
 225  
 226    // Pretend to have late static bindings, even in PHP 5.2
 227    protected function _lsb($method)
 228    {
 229      $class = get_class($this);
 230      $args = array_slice(func_get_args(), 1);
 231      return call_user_func_array(array($class, $method), $args);
 232    }
 233    protected static function _scopedLsb($class, $method)
 234    {
 235      $args = array_slice(func_get_args(), 2);
 236      return call_user_func_array(array($class, $method), $args);
 237    }
 238  
 239    public function __toJSON()
 240    {
 241      if (defined('JSON_PRETTY_PRINT')) {
 242        return json_encode($this->__toArray(true), JSON_PRETTY_PRINT);
 243      } else {
 244        return json_encode($this->__toArray(true));
 245      }
 246    }
 247  
 248    public function __toString()
 249    {
 250      return $this->__toJSON();
 251    }
 252  
 253    public function __toArray($recursive=false)
 254    {
 255      if ($recursive) {
 256        return Stripe_Util::convertStripeObjectToArray($this->_values);
 257      } else {
 258        return $this->_values;
 259      }
 260    }
 261  }
 262  
 263  
 264  Stripe_Object::init();


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