@@ -19,261 +19,261 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class GraphQLField implements GraphQLFieldInterface |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var string $name |
|
| 24 | - */ |
|
| 25 | - protected $name; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var string|string[] $type |
|
| 29 | - */ |
|
| 30 | - protected $type; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var string|null $key |
|
| 34 | - */ |
|
| 35 | - protected $key; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var string $description |
|
| 39 | - */ |
|
| 40 | - protected $description; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var callable $formatter |
|
| 44 | - */ |
|
| 45 | - protected $formatter; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var callable $resolve |
|
| 49 | - */ |
|
| 50 | - protected $resolver; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var array $caps |
|
| 54 | - */ |
|
| 55 | - protected $caps; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var array $args |
|
| 59 | - */ |
|
| 60 | - protected $args; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @var bool $use_for_input |
|
| 64 | - */ |
|
| 65 | - protected $use_for_input = true; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @var bool $use_for_output |
|
| 69 | - */ |
|
| 70 | - protected $use_for_output = true; |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @param string $name |
|
| 75 | - * @param string|string[] $type |
|
| 76 | - * @param string|null $key |
|
| 77 | - * @param string $description |
|
| 78 | - * @param callable|null $formatter |
|
| 79 | - * @param callable|null $resolver |
|
| 80 | - * @param array $caps |
|
| 81 | - * @param array $args |
|
| 82 | - */ |
|
| 83 | - public function __construct( |
|
| 84 | - string $name, |
|
| 85 | - $type, |
|
| 86 | - string $key = null, |
|
| 87 | - string $description = '', |
|
| 88 | - callable $formatter = null, |
|
| 89 | - callable $resolver = null, |
|
| 90 | - array $caps = [], |
|
| 91 | - array $args = [] |
|
| 92 | - ) { |
|
| 93 | - $this->name = $name; |
|
| 94 | - $this->type = $type; |
|
| 95 | - $this->key = $key; |
|
| 96 | - $this->description = $description; |
|
| 97 | - $this->formatter = $formatter; |
|
| 98 | - $this->resolver = $resolver; |
|
| 99 | - $this->caps = $caps; |
|
| 100 | - $this->args = $args; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @return array |
|
| 106 | - */ |
|
| 107 | - public function caps(): array |
|
| 108 | - { |
|
| 109 | - return $this->caps; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public function description(): string |
|
| 117 | - { |
|
| 118 | - return $this->description; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @return string |
|
| 124 | - */ |
|
| 125 | - public function key(): ?string |
|
| 126 | - { |
|
| 127 | - return $this->key; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function name(): string |
|
| 135 | - { |
|
| 136 | - return $this->name; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @return string|string[] |
|
| 142 | - */ |
|
| 143 | - public function type() |
|
| 144 | - { |
|
| 145 | - return $this->type; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Convert the field to array to be |
|
| 151 | - * able to pass as config to WP GraphQL |
|
| 152 | - * |
|
| 153 | - * @return array |
|
| 154 | - */ |
|
| 155 | - public function toArray(): array |
|
| 156 | - { |
|
| 157 | - return get_object_vars($this); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Sets the value for use_for_input. |
|
| 163 | - * |
|
| 164 | - * @param bool $use_for_input |
|
| 165 | - */ |
|
| 166 | - protected function setUseForInput(bool $use_for_input) |
|
| 167 | - { |
|
| 168 | - $this->use_for_input = filter_var($use_for_input, FILTER_VALIDATE_BOOLEAN); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Whether the field should be used for |
|
| 174 | - * mutation inputs. |
|
| 175 | - * |
|
| 176 | - * @return bool |
|
| 177 | - */ |
|
| 178 | - public function useForInput(): bool |
|
| 179 | - { |
|
| 180 | - return $this->use_for_input; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Whether the field should be used for |
|
| 186 | - * query outputs. |
|
| 187 | - * |
|
| 188 | - * @return bool |
|
| 189 | - */ |
|
| 190 | - public function useForOutput(): bool |
|
| 191 | - { |
|
| 192 | - return $this->use_for_output; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Sets the value for use_for_output |
|
| 198 | - * |
|
| 199 | - * @param bool $use_for_output |
|
| 200 | - */ |
|
| 201 | - protected function setUseForOutput(bool $use_for_output) |
|
| 202 | - { |
|
| 203 | - $this->use_for_output = filter_var($use_for_output, FILTER_VALIDATE_BOOLEAN); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Whether the field should resolve |
|
| 209 | - * based on the user caps etc. |
|
| 210 | - * |
|
| 211 | - * @return boolean |
|
| 212 | - */ |
|
| 213 | - public function shouldResolve(): bool |
|
| 214 | - { |
|
| 215 | - foreach ($this->caps as $cap) { |
|
| 216 | - if (! current_user_can($cap)) { |
|
| 217 | - return false; |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - return true; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Whether the field has an explicit resolver set. |
|
| 226 | - * |
|
| 227 | - * @return boolean |
|
| 228 | - */ |
|
| 229 | - public function hasInternalResolver(): bool |
|
| 230 | - { |
|
| 231 | - return is_callable($this->resolver); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Whether the field has an explicit resolver set. |
|
| 237 | - * |
|
| 238 | - * @param mixed $source The source that's passed down the GraphQL queries |
|
| 239 | - * @param array $args The inputArgs on the field |
|
| 240 | - * @param AppContext $context The AppContext passed down the GraphQL tree |
|
| 241 | - * @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
|
| 242 | - * @return mixed |
|
| 243 | - * @throws LogicException |
|
| 244 | - */ |
|
| 245 | - public function resolve($source, array $args, AppContext $context, ResolveInfo $info) |
|
| 246 | - { |
|
| 247 | - if (! $this->hasInternalResolver()) { |
|
| 248 | - throw new LogicException('GraphQLField has no internal resolver.'); |
|
| 249 | - } |
|
| 250 | - // dynamic methods using $this don't play nice |
|
| 251 | - // so capture resolver to a single var first |
|
| 252 | - $resolver = $this->resolver; |
|
| 253 | - return $resolver($source, $args, $context, $info); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Checks if the format callback is set. |
|
| 259 | - * If yes, then uses it to format the value. |
|
| 260 | - * |
|
| 261 | - * @param mixed $value |
|
| 262 | - * @param EE_Base_Class $source |
|
| 263 | - * @return mixed The formatted value. |
|
| 264 | - * @throws InvalidDataTypeException |
|
| 265 | - * @throws InvalidInterfaceException |
|
| 266 | - * @throws InvalidArgumentException |
|
| 267 | - * @throws ReflectionException |
|
| 268 | - */ |
|
| 269 | - public function mayBeFormatValue($value, EE_Base_Class $source) |
|
| 270 | - { |
|
| 271 | - if (is_callable($this->formatter)) { |
|
| 272 | - // dynamic methods using $this don't play nice |
|
| 273 | - // so capture formatter to a single var first |
|
| 274 | - $formatter = $this->formatter; |
|
| 275 | - return $formatter($value, $source); |
|
| 276 | - } |
|
| 277 | - return $value; |
|
| 278 | - } |
|
| 22 | + /** |
|
| 23 | + * @var string $name |
|
| 24 | + */ |
|
| 25 | + protected $name; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var string|string[] $type |
|
| 29 | + */ |
|
| 30 | + protected $type; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var string|null $key |
|
| 34 | + */ |
|
| 35 | + protected $key; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var string $description |
|
| 39 | + */ |
|
| 40 | + protected $description; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var callable $formatter |
|
| 44 | + */ |
|
| 45 | + protected $formatter; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var callable $resolve |
|
| 49 | + */ |
|
| 50 | + protected $resolver; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var array $caps |
|
| 54 | + */ |
|
| 55 | + protected $caps; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var array $args |
|
| 59 | + */ |
|
| 60 | + protected $args; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @var bool $use_for_input |
|
| 64 | + */ |
|
| 65 | + protected $use_for_input = true; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @var bool $use_for_output |
|
| 69 | + */ |
|
| 70 | + protected $use_for_output = true; |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @param string $name |
|
| 75 | + * @param string|string[] $type |
|
| 76 | + * @param string|null $key |
|
| 77 | + * @param string $description |
|
| 78 | + * @param callable|null $formatter |
|
| 79 | + * @param callable|null $resolver |
|
| 80 | + * @param array $caps |
|
| 81 | + * @param array $args |
|
| 82 | + */ |
|
| 83 | + public function __construct( |
|
| 84 | + string $name, |
|
| 85 | + $type, |
|
| 86 | + string $key = null, |
|
| 87 | + string $description = '', |
|
| 88 | + callable $formatter = null, |
|
| 89 | + callable $resolver = null, |
|
| 90 | + array $caps = [], |
|
| 91 | + array $args = [] |
|
| 92 | + ) { |
|
| 93 | + $this->name = $name; |
|
| 94 | + $this->type = $type; |
|
| 95 | + $this->key = $key; |
|
| 96 | + $this->description = $description; |
|
| 97 | + $this->formatter = $formatter; |
|
| 98 | + $this->resolver = $resolver; |
|
| 99 | + $this->caps = $caps; |
|
| 100 | + $this->args = $args; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @return array |
|
| 106 | + */ |
|
| 107 | + public function caps(): array |
|
| 108 | + { |
|
| 109 | + return $this->caps; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public function description(): string |
|
| 117 | + { |
|
| 118 | + return $this->description; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @return string |
|
| 124 | + */ |
|
| 125 | + public function key(): ?string |
|
| 126 | + { |
|
| 127 | + return $this->key; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function name(): string |
|
| 135 | + { |
|
| 136 | + return $this->name; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @return string|string[] |
|
| 142 | + */ |
|
| 143 | + public function type() |
|
| 144 | + { |
|
| 145 | + return $this->type; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Convert the field to array to be |
|
| 151 | + * able to pass as config to WP GraphQL |
|
| 152 | + * |
|
| 153 | + * @return array |
|
| 154 | + */ |
|
| 155 | + public function toArray(): array |
|
| 156 | + { |
|
| 157 | + return get_object_vars($this); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Sets the value for use_for_input. |
|
| 163 | + * |
|
| 164 | + * @param bool $use_for_input |
|
| 165 | + */ |
|
| 166 | + protected function setUseForInput(bool $use_for_input) |
|
| 167 | + { |
|
| 168 | + $this->use_for_input = filter_var($use_for_input, FILTER_VALIDATE_BOOLEAN); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Whether the field should be used for |
|
| 174 | + * mutation inputs. |
|
| 175 | + * |
|
| 176 | + * @return bool |
|
| 177 | + */ |
|
| 178 | + public function useForInput(): bool |
|
| 179 | + { |
|
| 180 | + return $this->use_for_input; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Whether the field should be used for |
|
| 186 | + * query outputs. |
|
| 187 | + * |
|
| 188 | + * @return bool |
|
| 189 | + */ |
|
| 190 | + public function useForOutput(): bool |
|
| 191 | + { |
|
| 192 | + return $this->use_for_output; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Sets the value for use_for_output |
|
| 198 | + * |
|
| 199 | + * @param bool $use_for_output |
|
| 200 | + */ |
|
| 201 | + protected function setUseForOutput(bool $use_for_output) |
|
| 202 | + { |
|
| 203 | + $this->use_for_output = filter_var($use_for_output, FILTER_VALIDATE_BOOLEAN); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Whether the field should resolve |
|
| 209 | + * based on the user caps etc. |
|
| 210 | + * |
|
| 211 | + * @return boolean |
|
| 212 | + */ |
|
| 213 | + public function shouldResolve(): bool |
|
| 214 | + { |
|
| 215 | + foreach ($this->caps as $cap) { |
|
| 216 | + if (! current_user_can($cap)) { |
|
| 217 | + return false; |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + return true; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Whether the field has an explicit resolver set. |
|
| 226 | + * |
|
| 227 | + * @return boolean |
|
| 228 | + */ |
|
| 229 | + public function hasInternalResolver(): bool |
|
| 230 | + { |
|
| 231 | + return is_callable($this->resolver); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Whether the field has an explicit resolver set. |
|
| 237 | + * |
|
| 238 | + * @param mixed $source The source that's passed down the GraphQL queries |
|
| 239 | + * @param array $args The inputArgs on the field |
|
| 240 | + * @param AppContext $context The AppContext passed down the GraphQL tree |
|
| 241 | + * @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
|
| 242 | + * @return mixed |
|
| 243 | + * @throws LogicException |
|
| 244 | + */ |
|
| 245 | + public function resolve($source, array $args, AppContext $context, ResolveInfo $info) |
|
| 246 | + { |
|
| 247 | + if (! $this->hasInternalResolver()) { |
|
| 248 | + throw new LogicException('GraphQLField has no internal resolver.'); |
|
| 249 | + } |
|
| 250 | + // dynamic methods using $this don't play nice |
|
| 251 | + // so capture resolver to a single var first |
|
| 252 | + $resolver = $this->resolver; |
|
| 253 | + return $resolver($source, $args, $context, $info); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Checks if the format callback is set. |
|
| 259 | + * If yes, then uses it to format the value. |
|
| 260 | + * |
|
| 261 | + * @param mixed $value |
|
| 262 | + * @param EE_Base_Class $source |
|
| 263 | + * @return mixed The formatted value. |
|
| 264 | + * @throws InvalidDataTypeException |
|
| 265 | + * @throws InvalidInterfaceException |
|
| 266 | + * @throws InvalidArgumentException |
|
| 267 | + * @throws ReflectionException |
|
| 268 | + */ |
|
| 269 | + public function mayBeFormatValue($value, EE_Base_Class $source) |
|
| 270 | + { |
|
| 271 | + if (is_callable($this->formatter)) { |
|
| 272 | + // dynamic methods using $this don't play nice |
|
| 273 | + // so capture formatter to a single var first |
|
| 274 | + $formatter = $this->formatter; |
|
| 275 | + return $formatter($value, $source); |
|
| 276 | + } |
|
| 277 | + return $value; |
|
| 278 | + } |
|
| 279 | 279 | } |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | public function shouldResolve(): bool |
| 214 | 214 | { |
| 215 | 215 | foreach ($this->caps as $cap) { |
| 216 | - if (! current_user_can($cap)) { |
|
| 216 | + if ( ! current_user_can($cap)) { |
|
| 217 | 217 | return false; |
| 218 | 218 | } |
| 219 | 219 | } |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | */ |
| 245 | 245 | public function resolve($source, array $args, AppContext $context, ResolveInfo $info) |
| 246 | 246 | { |
| 247 | - if (! $this->hasInternalResolver()) { |
|
| 247 | + if ( ! $this->hasInternalResolver()) { |
|
| 248 | 248 | throw new LogicException('GraphQLField has no internal resolver.'); |
| 249 | 249 | } |
| 250 | 250 | // dynamic methods using $this don't play nice |
@@ -6,57 +6,57 @@ |
||
| 6 | 6 | |
| 7 | 7 | class DecimalValues |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * number of decimal places to round numbers to when performing calculations |
|
| 11 | - * |
|
| 12 | - * @var integer |
|
| 13 | - */ |
|
| 14 | - protected $decimal_precision = 6; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * number of decimal places to round numbers to for display |
|
| 18 | - * |
|
| 19 | - * @var integer |
|
| 20 | - */ |
|
| 21 | - protected $locale_precision = 6; |
|
| 22 | - |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @param EE_Currency_Config $currency_config |
|
| 26 | - */ |
|
| 27 | - public function __construct(EE_Currency_Config $currency_config) |
|
| 28 | - { |
|
| 29 | - $this->locale_precision = $currency_config->dec_plc; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * strips formatting, rounds the provided number, and returns a float |
|
| 34 | - * if $round is set to true, then the decimal precision for the site locale will be used, |
|
| 35 | - * otherwise the default decimal precision of 6 will be used |
|
| 36 | - * |
|
| 37 | - * @param float|int|string $number unformatted number value, ex: 1234.5678956789 |
|
| 38 | - * @param bool $round whether to round the price off according to the locale settings |
|
| 39 | - * @return float rounded value, ex: 1,234.567896 |
|
| 40 | - */ |
|
| 41 | - public function roundDecimalValue($number, bool $round = false): float |
|
| 42 | - { |
|
| 43 | - $precision = $round ? $this->locale_precision : $this->decimal_precision; |
|
| 44 | - return round( |
|
| 45 | - $this->filterDecimalValue($number), |
|
| 46 | - $precision ?? $this->decimal_precision, |
|
| 47 | - PHP_ROUND_HALF_UP |
|
| 48 | - ); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Removes all characters except digits, +- and . |
|
| 54 | - * |
|
| 55 | - * @param float|int|string $number |
|
| 56 | - * @return float |
|
| 57 | - */ |
|
| 58 | - public function filterDecimalValue($number): float |
|
| 59 | - { |
|
| 60 | - return (float) $number; |
|
| 61 | - } |
|
| 9 | + /** |
|
| 10 | + * number of decimal places to round numbers to when performing calculations |
|
| 11 | + * |
|
| 12 | + * @var integer |
|
| 13 | + */ |
|
| 14 | + protected $decimal_precision = 6; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * number of decimal places to round numbers to for display |
|
| 18 | + * |
|
| 19 | + * @var integer |
|
| 20 | + */ |
|
| 21 | + protected $locale_precision = 6; |
|
| 22 | + |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @param EE_Currency_Config $currency_config |
|
| 26 | + */ |
|
| 27 | + public function __construct(EE_Currency_Config $currency_config) |
|
| 28 | + { |
|
| 29 | + $this->locale_precision = $currency_config->dec_plc; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * strips formatting, rounds the provided number, and returns a float |
|
| 34 | + * if $round is set to true, then the decimal precision for the site locale will be used, |
|
| 35 | + * otherwise the default decimal precision of 6 will be used |
|
| 36 | + * |
|
| 37 | + * @param float|int|string $number unformatted number value, ex: 1234.5678956789 |
|
| 38 | + * @param bool $round whether to round the price off according to the locale settings |
|
| 39 | + * @return float rounded value, ex: 1,234.567896 |
|
| 40 | + */ |
|
| 41 | + public function roundDecimalValue($number, bool $round = false): float |
|
| 42 | + { |
|
| 43 | + $precision = $round ? $this->locale_precision : $this->decimal_precision; |
|
| 44 | + return round( |
|
| 45 | + $this->filterDecimalValue($number), |
|
| 46 | + $precision ?? $this->decimal_precision, |
|
| 47 | + PHP_ROUND_HALF_UP |
|
| 48 | + ); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Removes all characters except digits, +- and . |
|
| 54 | + * |
|
| 55 | + * @param float|int|string $number |
|
| 56 | + * @return float |
|
| 57 | + */ |
|
| 58 | + public function filterDecimalValue($number): float |
|
| 59 | + { |
|
| 60 | + return (float) $number; |
|
| 61 | + } |
|
| 62 | 62 | } |
@@ -9,93 +9,93 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class EE_Money_Field extends EE_Float_Field |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var DecimalValues |
|
| 14 | - */ |
|
| 15 | - protected $decimal_values; |
|
| 12 | + /** |
|
| 13 | + * @var DecimalValues |
|
| 14 | + */ |
|
| 15 | + protected $decimal_values; |
|
| 16 | 16 | |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @param string $table_column |
|
| 20 | - * @param string $nicename |
|
| 21 | - * @param bool $nullable |
|
| 22 | - * @param null $default_value |
|
| 23 | - * @throws EE_Error |
|
| 24 | - * @throws ReflectionException |
|
| 25 | - */ |
|
| 26 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
| 27 | - { |
|
| 28 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
| 29 | - $this->setSchemaType('object'); |
|
| 30 | - $this->decimal_values = LoaderFactory::getShared( |
|
| 31 | - 'EventEspresso\core\services\helpers\DecimalValues', |
|
| 32 | - [EE_Currency_Config::getCurrencyConfig()] |
|
| 33 | - ); |
|
| 34 | - } |
|
| 18 | + /** |
|
| 19 | + * @param string $table_column |
|
| 20 | + * @param string $nicename |
|
| 21 | + * @param bool $nullable |
|
| 22 | + * @param null $default_value |
|
| 23 | + * @throws EE_Error |
|
| 24 | + * @throws ReflectionException |
|
| 25 | + */ |
|
| 26 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
| 27 | + { |
|
| 28 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
| 29 | + $this->setSchemaType('object'); |
|
| 30 | + $this->decimal_values = LoaderFactory::getShared( |
|
| 31 | + 'EventEspresso\core\services\helpers\DecimalValues', |
|
| 32 | + [EE_Currency_Config::getCurrencyConfig()] |
|
| 33 | + ); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Schemas: |
|
| 39 | - * 'localized_float': "3,023.00" |
|
| 40 | - * 'no_currency_code': "$3,023.00" |
|
| 41 | - * null: "$3,023.00<span>USD</span>" |
|
| 42 | - * |
|
| 43 | - * @param string $value_on_field_to_be_outputted |
|
| 44 | - * @param string $schema |
|
| 45 | - * @return string |
|
| 46 | - */ |
|
| 47 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null): string |
|
| 48 | - { |
|
| 49 | - if ($schema == 'localized_float') { |
|
| 50 | - return parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
| 51 | - } |
|
| 52 | - $display_code = $schema !== 'no_currency_code'; |
|
| 53 | - // we don't use the $pretty_float because format_currency will take care of it. |
|
| 54 | - return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
| 55 | - } |
|
| 37 | + /** |
|
| 38 | + * Schemas: |
|
| 39 | + * 'localized_float': "3,023.00" |
|
| 40 | + * 'no_currency_code': "$3,023.00" |
|
| 41 | + * null: "$3,023.00<span>USD</span>" |
|
| 42 | + * |
|
| 43 | + * @param string $value_on_field_to_be_outputted |
|
| 44 | + * @param string $schema |
|
| 45 | + * @return string |
|
| 46 | + */ |
|
| 47 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null): string |
|
| 48 | + { |
|
| 49 | + if ($schema == 'localized_float') { |
|
| 50 | + return parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
| 51 | + } |
|
| 52 | + $display_code = $schema !== 'no_currency_code'; |
|
| 53 | + // we don't use the $pretty_float because format_currency will take care of it. |
|
| 54 | + return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
| 60 | - * Rounds the float to the correct number of decimal places for this country's currency. |
|
| 61 | - * Also, interprets periods and commas according to the country's currency settings. |
|
| 62 | - * So if you want to pass in a string that NEEDS to interpret periods as decimal marks,typecast as a float first. |
|
| 63 | - * |
|
| 64 | - * @param string $amount |
|
| 65 | - * @return float |
|
| 66 | - */ |
|
| 67 | - public function prepare_for_set($amount): float |
|
| 68 | - { |
|
| 69 | - // now it's a float-style string or number |
|
| 70 | - $float_val = parent::prepare_for_set($amount); |
|
| 71 | - // round to the correctly number of decimal places for this currency |
|
| 72 | - $float_val = $this->decimal_values->roundDecimalValue($float_val); |
|
| 73 | - // finally, cap money values so they fit nicely within the db's Decimal(12,6) schema |
|
| 74 | - return min($float_val, 999999.999999); |
|
| 75 | - } |
|
| 58 | + /** |
|
| 59 | + * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
| 60 | + * Rounds the float to the correct number of decimal places for this country's currency. |
|
| 61 | + * Also, interprets periods and commas according to the country's currency settings. |
|
| 62 | + * So if you want to pass in a string that NEEDS to interpret periods as decimal marks,typecast as a float first. |
|
| 63 | + * |
|
| 64 | + * @param string $amount |
|
| 65 | + * @return float |
|
| 66 | + */ |
|
| 67 | + public function prepare_for_set($amount): float |
|
| 68 | + { |
|
| 69 | + // now it's a float-style string or number |
|
| 70 | + $float_val = parent::prepare_for_set($amount); |
|
| 71 | + // round to the correctly number of decimal places for this currency |
|
| 72 | + $float_val = $this->decimal_values->roundDecimalValue($float_val); |
|
| 73 | + // finally, cap money values so they fit nicely within the db's Decimal(12,6) schema |
|
| 74 | + return min($float_val, 999999.999999); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * @return array[] |
|
| 80 | - */ |
|
| 81 | - public function getSchemaProperties(): array |
|
| 82 | - { |
|
| 83 | - return [ |
|
| 84 | - 'raw' => [ |
|
| 85 | - 'description' => sprintf( |
|
| 86 | - esc_html__('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
| 87 | - $this->get_nicename() |
|
| 88 | - ), |
|
| 89 | - 'type' => 'number', |
|
| 90 | - ], |
|
| 91 | - 'pretty' => [ |
|
| 92 | - 'description' => sprintf( |
|
| 93 | - esc_html__('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
| 94 | - $this->get_nicename() |
|
| 95 | - ), |
|
| 96 | - 'type' => 'string', |
|
| 97 | - 'format' => 'money', |
|
| 98 | - ], |
|
| 99 | - ]; |
|
| 100 | - } |
|
| 78 | + /** |
|
| 79 | + * @return array[] |
|
| 80 | + */ |
|
| 81 | + public function getSchemaProperties(): array |
|
| 82 | + { |
|
| 83 | + return [ |
|
| 84 | + 'raw' => [ |
|
| 85 | + 'description' => sprintf( |
|
| 86 | + esc_html__('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
| 87 | + $this->get_nicename() |
|
| 88 | + ), |
|
| 89 | + 'type' => 'number', |
|
| 90 | + ], |
|
| 91 | + 'pretty' => [ |
|
| 92 | + 'description' => sprintf( |
|
| 93 | + esc_html__('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
| 94 | + $this->get_nicename() |
|
| 95 | + ), |
|
| 96 | + 'type' => 'string', |
|
| 97 | + 'format' => 'money', |
|
| 98 | + ], |
|
| 99 | + ]; |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -6,84 +6,84 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class EE_Float_Field extends EE_Model_Field_Base |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var EE_Currency_Config |
|
| 11 | - */ |
|
| 12 | - protected $currency; |
|
| 9 | + /** |
|
| 10 | + * @var EE_Currency_Config |
|
| 11 | + */ |
|
| 12 | + protected $currency; |
|
| 13 | 13 | |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @param string $table_column |
|
| 17 | - * @param string $nicename |
|
| 18 | - * @param bool $nullable |
|
| 19 | - * @param null $default_value |
|
| 20 | - */ |
|
| 21 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
| 22 | - { |
|
| 23 | - $this->currency = EE_Config::instance()->currency instanceof EE_Currency_Config |
|
| 24 | - ? EE_Config::instance()->currency |
|
| 25 | - : new EE_Currency_Config(); |
|
| 26 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
| 27 | - $this->setSchemaType('number'); |
|
| 28 | - } |
|
| 15 | + /** |
|
| 16 | + * @param string $table_column |
|
| 17 | + * @param string $nicename |
|
| 18 | + * @param bool $nullable |
|
| 19 | + * @param null $default_value |
|
| 20 | + */ |
|
| 21 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
| 22 | + { |
|
| 23 | + $this->currency = EE_Config::instance()->currency instanceof EE_Currency_Config |
|
| 24 | + ? EE_Config::instance()->currency |
|
| 25 | + : new EE_Currency_Config(); |
|
| 26 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
| 27 | + $this->setSchemaType('number'); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * If provided a string, strips out number-related formatting, like commas, periods, spaces, other junk, etc. |
|
| 33 | - * However, treats commas and periods as thousand-separators ro decimal marks, as indicate by the config's currency. |
|
| 34 | - * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, typecast as float first. |
|
| 35 | - * Returns a float |
|
| 36 | - * |
|
| 37 | - * @param float|string $number |
|
| 38 | - * @return float |
|
| 39 | - */ |
|
| 40 | - public function prepare_for_set($number) |
|
| 41 | - { |
|
| 42 | - // remove whitespaces and thousands separators |
|
| 43 | - if (is_string($number)) { |
|
| 44 | - // scientific notation can just be cast as a float |
|
| 45 | - if (strpos($number, 'e') || strpos($number, 'E')) { |
|
| 46 | - return (float) $number; |
|
| 47 | - } |
|
| 48 | - $number = str_replace( |
|
| 49 | - array(" ", $this->currency->thsnds), |
|
| 50 | - "", |
|
| 51 | - $number |
|
| 52 | - ); |
|
| 53 | - // normalize it so periods are decimal marks (we don't care where you're from: we're talking PHP now) |
|
| 54 | - $number = str_replace( |
|
| 55 | - $this->currency->dec_mrk, |
|
| 56 | - ".", |
|
| 57 | - $number |
|
| 58 | - ); |
|
| 59 | - // double-check there's absolutely nothing left on this string besides numbers |
|
| 60 | - $number = preg_replace( |
|
| 61 | - "/[^0-9,.]/", |
|
| 62 | - "", |
|
| 63 | - $number |
|
| 64 | - ); |
|
| 65 | - } |
|
| 66 | - return (float) $number; |
|
| 67 | - } |
|
| 31 | + /** |
|
| 32 | + * If provided a string, strips out number-related formatting, like commas, periods, spaces, other junk, etc. |
|
| 33 | + * However, treats commas and periods as thousand-separators ro decimal marks, as indicate by the config's currency. |
|
| 34 | + * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, typecast as float first. |
|
| 35 | + * Returns a float |
|
| 36 | + * |
|
| 37 | + * @param float|string $number |
|
| 38 | + * @return float |
|
| 39 | + */ |
|
| 40 | + public function prepare_for_set($number) |
|
| 41 | + { |
|
| 42 | + // remove whitespaces and thousands separators |
|
| 43 | + if (is_string($number)) { |
|
| 44 | + // scientific notation can just be cast as a float |
|
| 45 | + if (strpos($number, 'e') || strpos($number, 'E')) { |
|
| 46 | + return (float) $number; |
|
| 47 | + } |
|
| 48 | + $number = str_replace( |
|
| 49 | + array(" ", $this->currency->thsnds), |
|
| 50 | + "", |
|
| 51 | + $number |
|
| 52 | + ); |
|
| 53 | + // normalize it so periods are decimal marks (we don't care where you're from: we're talking PHP now) |
|
| 54 | + $number = str_replace( |
|
| 55 | + $this->currency->dec_mrk, |
|
| 56 | + ".", |
|
| 57 | + $number |
|
| 58 | + ); |
|
| 59 | + // double-check there's absolutely nothing left on this string besides numbers |
|
| 60 | + $number = preg_replace( |
|
| 61 | + "/[^0-9,.]/", |
|
| 62 | + "", |
|
| 63 | + $number |
|
| 64 | + ); |
|
| 65 | + } |
|
| 66 | + return (float) $number; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Returns the number formatted according to local custom (set by the country of the blog). |
|
| 71 | - * |
|
| 72 | - * @param float $number |
|
| 73 | - * @return string |
|
| 74 | - */ |
|
| 75 | - public function prepare_for_pretty_echoing($number, $schema = null) |
|
| 76 | - { |
|
| 77 | - return number_format( |
|
| 78 | - $number, |
|
| 79 | - $this->currency->dec_plc, |
|
| 80 | - $this->currency->dec_mrk, |
|
| 81 | - $this->currency->thsnds |
|
| 82 | - ); |
|
| 83 | - } |
|
| 69 | + /** |
|
| 70 | + * Returns the number formatted according to local custom (set by the country of the blog). |
|
| 71 | + * |
|
| 72 | + * @param float $number |
|
| 73 | + * @return string |
|
| 74 | + */ |
|
| 75 | + public function prepare_for_pretty_echoing($number, $schema = null) |
|
| 76 | + { |
|
| 77 | + return number_format( |
|
| 78 | + $number, |
|
| 79 | + $this->currency->dec_plc, |
|
| 80 | + $this->currency->dec_mrk, |
|
| 81 | + $this->currency->thsnds |
|
| 82 | + ); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - public function prepare_for_set_from_db($number) |
|
| 86 | - { |
|
| 87 | - return (float) $number; |
|
| 88 | - } |
|
| 85 | + public function prepare_for_set_from_db($number) |
|
| 86 | + { |
|
| 87 | + return (float) $number; |
|
| 88 | + } |
|
| 89 | 89 | } |
@@ -10,33 +10,33 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class GraphQLInputField extends GraphQLField |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * @param string $name |
|
| 15 | - * @param string|string[] $type |
|
| 16 | - * @param string|null $key |
|
| 17 | - * @param string $description |
|
| 18 | - * @param callable|null $formatter |
|
| 19 | - * @param callable|null $resolver |
|
| 20 | - * @param array $caps |
|
| 21 | - * @param array $args |
|
| 22 | - */ |
|
| 23 | - public function __construct( |
|
| 24 | - string $name, |
|
| 25 | - $type, |
|
| 26 | - string $key = null, |
|
| 27 | - string $description = '', |
|
| 28 | - array $caps = [] |
|
| 29 | - ) { |
|
| 30 | - parent::__construct( |
|
| 31 | - $name, |
|
| 32 | - $type, |
|
| 33 | - $key, |
|
| 34 | - $description, |
|
| 35 | - null, |
|
| 36 | - null, |
|
| 37 | - $caps |
|
| 38 | - ); |
|
| 13 | + /** |
|
| 14 | + * @param string $name |
|
| 15 | + * @param string|string[] $type |
|
| 16 | + * @param string|null $key |
|
| 17 | + * @param string $description |
|
| 18 | + * @param callable|null $formatter |
|
| 19 | + * @param callable|null $resolver |
|
| 20 | + * @param array $caps |
|
| 21 | + * @param array $args |
|
| 22 | + */ |
|
| 23 | + public function __construct( |
|
| 24 | + string $name, |
|
| 25 | + $type, |
|
| 26 | + string $key = null, |
|
| 27 | + string $description = '', |
|
| 28 | + array $caps = [] |
|
| 29 | + ) { |
|
| 30 | + parent::__construct( |
|
| 31 | + $name, |
|
| 32 | + $type, |
|
| 33 | + $key, |
|
| 34 | + $description, |
|
| 35 | + null, |
|
| 36 | + null, |
|
| 37 | + $caps |
|
| 38 | + ); |
|
| 39 | 39 | |
| 40 | - $this->setUseForOutput(false); |
|
| 41 | - } |
|
| 40 | + $this->setUseForOutput(false); |
|
| 41 | + } |
|
| 42 | 42 | } |