AccountDetail.php
133 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Stripe\Models; |
| 4 | |
| 5 | use Give\Helpers\ArrayDataSet; |
| 6 | use Give\PaymentGateways\Exceptions\InvalidPropertyName; |
| 7 | |
| 8 | /** |
| 9 | * Class AccountDetail |
| 10 | * |
| 11 | * @package Give\PaymentGateways\Stripe\Models |
| 12 | * @since 2.10.2 |
| 13 | * |
| 14 | * @property-read string $type |
| 15 | * @property-read string $accountName |
| 16 | * @property-read string $accountSlug |
| 17 | * @property-read string $accountEmail |
| 18 | * @property-read string $accountCountry |
| 19 | * @property-read string $accountId |
| 20 | * @property-read string $liveSecretKey |
| 21 | * @property-read string $livePublishableKey |
| 22 | * @property-read string $testSecretKey |
| 23 | * @property-read string $testPublishableKey |
| 24 | */ |
| 25 | class AccountDetail |
| 26 | { |
| 27 | protected $args; |
| 28 | protected $propertiesArgs; |
| 29 | protected $requiredArgs = [ |
| 30 | 'type', |
| 31 | 'account_name', |
| 32 | 'account_slug', |
| 33 | 'account_email', |
| 34 | 'account_country', |
| 35 | 'account_id', |
| 36 | 'live_secret_key', |
| 37 | 'live_publishable_key', |
| 38 | 'test_secret_key', |
| 39 | 'test_publishable_key', |
| 40 | ]; |
| 41 | |
| 42 | /** |
| 43 | * AccountDetail constructor. |
| 44 | * |
| 45 | * @since 2.10.2 |
| 46 | * |
| 47 | * @param array $args |
| 48 | * |
| 49 | * @throws InvalidPropertyName |
| 50 | */ |
| 51 | public function __construct(array $args) |
| 52 | { |
| 53 | $this->args = $args; |
| 54 | $this->propertiesArgs = ArrayDataSet::camelCaseKeys($args); |
| 55 | $this->validate($args); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @since 2.13.0 |
| 60 | * @throws InvalidPropertyName |
| 61 | */ |
| 62 | public static function fromArray($array) |
| 63 | { |
| 64 | return new static($array); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @since 2.13.0 |
| 69 | * |
| 70 | * @return array |
| 71 | */ |
| 72 | public function toArray() |
| 73 | { |
| 74 | return [ |
| 75 | 'type' => $this->type, |
| 76 | 'account_id' => $this->accountId, |
| 77 | 'account_slug' => $this->accountSlug, |
| 78 | 'account_name' => $this->accountName, |
| 79 | 'account_country' => $this->accountCountry, |
| 80 | 'account_email' => $this->accountEmail, |
| 81 | 'live_secret_key' => $this->liveSecretKey, |
| 82 | 'test_secret_key' => $this->testSecretKey, |
| 83 | 'live_publishable_key' => $this->livePublishableKey, |
| 84 | 'test_publishable_key' => $this->testPublishableKey, |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @since 2.10.2 |
| 90 | * |
| 91 | * @param string $key |
| 92 | * |
| 93 | * @return mixed |
| 94 | * @throws InvalidPropertyName |
| 95 | */ |
| 96 | public function __get($key) |
| 97 | { |
| 98 | if ( ! array_key_exists($key, $this->propertiesArgs)) { |
| 99 | throw new InvalidPropertyName( |
| 100 | sprintf( |
| 101 | '$1%s property does not exist in %2$s class', |
| 102 | $key, |
| 103 | __CLASS__ |
| 104 | ) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | return $this->propertiesArgs[$key]; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Validate array format. |
| 113 | * |
| 114 | * @since 2.10.2 |
| 115 | * |
| 116 | * @param array $array |
| 117 | * |
| 118 | * @throws InvalidPropertyName |
| 119 | */ |
| 120 | private function validate($array) |
| 121 | { |
| 122 | if (array_diff($this->requiredArgs, array_keys($array))) { |
| 123 | throw new InvalidPropertyName( |
| 124 | sprintf( |
| 125 | 'To create a %1$s object, please provide valid: %2$s', |
| 126 | __CLASS__, |
| 127 | implode(' , ', $this->requiredArgs) |
| 128 | ) |
| 129 | ); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 |