app/Plugin/PayJp42/vendor/payjp-php/lib/PayjpObject.php line 8

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