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