Primo Committ

This commit is contained in:
paoloar77
2024-05-07 12:17:25 +02:00
commit e73d0e5113
7204 changed files with 884387 additions and 0 deletions

19
vendor/asm89/stack-cors/LICENSE vendored Normal file
View File

@@ -0,0 +1,19 @@
Copyright (c) 2013-2017 Alexander <iam.asm89@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

83
vendor/asm89/stack-cors/README.md vendored Normal file
View File

@@ -0,0 +1,83 @@
# Stack/Cors
Library and middleware enabling cross-origin resource sharing for your
http-{foundation,kernel} using application. It attempts to implement the
[W3C Recommendation] for cross-origin resource sharing.
[W3C Recommendation]: http://www.w3.org/TR/cors/
Build status: ![.github/workflows/run-tests.yml](https://github.com/asm89/stack-cors/workflows/.github/workflows/run-tests.yml/badge.svg)
## Installation
Require `asm89/stack-cors` using composer.
## Usage
This package can be used as a library or as [stack middleware].
[stack middleware]: http://stackphp.com/
### Options
| Option | Description | Default value |
|------------------------|------------------------------------------------------------|---------------|
| allowedMethods | Matches the request method. | `[]` |
| allowedOrigins | Matches the request origin. | `[]` |
| allowedOriginsPatterns | Matches the request origin with `preg_match`. | `[]` |
| allowedHeaders | Sets the Access-Control-Allow-Headers response header. | `[]` |
| exposedHeaders | Sets the Access-Control-Expose-Headers response header. | `false` |
| maxAge | Sets the Access-Control-Max-Age response header. | `false` |
| supportsCredentials | Sets the Access-Control-Allow-Credentials header. | `false` |
The _allowedMethods_ and _allowedHeaders_ options are case-insensitive.
You don't need to provide both _allowedOrigins_ and _allowedOriginsPatterns_. If one of the strings passed matches, it is considered a valid origin.
If `['*']` is provided to _allowedMethods_, _allowedOrigins_ or _allowedHeaders_ all methods / origins / headers are allowed.
### Example: using the library
```php
<?php
use Asm89\Stack\CorsService;
$cors = new CorsService([
'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'],
'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'],
'allowedOrigins' => ['http://localhost'],
'allowedOriginsPatterns' => ['/localhost:\d/'],
'exposedHeaders' => false,
'maxAge' => false,
'supportsCredentials' => false,
]);
$cors->addActualRequestHeaders(Response $response, $origin);
$cors->handlePreflightRequest(Request $request);
$cors->isActualRequestAllowed(Request $request);
$cors->isCorsRequest(Request $request);
$cors->isPreflightRequest(Request $request);
```
## Example: using the stack middleware
```php
<?php
use Asm89\Stack\Cors;
$app = new Cors($app, [
// you can use ['*'] to allow any headers
'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'],
// you can use ['*'] to allow any methods
'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'],
// you can use ['*'] to allow requests from any origin
'allowedOrigins' => ['localhost'],
// you can enter regexes that are matched to the origin request header
'allowedOriginsPatterns' => ['/localhost:\d/'],
'exposedHeaders' => false,
'maxAge' => false,
'supportsCredentials' => false,
]);
```

43
vendor/asm89/stack-cors/composer.json vendored Normal file
View File

@@ -0,0 +1,43 @@
{
"name": "asm89/stack-cors",
"description": "Cross-origin resource sharing library and stack middleware",
"keywords": ["stack", "cors"],
"homepage": "https://github.com/asm89/stack-cors",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Alexander",
"email": "iam.asm89@gmail.com"
}
],
"require": {
"php": "^7.2|^8.0",
"symfony/http-foundation": "^4|^5|^6",
"symfony/http-kernel": "^4|^5|^6"
},
"require-dev": {
"phpunit/phpunit": "^7|^9",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
"Asm89\\Stack\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Asm89\\Stack\\Tests\\": "tests/"
}
},
"scripts": {
"test": "phpunit",
"check-style": "phpcs -p --standard=PSR12 --exclude=Generic.Files.LineLength --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src",
"fix-style": "phpcbf -p --standard=PSR12 --exclude=Generic.Files.LineLength --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src"
},
"extra": {
"branch-alias": {
"dev-master": "2.1-dev"
}
}
}

61
vendor/asm89/stack-cors/src/Cors.php vendored Normal file
View File

@@ -0,0 +1,61 @@
<?php
/*
* This file is part of asm89/stack-cors.
*
* (c) Alexander <iam.asm89@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Asm89\Stack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Request;
class Cors implements HttpKernelInterface
{
/**
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/
private $app;
/**
* @var \Asm89\Stack\CorsService
*/
private $cors;
private $defaultOptions = [
'allowedHeaders' => [],
'allowedMethods' => [],
'allowedOrigins' => [],
'allowedOriginsPatterns' => [],
'exposedHeaders' => [],
'maxAge' => 0,
'supportsCredentials' => false,
];
public function __construct(HttpKernelInterface $app, array $options = [])
{
$this->app = $app;
$this->cors = new CorsService(array_merge($this->defaultOptions, $options));
}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response
{
if ($this->cors->isPreflightRequest($request)) {
$response = $this->cors->handlePreflightRequest($request);
return $this->cors->varyHeader($response, 'Access-Control-Request-Method');
}
$response = $this->app->handle($request, $type, $catch);
if ($request->getMethod() === 'OPTIONS') {
$this->cors->varyHeader($response, 'Access-Control-Request-Method');
}
return $this->cors->addActualRequestHeaders($response, $request);
}
}

View File

@@ -0,0 +1,225 @@
<?php
/*
* This file is part of asm89/stack-cors.
*
* (c) Alexander <iam.asm89@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Asm89\Stack;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class CorsService
{
private $options;
public function __construct(array $options = [])
{
$this->options = $this->normalizeOptions($options);
}
private function normalizeOptions(array $options = []): array
{
$options += [
'allowedOrigins' => [],
'allowedOriginsPatterns' => [],
'supportsCredentials' => false,
'allowedHeaders' => [],
'exposedHeaders' => [],
'allowedMethods' => [],
'maxAge' => 0,
];
// normalize array('*') to true
if (in_array('*', $options['allowedOrigins'])) {
$options['allowedOrigins'] = true;
}
if (in_array('*', $options['allowedHeaders'])) {
$options['allowedHeaders'] = true;
} else {
$options['allowedHeaders'] = array_map('strtolower', $options['allowedHeaders']);
}
if (in_array('*', $options['allowedMethods'])) {
$options['allowedMethods'] = true;
} else {
$options['allowedMethods'] = array_map('strtoupper', $options['allowedMethods']);
}
return $options;
}
/**
* @deprecated use isOriginAllowed
*/
public function isActualRequestAllowed(Request $request): bool
{
return $this->isOriginAllowed($request);
}
public function isCorsRequest(Request $request): bool
{
return $request->headers->has('Origin');
}
public function isPreflightRequest(Request $request): bool
{
return $request->getMethod() === 'OPTIONS' && $request->headers->has('Access-Control-Request-Method');
}
public function handlePreflightRequest(Request $request): Response
{
$response = new Response();
$response->setStatusCode(204);
return $this->addPreflightRequestHeaders($response, $request);
}
public function addPreflightRequestHeaders(Response $response, Request $request): Response
{
$this->configureAllowedOrigin($response, $request);
if ($response->headers->has('Access-Control-Allow-Origin')) {
$this->configureAllowCredentials($response, $request);
$this->configureAllowedMethods($response, $request);
$this->configureAllowedHeaders($response, $request);
$this->configureMaxAge($response, $request);
}
return $response;
}
public function isOriginAllowed(Request $request): bool
{
if ($this->options['allowedOrigins'] === true) {
return true;
}
if (!$request->headers->has('Origin')) {
return false;
}
$origin = $request->headers->get('Origin');
if (in_array($origin, $this->options['allowedOrigins'])) {
return true;
}
foreach ($this->options['allowedOriginsPatterns'] as $pattern) {
if (preg_match($pattern, $origin)) {
return true;
}
}
return false;
}
public function addActualRequestHeaders(Response $response, Request $request): Response
{
$this->configureAllowedOrigin($response, $request);
if ($response->headers->has('Access-Control-Allow-Origin')) {
$this->configureAllowCredentials($response, $request);
$this->configureExposedHeaders($response, $request);
}
return $response;
}
private function configureAllowedOrigin(Response $response, Request $request)
{
if ($this->options['allowedOrigins'] === true && !$this->options['supportsCredentials']) {
// Safe+cacheable, allow everything
$response->headers->set('Access-Control-Allow-Origin', '*');
} elseif ($this->isSingleOriginAllowed()) {
// Single origins can be safely set
$response->headers->set('Access-Control-Allow-Origin', array_values($this->options['allowedOrigins'])[0]);
} else {
// For dynamic headers, set the requested Origin header when set and allowed
if ($this->isCorsRequest($request) && $this->isOriginAllowed($request)) {
$response->headers->set('Access-Control-Allow-Origin', $request->headers->get('Origin'));
}
$this->varyHeader($response, 'Origin');
}
}
private function isSingleOriginAllowed(): bool
{
if ($this->options['allowedOrigins'] === true || !empty($this->options['allowedOriginsPatterns'])) {
return false;
}
return count($this->options['allowedOrigins']) === 1;
}
private function configureAllowedMethods(Response $response, Request $request)
{
if ($this->options['allowedMethods'] === true) {
$allowMethods = strtoupper($request->headers->get('Access-Control-Request-Method'));
$this->varyHeader($response, 'Access-Control-Request-Method');
} else {
$allowMethods = implode(', ', $this->options['allowedMethods']);
}
$response->headers->set('Access-Control-Allow-Methods', $allowMethods);
}
private function configureAllowedHeaders(Response $response, Request $request)
{
if ($this->options['allowedHeaders'] === true) {
$allowHeaders = $request->headers->get('Access-Control-Request-Headers');
$this->varyHeader($response, 'Access-Control-Request-Headers');
} else {
$allowHeaders = implode(', ', $this->options['allowedHeaders']);
}
$response->headers->set('Access-Control-Allow-Headers', $allowHeaders);
}
private function configureAllowCredentials(Response $response, Request $request)
{
if ($this->options['supportsCredentials']) {
$response->headers->set('Access-Control-Allow-Credentials', 'true');
}
}
private function configureExposedHeaders(Response $response, Request $request)
{
if ($this->options['exposedHeaders']) {
$response->headers->set('Access-Control-Expose-Headers', implode(', ', $this->options['exposedHeaders']));
}
}
private function configureMaxAge(Response $response, Request $request)
{
if ($this->options['maxAge'] !== null) {
$response->headers->set('Access-Control-Max-Age', (int) $this->options['maxAge']);
}
}
public function varyHeader(Response $response, $header): Response
{
if (!$response->headers->has('Vary')) {
$response->headers->set('Vary', $header);
} elseif (!in_array($header, explode(', ', $response->headers->get('Vary')))) {
$response->headers->set('Vary', $response->headers->get('Vary') . ', ' . $header);
}
return $response;
}
private function isSameHost(Request $request): bool
{
return $request->headers->get('Origin') === $request->getSchemeAndHttpHost();
}
}

12
vendor/autoload.php vendored Normal file
View File

@@ -0,0 +1,12 @@
<?php
// autoload.php @generated by Composer
if (PHP_VERSION_ID < 50600) {
echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
exit(1);
}
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit67b256f53a0993f7a39d76612331a21d::getLoader();

21
vendor/automattic/woocommerce/LICENSE vendored Normal file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016, Automattic (https://automattic.com/)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

169
vendor/automattic/woocommerce/README.md vendored Normal file
View File

@@ -0,0 +1,169 @@
# WooCommerce API - PHP Client
A PHP wrapper for the WooCommerce REST API. Easily interact with the WooCommerce REST API securely using this library. If using a HTTPS connection this library uses BasicAuth, else it uses Oauth to provide a secure connection to WooCommerce.
[![CI status](https://github.com/woocommerce/wc-api-php/actions/workflows/ci.yml/badge.svg?branch=trunk)](https://github.com/woocommerce/wc-api-php/actions/workflows/ci.yml)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/woocommerce/wc-api-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/woocommerce/wc-api-php/?branch=master)
[![PHP version](https://badge.fury.io/ph/automattic%2Fwoocommerce.svg)](https://packagist.org/packages/automattic/woocommerce)
## Installation
```
composer require automattic/woocommerce
```
## Getting started
Generate API credentials (Consumer Key & Consumer Secret) following this instructions <http://docs.woocommerce.com/document/woocommerce-rest-api/>
.
Check out the WooCommerce API endpoints and data that can be manipulated in <https://woocommerce.github.io/woocommerce-rest-api-docs/>.
## Setup
Setup for the new WP REST API integration (WooCommerce 2.6 or later):
```php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'http://example.com',
'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
[
'version' => 'wc/v3',
]
);
```
## Client class
```php
$woocommerce = new Client($url, $consumer_key, $consumer_secret, $options);
```
### Options
| Option | Type | Required | Description |
| ----------------- | -------- | -------- | ------------------------------------------ |
| `url` | `string` | yes | Your Store URL, example: http://woo.dev/ |
| `consumer_key` | `string` | yes | Your API consumer key |
| `consumer_secret` | `string` | yes | Your API consumer secret |
| `options` | `array` | no | Extra arguments (see client options table) |
#### Client options
| Option | Type | Required | Description |
| ------------------------ | -------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `version` | `string` | no | API version, default is `wc/v3` |
| `timeout` | `int` | no | Request timeout, default is `15` |
| `verify_ssl` | `bool` | no | Verify SSL when connect, use this option as `false` when need to test with self-signed certificates, default is `true` |
| `follow_redirects` | `bool` | no | Allow the API call to follow redirects |
| `query_string_auth` | `bool` | no | Force Basic Authentication as query string when `true` and using under HTTPS, default is `false` |
| `oauth_timestamp` | `string` | no | Custom oAuth timestamp, default is `time()` |
| `oauth_only` | `bool` | no | Only use oauth for requests, it will disable Basic Auth, default is `false` |
| `user_agent` | `string` | no | Custom user-agent, default is `WooCommerce API Client-PHP` |
| `wp_api_prefix` | `string` | no | Custom WP REST API URL prefix, used to support custom prefixes created with the `rest_url_prefix` filter |
| `wp_api` | `bool` | no | Set to `false` in order to use the legacy WooCommerce REST API (deprecated and not recommended) |
| `method_override_query` | `bool` | no | If true will mask all non-GET/POST methods by using POST method with added query parameter `?_method=METHOD` into URL |
| `method_override_header` | `bool` | no | If true will mask all non-GET/POST methods (PUT/DELETE/etc.) by using POST method with added `X-HTTP-Method-Override: METHOD` HTTP header into request |
## Client methods
### GET
```php
$woocommerce->get($endpoint, $parameters = []);
```
### POST
```php
$woocommerce->post($endpoint, $data);
```
### PUT
```php
$woocommerce->put($endpoint, $data);
```
### DELETE
```php
$woocommerce->delete($endpoint, $parameters = []);
```
### OPTIONS
```php
$woocommerce->options($endpoint);
```
#### Arguments
| Params | Type | Description |
| ------------ | -------- | ------------------------------------------------------------ |
| `endpoint` | `string` | WooCommerce API endpoint, example: `customers` or `order/12` |
| `data` | `array` | Only for POST and PUT, data that will be converted to JSON |
| `parameters` | `array` | Only for GET and DELETE, request query string |
#### Response
All methods will return arrays on success or throwing `HttpClientException` errors on failure.
```php
use Automattic\WooCommerce\HttpClient\HttpClientException;
try {
// Array of response results.
$results = $woocommerce->get('customers');
// Example: ['customers' => [[ 'id' => 8, 'created_at' => '2015-05-06T17:43:51Z', 'email' => ...
echo '<pre><code>' . print_r($results, true) . '</code><pre>'; // JSON output.
// Last request data.
$lastRequest = $woocommerce->http->getRequest();
echo '<pre><code>' . print_r($lastRequest->getUrl(), true) . '</code><pre>'; // Requested URL (string).
echo '<pre><code>' .
print_r($lastRequest->getMethod(), true) .
'</code><pre>'; // Request method (string).
echo '<pre><code>' .
print_r($lastRequest->getParameters(), true) .
'</code><pre>'; // Request parameters (array).
echo '<pre><code>' .
print_r($lastRequest->getHeaders(), true) .
'</code><pre>'; // Request headers (array).
echo '<pre><code>' . print_r($lastRequest->getBody(), true) . '</code><pre>'; // Request body (JSON).
// Last response data.
$lastResponse = $woocommerce->http->getResponse();
echo '<pre><code>' . print_r($lastResponse->getCode(), true) . '</code><pre>'; // Response code (int).
echo '<pre><code>' .
print_r($lastResponse->getHeaders(), true) .
'</code><pre>'; // Response headers (array).
echo '<pre><code>' . print_r($lastResponse->getBody(), true) . '</code><pre>'; // Response body (JSON).
} catch (HttpClientException $e) {
echo '<pre><code>' . print_r($e->getMessage(), true) . '</code><pre>'; // Error message.
echo '<pre><code>' . print_r($e->getRequest(), true) . '</code><pre>'; // Last request data.
echo '<pre><code>' . print_r($e->getResponse(), true) . '</code><pre>'; // Last response data.
}
```
## Release History
- 2022-03-18 - 3.1.0 - Added new options to support `_method` and `X-HTTP-Method-Override` from WP, supports 7+, dropped support to PHP 5.
- 2019-01-16 - 3.0.0 - Legacy API turned off by default, and improved JSON error handler.
- 2018-03-29 - 2.0.1 - Fixed fatal errors on `lookForErrors`.
- 2018-01-12 - 2.0.0 - Responses changes from arrays to `stdClass` objects. Added `follow_redirects` option.
- 2017-06-06 - 1.3.0 - Remove BOM before decoding and added support for multi-dimensional arrays for oAuth1.0a.
- 2017-03-15 - 1.2.0 - Added `user_agent` option.
- 2016-12-14 - 1.1.4 - Fixed WordPress 4.7 compatibility.
- 2016-10-26 - 1.1.3 - Allow set `oauth_timestamp` and improved how is handled the response headers.
- 2016-09-30 - 1.1.2 - Added `wp_api_prefix` option to allow custom WP REST API URL prefix.
- 2016-05-10 - 1.1.1 - Fixed oAuth and error handler for WP REST API.
- 2016-05-09 - 1.1.0 - Added support for WP REST API, added method `Automattic\WooCommerce\Client::options` and fixed multiple headers responses.
- 2016-01-25 - 1.0.2 - Fixed an error when getting data containing non-latin characters.
- 2016-01-21 - 1.0.1 - Sort all oAuth parameters before build request URLs.
- 2016-01-11 - 1.0.0 - Stable release.

View File

@@ -0,0 +1,38 @@
{
"name": "automattic/woocommerce",
"description": "A PHP wrapper for the WooCommerce REST API",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Claudio Sanches",
"email": "claudio.sanches@automattic.com"
}
],
"minimum-stability": "dev",
"keywords": [
"API",
"WooCommerce"
],
"require": {
"php": ">= 7.1.0",
"ext-curl": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^8",
"squizlabs/php_codesniffer": "3.*",
"overtrue/phplint": "7.4.x-dev"
},
"autoload": {
"psr-4": {
"Automattic\\WooCommerce\\": ["src/WooCommerce"]
}
},
"autoload-dev": {
"psr-4": {
"Automattic\\WooCommerce\\LegacyTests\\": "tests/legacy-php/WooCommerce/Tests",
"Automattic\\WooCommerce\\Tests\\": "tests/php/WooCommerce/Tests"
}
}
}

View File

@@ -0,0 +1,109 @@
<?php
/**
* WooCommerce REST API Client
*
* @category Client
* @package Automattic/WooCommerce
*/
namespace Automattic\WooCommerce;
use Automattic\WooCommerce\HttpClient\HttpClient;
/**
* REST API Client class.
*
* @package Automattic/WooCommerce
*/
class Client
{
/**
* WooCommerce REST API Client version.
*/
public const VERSION = '3.1.0';
/**
* HttpClient instance.
*
* @var HttpClient
*/
public $http;
/**
* Initialize client.
*
* @param string $url Store URL.
* @param string $consumerKey Consumer key.
* @param string $consumerSecret Consumer secret.
* @param array $options Options (version, timeout, verify_ssl, oauth_only).
*/
public function __construct($url, $consumerKey, $consumerSecret, $options = [])
{
$this->http = new HttpClient($url, $consumerKey, $consumerSecret, $options);
}
/**
* POST method.
*
* @param string $endpoint API endpoint.
* @param array $data Request data.
*
* @return \stdClass
*/
public function post($endpoint, $data)
{
return $this->http->request($endpoint, 'POST', $data);
}
/**
* PUT method.
*
* @param string $endpoint API endpoint.
* @param array $data Request data.
*
* @return \stdClass
*/
public function put($endpoint, $data)
{
return $this->http->request($endpoint, 'PUT', $data);
}
/**
* GET method.
*
* @param string $endpoint API endpoint.
* @param array $parameters Request parameters.
*
* @return \stdClass
*/
public function get($endpoint, $parameters = [])
{
return $this->http->request($endpoint, 'GET', [], $parameters);
}
/**
* DELETE method.
*
* @param string $endpoint API endpoint.
* @param array $parameters Request parameters.
*
* @return \stdClass
*/
public function delete($endpoint, $parameters = [])
{
return $this->http->request($endpoint, 'DELETE', [], $parameters);
}
/**
* OPTIONS method.
*
* @param string $endpoint API endpoint.
*
* @return \stdClass
*/
public function options($endpoint)
{
return $this->http->request($endpoint, 'OPTIONS', [], []);
}
}

View File

@@ -0,0 +1,96 @@
<?php
/**
* WooCommerce Basic Authentication
*
* @category HttpClient
* @package Automattic/WooCommerce
*/
namespace Automattic\WooCommerce\HttpClient;
/**
* Basic Authentication class.
*
* @package Automattic/WooCommerce
*/
class BasicAuth
{
/**
* cURL handle.
*
* @var resource
*/
protected $ch;
/**
* Consumer key.
*
* @var string
*/
protected $consumerKey;
/**
* Consumer secret.
*
* @var string
*/
protected $consumerSecret;
/**
* Do query string auth.
*
* @var bool
*/
protected $doQueryString;
/**
* Request parameters.
*
* @var array
*/
protected $parameters;
/**
* Initialize Basic Authentication class.
*
* @param resource $ch cURL handle.
* @param string $consumerKey Consumer key.
* @param string $consumerSecret Consumer Secret.
* @param bool $doQueryString Do or not query string auth.
* @param array $parameters Request parameters.
*/
public function __construct($ch, $consumerKey, $consumerSecret, $doQueryString, $parameters = [])
{
$this->ch = $ch;
$this->consumerKey = $consumerKey;
$this->consumerSecret = $consumerSecret;
$this->doQueryString = $doQueryString;
$this->parameters = $parameters;
$this->processAuth();
}
/**
* Process auth.
*/
protected function processAuth()
{
if ($this->doQueryString) {
$this->parameters['consumer_key'] = $this->consumerKey;
$this->parameters['consumer_secret'] = $this->consumerSecret;
} else {
\curl_setopt($this->ch, CURLOPT_USERPWD, $this->consumerKey . ':' . $this->consumerSecret);
}
}
/**
* Get parameters.
*
* @return array
*/
public function getParameters()
{
return $this->parameters;
}
}

View File

@@ -0,0 +1,487 @@
<?php
/**
* WooCommerce REST API HTTP Client
*
* @category HttpClient
* @package Automattic/WooCommerce
*/
namespace Automattic\WooCommerce\HttpClient;
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\BasicAuth;
use Automattic\WooCommerce\HttpClient\HttpClientException;
use Automattic\WooCommerce\HttpClient\OAuth;
use Automattic\WooCommerce\HttpClient\Options;
use Automattic\WooCommerce\HttpClient\Request;
use Automattic\WooCommerce\HttpClient\Response;
/**
* REST API HTTP Client class.
*
* @package Automattic/WooCommerce
*/
class HttpClient
{
/**
* cURL handle.
*
* @var resource
*/
protected $ch;
/**
* Store API URL.
*
* @var string
*/
protected $url;
/**
* Consumer key.
*
* @var string
*/
protected $consumerKey;
/**
* Consumer secret.
*
* @var string
*/
protected $consumerSecret;
/**
* Client options.
*
* @var Options
*/
protected $options;
/**
* The custom cURL options to use in the requests.
*
* @var array
*/
private $customCurlOptions = [];
/**
* Request.
*
* @var Request
*/
private $request;
/**
* Response.
*
* @var Response
*/
private $response;
/**
* Response headers.
*
* @var string
*/
private $responseHeaders;
/**
* Initialize HTTP client.
*
* @param string $url Store URL.
* @param string $consumerKey Consumer key.
* @param string $consumerSecret Consumer Secret.
* @param array $options Client options.
*/
public function __construct($url, $consumerKey, $consumerSecret, $options)
{
if (!\function_exists('curl_version')) {
throw new HttpClientException('cURL is NOT installed on this server', -1, new Request(), new Response());
}
$this->options = new Options($options);
$this->url = $this->buildApiUrl($url);
$this->consumerKey = $consumerKey;
$this->consumerSecret = $consumerSecret;
}
/**
* Check if is under SSL.
*
* @return bool
*/
protected function isSsl()
{
return 'https://' === \substr($this->url, 0, 8);
}
/**
* Build API URL.
*
* @param string $url Store URL.
*
* @return string
*/
protected function buildApiUrl($url)
{
$api = $this->options->isWPAPI() ? $this->options->apiPrefix() : '/wc-api/';
return \rtrim($url, '/') . $api . $this->options->getVersion() . '/';
}
/**
* Build URL.
*
* @param string $url URL.
* @param array $parameters Query string parameters.
*
* @return string
*/
protected function buildUrlQuery($url, $parameters = [])
{
if (!empty($parameters)) {
if (false !== strpos($url, '?')) {
$url .= '&' . \http_build_query($parameters);
} else {
$url .= '?' . \http_build_query($parameters);
}
}
return $url;
}
/**
* Authenticate.
*
* @param string $url Request URL.
* @param string $method Request method.
* @param array $parameters Request parameters.
*
* @return array
*/
protected function authenticate($url, $method, $parameters = [])
{
// Setup authentication.
if (!$this->options->isOAuthOnly() && $this->isSsl()) {
$basicAuth = new BasicAuth(
$this->ch,
$this->consumerKey,
$this->consumerSecret,
$this->options->isQueryStringAuth(),
$parameters
);
$parameters = $basicAuth->getParameters();
} else {
$oAuth = new OAuth(
$url,
$this->consumerKey,
$this->consumerSecret,
$this->options->getVersion(),
$method,
$parameters,
$this->options->oauthTimestamp()
);
$parameters = $oAuth->getParameters();
}
return $parameters;
}
/**
* Setup method.
*
* @param string $method Request method.
*/
protected function setupMethod($method)
{
if ('POST' == $method) {
\curl_setopt($this->ch, CURLOPT_POST, true);
} elseif (\in_array($method, ['PUT', 'DELETE', 'OPTIONS'])) {
\curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method);
}
}
/**
* Get request headers.
*
* @param bool $sendData If request send data or not.
*
* @return array
*/
protected function getRequestHeaders($sendData = false)
{
$headers = [
'Accept' => 'application/json',
'User-Agent' => $this->options->userAgent() . '/' . Client::VERSION,
];
if ($sendData) {
$headers['Content-Type'] = 'application/json;charset=utf-8';
}
return $headers;
}
/**
* Create request.
*
* @param string $endpoint Request endpoint.
* @param string $method Request method.
* @param array $data Request data.
* @param array $parameters Request parameters.
*
* @return Request
*/
protected function createRequest($endpoint, $method, $data = [], $parameters = [])
{
$body = '';
$url = $this->url . $endpoint;
$hasData = !empty($data);
$headers = $this->getRequestHeaders($hasData);
// HTTP method override feature which masks PUT and DELETE HTTP methods as POST method with added
// ?_method=PUT query parameter and/or X-HTTP-Method-Override HTTP header.
if (!in_array($method, ['GET', 'POST'])) {
$usePostMethod = false;
if ($this->options->isMethodOverrideQuery()) {
$parameters = array_merge(['_method' => $method], $parameters);
$usePostMethod = true;
}
if ($this->options->isMethodOverrideHeader()) {
$headers['X-HTTP-Method-Override'] = $method;
$usePostMethod = true;
}
if ($usePostMethod) {
$method = 'POST';
}
}
// Setup authentication.
$parameters = $this->authenticate($url, $method, $parameters);
// Setup method.
$this->setupMethod($method);
// Include post fields.
if ($hasData) {
$body = \json_encode($data);
\curl_setopt($this->ch, CURLOPT_POSTFIELDS, $body);
}
$this->request = new Request(
$this->buildUrlQuery($url, $parameters),
$method,
$parameters,
$headers,
$body
);
return $this->getRequest();
}
/**
* Get response headers.
*
* @return array
*/
protected function getResponseHeaders()
{
$headers = [];
$lines = \explode("\n", $this->responseHeaders);
$lines = \array_filter($lines, 'trim');
foreach ($lines as $index => $line) {
// Remove HTTP/xxx params.
if (strpos($line, ': ') === false) {
continue;
}
list($key, $value) = \explode(': ', $line);
$headers[$key] = isset($headers[$key]) ? $headers[$key] . ', ' . trim($value) : trim($value);
}
return $headers;
}
/**
* Create response.
*
* @return Response
*/
protected function createResponse()
{
// Set response headers.
$this->responseHeaders = '';
\curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, function ($_, $headers) {
$this->responseHeaders .= $headers;
return \strlen($headers);
});
// Get response data.
$body = \curl_exec($this->ch);
$code = \curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
$headers = $this->getResponseHeaders();
// Register response.
$this->response = new Response($code, $headers, $body);
return $this->getResponse();
}
/**
* Set default cURL settings.
*/
protected function setDefaultCurlSettings()
{
$verifySsl = $this->options->verifySsl();
$timeout = $this->options->getTimeout();
$followRedirects = $this->options->getFollowRedirects();
\curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, $verifySsl);
if (!$verifySsl) {
\curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, $verifySsl);
}
if ($followRedirects) {
\curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
}
\curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $timeout);
\curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
\curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->request->getRawHeaders());
\curl_setopt($this->ch, CURLOPT_URL, $this->request->getUrl());
foreach ($this->customCurlOptions as $customCurlOptionKey => $customCurlOptionValue) {
\curl_setopt($this->ch, $customCurlOptionKey, $customCurlOptionValue);
}
}
/**
* Look for errors in the request.
*
* @param array $parsedResponse Parsed body response.
*/
protected function lookForErrors($parsedResponse)
{
// Any non-200/201/202 response code indicates an error.
if (!\in_array($this->response->getCode(), ['200', '201', '202'])) {
$errors = isset($parsedResponse->errors) ? $parsedResponse->errors : $parsedResponse;
$errorMessage = '';
$errorCode = '';
if (is_array($errors)) {
$errorMessage = $errors[0]->message;
$errorCode = $errors[0]->code;
} elseif (isset($errors->message, $errors->code)) {
$errorMessage = $errors->message;
$errorCode = $errors->code;
}
throw new HttpClientException(
\sprintf('Error: %s [%s]', $errorMessage, $errorCode),
$this->response->getCode(),
$this->request,
$this->response
);
}
}
/**
* Process response.
*
* @return \stdClass
*/
protected function processResponse()
{
$body = $this->response->getBody();
// Look for UTF-8 BOM and remove.
if (0 === strpos(bin2hex(substr($body, 0, 4)), 'efbbbf')) {
$body = substr($body, 3);
}
$parsedResponse = \json_decode($body);
// Test if return a valid JSON.
if (JSON_ERROR_NONE !== json_last_error()) {
$message = function_exists('json_last_error_msg') ? json_last_error_msg() : 'Invalid JSON returned';
throw new HttpClientException(
sprintf('JSON ERROR: %s', $message),
$this->response->getCode(),
$this->request,
$this->response
);
}
$this->lookForErrors($parsedResponse);
return $parsedResponse;
}
/**
* Make requests.
*
* @param string $endpoint Request endpoint.
* @param string $method Request method.
* @param array $data Request data.
* @param array $parameters Request parameters.
*
* @return \stdClass
*/
public function request($endpoint, $method, $data = [], $parameters = [])
{
// Initialize cURL.
$this->ch = \curl_init();
// Set request args.
$request = $this->createRequest($endpoint, $method, $data, $parameters);
// Default cURL settings.
$this->setDefaultCurlSettings();
// Get response.
$response = $this->createResponse();
// Check for cURL errors.
if (\curl_errno($this->ch)) {
throw new HttpClientException('cURL Error: ' . \curl_error($this->ch), 0, $request, $response);
}
\curl_close($this->ch);
return $this->processResponse();
}
/**
* Get request data.
*
* @return Request
*/
public function getRequest()
{
return $this->request;
}
/**
* Get response data.
*
* @return Response
*/
public function getResponse()
{
return $this->response;
}
/**
* Set custom cURL options to use in requests.
*
* @param array $curlOptions
*/
public function setCustomCurlOptions(array $curlOptions)
{
$this->customCurlOptions = $curlOptions;
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* WooCommerce REST API HTTP Client Exception
*
* @category HttpClient
* @package Automattic/WooCommerce
*/
namespace Automattic\WooCommerce\HttpClient;
use Automattic\WooCommerce\HttpClient\Request;
use Automattic\WooCommerce\HttpClient\Response;
/**
* REST API HTTP Client Exception class.
*
* @package Automattic/WooCommerce
*/
class HttpClientException extends \Exception
{
/**
* Request.
*
* @var Request
*/
private $request;
/**
* Response.
*
* @var Response
*/
private $response;
/**
* Initialize exception.
*
* @param string $message Error message.
* @param int $code Error code.
* @param Request $request Request data.
* @param Response $response Response data.
*/
public function __construct($message, $code, Request $request, Response $response)
{
parent::__construct($message, $code);
$this->request = $request;
$this->response = $response;
}
/**
* Get request data.
*
* @return Request
*/
public function getRequest()
{
return $this->request;
}
/**
* Get response data.
*
* @return Response
*/
public function getResponse()
{
return $this->response;
}
}

View File

@@ -0,0 +1,268 @@
<?php
/**
* WooCommerce oAuth1.0
*
* @category HttpClient
* @package Automattic/WooCommerce
*/
namespace Automattic\WooCommerce\HttpClient;
/**
* oAuth1.0 class.
*
* @package Automattic/WooCommerce
*/
class OAuth
{
/**
* OAuth signature method algorithm.
*/
public const HASH_ALGORITHM = 'SHA256';
/**
* API endpoint URL.
*
* @var string
*/
protected $url;
/**
* Consumer key.
*
* @var string
*/
protected $consumerKey;
/**
* Consumer secret.
*
* @var string
*/
protected $consumerSecret;
/**
* API version.
*
* @var string
*/
protected $apiVersion;
/**
* Request method.
*
* @var string
*/
protected $method;
/**
* Request parameters.
*
* @var array
*/
protected $parameters;
/**
* Timestamp.
*
* @var string
*/
protected $timestamp;
/**
* Initialize oAuth class.
*
* @param string $url Store URL.
* @param string $consumerKey Consumer key.
* @param string $consumerSecret Consumer Secret.
* @param string $method Request method.
* @param string $apiVersion API version.
* @param array $parameters Request parameters.
* @param string $timestamp Timestamp.
*/
public function __construct(
$url,
$consumerKey,
$consumerSecret,
$apiVersion,
$method,
$parameters = [],
$timestamp = ''
) {
$this->url = $url;
$this->consumerKey = $consumerKey;
$this->consumerSecret = $consumerSecret;
$this->apiVersion = $apiVersion;
$this->method = $method;
$this->parameters = $parameters;
$this->timestamp = $timestamp;
}
/**
* Encode according to RFC 3986.
*
* @param string|array $value Value to be normalized.
*
* @return string
*/
protected function encode($value)
{
if (is_array($value)) {
return array_map([$this, 'encode'], $value);
} else {
return str_replace(['+', '%7E'], [' ', '~'], rawurlencode($value));
}
}
/**
* Normalize parameters.
*
* @param array $parameters Parameters to normalize.
*
* @return array
*/
protected function normalizeParameters($parameters)
{
$normalized = [];
foreach ($parameters as $key => $value) {
// Percent symbols (%) must be double-encoded.
$key = $this->encode($key);
$value = $this->encode($value);
$normalized[$key] = $value;
}
return $normalized;
}
/**
* Process filters.
*
* @param array $parameters Request parameters.
*
* @return array
*/
protected function processFilters($parameters)
{
if (isset($parameters['filter'])) {
$filters = $parameters['filter'];
unset($parameters['filter']);
foreach ($filters as $filter => $value) {
$parameters['filter[' . $filter . ']'] = $value;
}
}
return $parameters;
}
/**
* Get secret.
*
* @return string
*/
protected function getSecret()
{
$secret = $this->consumerSecret;
// Fix secret for v3 or later.
if (!\in_array($this->apiVersion, ['v1', 'v2'])) {
$secret .= '&';
}
return $secret;
}
/**
* Generate oAuth1.0 signature.
*
* @param array $parameters Request parameters including oauth.
*
* @return string
*/
protected function generateOauthSignature($parameters)
{
$baseRequestUri = \rawurlencode($this->url);
// Extract filters.
$parameters = $this->processFilters($parameters);
// Normalize parameter key/values and sort them.
$parameters = $this->normalizeParameters($parameters);
$parameters = $this->getSortedParameters($parameters);
// Set query string.
$queryString = \implode('%26', $this->joinWithEqualsSign($parameters)); // Join with ampersand.
$stringToSign = $this->method . '&' . $baseRequestUri . '&' . $queryString;
$secret = $this->getSecret();
return \base64_encode(\hash_hmac(self::HASH_ALGORITHM, $stringToSign, $secret, true));
}
/**
* Creates an array of urlencoded strings out of each array key/value pairs.
*
* @param array $params Array of parameters to convert.
* @param array $queryParams Array to extend.
* @param string $key Optional Array key to append
* @return string Array of urlencoded strings
*/
protected function joinWithEqualsSign($params, $queryParams = [], $key = '')
{
foreach ($params as $paramKey => $paramValue) {
if ($key) {
$paramKey = $key . '%5B' . $paramKey . '%5D'; // Handle multi-dimensional array.
}
if (is_array($paramValue)) {
$queryParams = $this->joinWithEqualsSign($paramValue, $queryParams, $paramKey);
} else {
$string = $paramKey . '=' . $paramValue; // Join with equals sign.
$queryParams[] = $this->encode($string);
}
}
return $queryParams;
}
/**
* Sort parameters.
*
* @param array $parameters Parameters to sort in byte-order.
*
* @return array
*/
protected function getSortedParameters($parameters)
{
\uksort($parameters, 'strcmp');
foreach ($parameters as $key => $value) {
if (\is_array($value)) {
\uksort($parameters[$key], 'strcmp');
}
}
return $parameters;
}
/**
* Get oAuth1.0 parameters.
*
* @return string
*/
public function getParameters()
{
$parameters = \array_merge($this->parameters, [
'oauth_consumer_key' => $this->consumerKey,
'oauth_timestamp' => $this->timestamp,
'oauth_nonce' => \sha1(\microtime()),
'oauth_signature_method' => 'HMAC-' . self::HASH_ALGORITHM,
]);
// The parameters above must be included in the signature generation.
$parameters['oauth_signature'] = $this->generateOauthSignature($parameters);
return $this->getSortedParameters($parameters);
}
}

View File

@@ -0,0 +1,182 @@
<?php
/**
* WooCommerce REST API HTTP Client Options
*
* @category HttpClient
* @package Automattic/WooCommerce
*/
namespace Automattic\WooCommerce\HttpClient;
/**
* REST API HTTP Client Options class.
*
* @package Automattic/WooCommerce
*/
class Options
{
/**
* Default WooCommerce REST API version.
*
* @var string
*/
public const VERSION = 'wc/v3';
/**
* Default request timeout.
*/
public const TIMEOUT = 15;
/**
* Default WP API prefix.
* Including leading and trailing slashes.
*/
public const WP_API_PREFIX = '/wp-json/';
/**
* Default User Agent.
* No version number.
*/
public const USER_AGENT = 'WooCommerce API Client-PHP';
/**
* Options.
*
* @var array
*/
private $options;
/**
* Initialize HTTP client options.
*
* @param array $options Client options.
*/
public function __construct($options)
{
$this->options = $options;
}
/**
* Get API version.
*
* @return string
*/
public function getVersion()
{
return isset($this->options['version']) ? $this->options['version'] : self::VERSION;
}
/**
* Check if need to verify SSL.
*
* @return bool
*/
public function verifySsl()
{
return isset($this->options['verify_ssl']) ? (bool) $this->options['verify_ssl'] : true;
}
/**
* Only use OAuth.
*
* @return bool
*/
public function isOAuthOnly()
{
return isset($this->options['oauth_only']) ? (bool) $this->options['oauth_only'] : false;
}
/**
* Get timeout.
*
* @return int
*/
public function getTimeout()
{
return isset($this->options['timeout']) ? (int) $this->options['timeout'] : self::TIMEOUT;
}
/**
* Basic Authentication as query string.
* Some old servers are not able to use CURLOPT_USERPWD.
*
* @return bool
*/
public function isQueryStringAuth()
{
return isset($this->options['query_string_auth']) ? (bool) $this->options['query_string_auth'] : false;
}
/**
* Check if is WP REST API.
*
* @return bool
*/
public function isWPAPI()
{
return isset($this->options['wp_api']) ? (bool) $this->options['wp_api'] : true;
}
/**
* Custom API Prefix for WP API.
*
* @return string
*/
public function apiPrefix()
{
return isset($this->options['wp_api_prefix']) ? $this->options['wp_api_prefix'] : self::WP_API_PREFIX;
}
/**
* oAuth timestamp.
*
* @return string
*/
public function oauthTimestamp()
{
return isset($this->options['oauth_timestamp']) ? $this->options['oauth_timestamp'] : \time();
}
/**
* Custom user agent.
*
* @return string
*/
public function userAgent()
{
return isset($this->options['user_agent']) ? $this->options['user_agent'] : self::USER_AGENT;
}
/**
* Get follow redirects.
*
* @return bool
*/
public function getFollowRedirects()
{
return isset($this->options['follow_redirects']) ? (bool) $this->options['follow_redirects'] : false;
}
/**
* Check is it needed to mask all non-GET/POST methods (PUT/DELETE/etc.) by using POST method with added
* query parameter ?_method=METHOD into URL.
*
* @return bool
*/
public function isMethodOverrideQuery()
{
return isset($this->options['method_override_query']) && $this->options['method_override_query'];
}
/**
* Check is it needed to mask all non-GET/POST methods (PUT/DELETE/etc.) by using POST method with added
* "X-HTTP-Method-Override: METHOD" HTTP header into request.
*
* @return bool
*/
public function isMethodOverrideHeader()
{
return isset($this->options['method_override_header']) && $this->options['method_override_header'];
}
}

View File

@@ -0,0 +1,187 @@
<?php
/**
* WooCommerce REST API HTTP Client Request
*
* @category HttpClient
* @package Automattic/WooCommerce
*/
namespace Automattic\WooCommerce\HttpClient;
/**
* REST API HTTP Client Request class.
*
* @package Automattic/WooCommerce
*/
class Request
{
/**
* Request url.
*
* @var string
*/
private $url;
/**
* Request method.
*
* @var string
*/
private $method;
/**
* Request paramenters.
*
* @var array
*/
private $parameters;
/**
* Request headers.
*
* @var array
*/
private $headers;
/**
* Request body.
*
* @var string
*/
private $body;
/**
* Initialize request.
*
* @param string $url Request url.
* @param string $method Request method.
* @param array $parameters Request paramenters.
* @param array $headers Request headers.
* @param string $body Request body.
*/
public function __construct($url = '', $method = 'POST', $parameters = [], $headers = [], $body = '')
{
$this->url = $url;
$this->method = $method;
$this->parameters = $parameters;
$this->headers = $headers;
$this->body = $body;
}
/**
* Set url.
*
* @param string $url Request url.
*/
public function setUrl($url)
{
$this->url = $url;
}
/**
* Set method.
*
* @param string $method Request method.
*/
public function setMethod($method)
{
$this->method = $method;
}
/**
* Set parameters.
*
* @param array $parameters Request paramenters.
*/
public function setParameters($parameters)
{
$this->parameters = $parameters;
}
/**
* Set headers.
*
* @param array $headers Request headers.
*/
public function setHeaders($headers)
{
$this->headers = $headers;
}
/**
* Set body.
*
* @param string $body Request body.
*/
public function setBody($body)
{
$this->body = $body;
}
/**
* Get url.
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Get method.
*
* @return string
*/
public function getMethod()
{
return $this->method;
}
/**
* Get parameters.
*
* @return array
*/
public function getParameters()
{
return $this->parameters;
}
/**
* Get headers.
*
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Get raw headers.
*
* @return array
*/
public function getRawHeaders()
{
$headers = [];
foreach ($this->headers as $key => $value) {
$headers[] = $key . ': ' . $value;
}
return $headers;
}
/**
* Get body.
*
* @return string
*/
public function getBody()
{
return $this->body;
}
}

View File

@@ -0,0 +1,127 @@
<?php
/**
* WooCommerce REST API HTTP Client Response
*
* @category HttpClient
* @package Automattic/WooCommerce
*/
namespace Automattic\WooCommerce\HttpClient;
/**
* REST API HTTP Client Response class.
*
* @package Automattic/WooCommerce
*/
class Response
{
/**
* Response code.
*
* @var int
*/
private $code;
/**
* Response headers.
*
* @var array
*/
private $headers;
/**
* Response body.
*
* @var string
*/
private $body;
/**
* Initialize response.
*
* @param int $code Response code.
* @param array $headers Response headers.
* @param string $body Response body.
*/
public function __construct($code = 0, $headers = [], $body = '')
{
$this->code = $code;
$this->headers = $headers;
$this->body = $body;
}
/**
* To string.
*
* @return string
*/
public function __toString()
{
return \json_encode([
'code' => $this->code,
'headers' => $this->headers,
'body' => $this->body,
]);
}
/**
* Set code.
*
* @param int $code Response code.
*/
public function setCode($code)
{
$this->code = (int) $code;
}
/**
* Set headers.
*
* @param array $headers Response headers.
*/
public function setHeaders($headers)
{
$this->headers = $headers;
}
/**
* Set body.
*
* @param string $body Response body.
*/
public function setBody($body)
{
$this->body = $body;
}
/**
* Get code.
*
* @return int
*/
public function getCode()
{
return $this->code;
}
/**
* Get headers.
*
* @return array $headers Response headers.
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Get body.
*
* @return string $body Response body.
*/
public function getBody()
{
return $this->body;
}
}

117
vendor/bin/carbon vendored Executable file
View File

@@ -0,0 +1,117 @@
#!/usr/bin/env php
<?php
/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../nesbot/carbon/bin/carbon)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/
namespace Composer;
$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';
if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;
public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = $this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;
return (bool) $this->handle;
}
public function stream_read($count)
{
$data = fread($this->handle, $count);
if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}
$this->position += strlen($data);
return $data;
}
public function stream_cast($castAs)
{
return $this->handle;
}
public function stream_close()
{
fclose($this->handle);
}
public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}
public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}
return false;
}
public function stream_tell()
{
return $this->position;
}
public function stream_eof()
{
return feof($this->handle);
}
public function stream_stat()
{
return array();
}
public function stream_set_option($option, $arg1, $arg2)
{
return true;
}
public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}
return false;
}
}
}
if (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/nesbot/carbon/bin/carbon');
exit(0);
}
}
include __DIR__ . '/..'.'/nesbot/carbon/bin/carbon';

117
vendor/bin/commonmark vendored Executable file
View File

@@ -0,0 +1,117 @@
#!/usr/bin/env php
<?php
/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../league/commonmark/bin/commonmark)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/
namespace Composer;
$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';
if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;
public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = $this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;
return (bool) $this->handle;
}
public function stream_read($count)
{
$data = fread($this->handle, $count);
if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}
$this->position += strlen($data);
return $data;
}
public function stream_cast($castAs)
{
return $this->handle;
}
public function stream_close()
{
fclose($this->handle);
}
public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}
public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}
return false;
}
public function stream_tell()
{
return $this->position;
}
public function stream_eof()
{
return feof($this->handle);
}
public function stream_stat()
{
return array();
}
public function stream_set_option($option, $arg1, $arg2)
{
return true;
}
public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}
return false;
}
}
}
if (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/league/commonmark/bin/commonmark');
exit(0);
}
}
include __DIR__ . '/..'.'/league/commonmark/bin/commonmark';

117
vendor/bin/patch-type-declarations vendored Executable file
View File

@@ -0,0 +1,117 @@
#!/usr/bin/env php
<?php
/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../symfony/error-handler/Resources/bin/patch-type-declarations)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/
namespace Composer;
$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';
if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;
public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = $this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;
return (bool) $this->handle;
}
public function stream_read($count)
{
$data = fread($this->handle, $count);
if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}
$this->position += strlen($data);
return $data;
}
public function stream_cast($castAs)
{
return $this->handle;
}
public function stream_close()
{
fclose($this->handle);
}
public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}
public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}
return false;
}
public function stream_tell()
{
return $this->position;
}
public function stream_eof()
{
return feof($this->handle);
}
public function stream_stat()
{
return array();
}
public function stream_set_option($option, $arg1, $arg2)
{
return true;
}
public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}
return false;
}
}
}
if (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/symfony/error-handler/Resources/bin/patch-type-declarations');
exit(0);
}
}
include __DIR__ . '/..'.'/symfony/error-handler/Resources/bin/patch-type-declarations';

117
vendor/bin/php-parse vendored Executable file
View File

@@ -0,0 +1,117 @@
#!/usr/bin/env php
<?php
/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../nikic/php-parser/bin/php-parse)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/
namespace Composer;
$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';
if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;
public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = $this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;
return (bool) $this->handle;
}
public function stream_read($count)
{
$data = fread($this->handle, $count);
if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}
$this->position += strlen($data);
return $data;
}
public function stream_cast($castAs)
{
return $this->handle;
}
public function stream_close()
{
fclose($this->handle);
}
public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}
public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}
return false;
}
public function stream_tell()
{
return $this->position;
}
public function stream_eof()
{
return feof($this->handle);
}
public function stream_stat()
{
return array();
}
public function stream_set_option($option, $arg1, $arg2)
{
return true;
}
public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}
return false;
}
}
}
if (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/nikic/php-parser/bin/php-parse');
exit(0);
}
}
include __DIR__ . '/..'.'/nikic/php-parser/bin/php-parse';

120
vendor/bin/phpunit vendored Executable file
View File

@@ -0,0 +1,120 @@
#!/usr/bin/env php
<?php
/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../phpunit/phpunit/phpunit)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/
namespace Composer;
$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';
$GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'] = $GLOBALS['__PHPUNIT_ISOLATION_BLACKLIST'] = array(realpath(__DIR__ . '/..'.'/phpunit/phpunit/phpunit'));
if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;
public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = 'phpvfscomposer://'.$this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;
return (bool) $this->handle;
}
public function stream_read($count)
{
$data = fread($this->handle, $count);
if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}
$data = str_replace('__DIR__', var_export(dirname($this->realpath), true), $data);
$data = str_replace('__FILE__', var_export($this->realpath, true), $data);
$this->position += strlen($data);
return $data;
}
public function stream_cast($castAs)
{
return $this->handle;
}
public function stream_close()
{
fclose($this->handle);
}
public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}
public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}
return false;
}
public function stream_tell()
{
return $this->position;
}
public function stream_eof()
{
return feof($this->handle);
}
public function stream_stat()
{
return array();
}
public function stream_set_option($option, $arg1, $arg2)
{
return true;
}
public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}
return false;
}
}
}
if (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/phpunit/phpunit/phpunit');
exit(0);
}
}
include __DIR__ . '/..'.'/phpunit/phpunit/phpunit';

117
vendor/bin/psysh vendored Executable file
View File

@@ -0,0 +1,117 @@
#!/usr/bin/env php
<?php
/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../psy/psysh/bin/psysh)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/
namespace Composer;
$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';
if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;
public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = $this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;
return (bool) $this->handle;
}
public function stream_read($count)
{
$data = fread($this->handle, $count);
if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}
$this->position += strlen($data);
return $data;
}
public function stream_cast($castAs)
{
return $this->handle;
}
public function stream_close()
{
fclose($this->handle);
}
public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}
public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}
return false;
}
public function stream_tell()
{
return $this->position;
}
public function stream_eof()
{
return feof($this->handle);
}
public function stream_stat()
{
return array();
}
public function stream_set_option($option, $arg1, $arg2)
{
return true;
}
public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}
return false;
}
}
}
if (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/psy/psysh/bin/psysh');
exit(0);
}
}
include __DIR__ . '/..'.'/psy/psysh/bin/psysh';

117
vendor/bin/var-dump-server vendored Executable file
View File

@@ -0,0 +1,117 @@
#!/usr/bin/env php
<?php
/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../symfony/var-dumper/Resources/bin/var-dump-server)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/
namespace Composer;
$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';
if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;
public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = $this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;
return (bool) $this->handle;
}
public function stream_read($count)
{
$data = fread($this->handle, $count);
if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}
$this->position += strlen($data);
return $data;
}
public function stream_cast($castAs)
{
return $this->handle;
}
public function stream_close()
{
fclose($this->handle);
}
public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}
public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}
return false;
}
public function stream_tell()
{
return $this->position;
}
public function stream_eof()
{
return feof($this->handle);
}
public function stream_stat()
{
return array();
}
public function stream_set_option($option, $arg1, $arg2)
{
return true;
}
public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}
return false;
}
}
}
if (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/symfony/var-dumper/Resources/bin/var-dump-server');
exit(0);
}
}
include __DIR__ . '/..'.'/symfony/var-dumper/Resources/bin/var-dump-server';

415
vendor/brick/math/CHANGELOG.md vendored Normal file
View File

@@ -0,0 +1,415 @@
# Changelog
All notable changes to this project will be documented in this file.
## [0.9.3](https://github.com/brick/math/releases/tag/0.9.3) - 2021-08-15
🚀 **Compatibility with PHP 8.1**
- Support for custom object serialization; this removes a warning on PHP 8.1 due to the `Serializable` interface being deprecated (thanks @TRowbotham)
## [0.9.2](https://github.com/brick/math/releases/tag/0.9.2) - 2021-01-20
🐛 **Bug fix**
- Incorrect results could be returned when using the BCMath calculator, with a default scale set with `bcscale()`, on PHP >= 7.2 (#55).
## [0.9.1](https://github.com/brick/math/releases/tag/0.9.1) - 2020-08-19
✨ New features
- `BigInteger::not()` returns the bitwise `NOT` value
🐛 **Bug fixes**
- `BigInteger::toBytes()` could return an incorrect binary representation for some numbers
- The bitwise operations `and()`, `or()`, `xor()` on `BigInteger` could return an incorrect result when the GMP extension is not available
## [0.9.0](https://github.com/brick/math/releases/tag/0.9.0) - 2020-08-18
👌 **Improvements**
- `BigNumber::of()` now accepts `.123` and `123.` formats, both of which return a `BigDecimal`
💥 **Breaking changes**
- Deprecated method `BigInteger::powerMod()` has been removed - use `modPow()` instead
- Deprecated method `BigInteger::parse()` has been removed - use `fromBase()` instead
## [0.8.17](https://github.com/brick/math/releases/tag/0.8.17) - 2020-08-19
🐛 **Bug fix**
- `BigInteger::toBytes()` could return an incorrect binary representation for some numbers
- The bitwise operations `and()`, `or()`, `xor()` on `BigInteger` could return an incorrect result when the GMP extension is not available
## [0.8.16](https://github.com/brick/math/releases/tag/0.8.16) - 2020-08-18
🚑 **Critical fix**
- This version reintroduces the deprecated `BigInteger::parse()` method, that has been removed by mistake in version `0.8.9` and should have lasted for the whole `0.8` release cycle.
**New features**
- `BigInteger::modInverse()` calculates a modular multiplicative inverse
- `BigInteger::fromBytes()` creates a `BigInteger` from a byte string
- `BigInteger::toBytes()` converts a `BigInteger` to a byte string
- `BigInteger::randomBits()` creates a pseudo-random `BigInteger` of a given bit length
- `BigInteger::randomRange()` creates a pseudo-random `BigInteger` between two bounds
💩 **Deprecations**
- `BigInteger::powerMod()` is now deprecated in favour of `modPow()`
## [0.8.15](https://github.com/brick/math/releases/tag/0.8.15) - 2020-04-15
🐛 **Fixes**
- added missing `ext-json` requirement, due to `BigNumber` implementing `JsonSerializable`
⚡️ **Optimizations**
- additional optimization in `BigInteger::remainder()`
## [0.8.14](https://github.com/brick/math/releases/tag/0.8.14) - 2020-02-18
**New features**
- `BigInteger::getLowestSetBit()` returns the index of the rightmost one bit
## [0.8.13](https://github.com/brick/math/releases/tag/0.8.13) - 2020-02-16
**New features**
- `BigInteger::isEven()` tests whether the number is even
- `BigInteger::isOdd()` tests whether the number is odd
- `BigInteger::testBit()` tests if a bit is set
- `BigInteger::getBitLength()` returns the number of bits in the minimal representation of the number
## [0.8.12](https://github.com/brick/math/releases/tag/0.8.12) - 2020-02-03
🛠️ **Maintenance release**
Classes are now annotated for better static analysis with [psalm](https://psalm.dev/).
This is a maintenance release: no bug fixes, no new features, no breaking changes.
## [0.8.11](https://github.com/brick/math/releases/tag/0.8.11) - 2020-01-23
**New feature**
`BigInteger::powerMod()` performs a power-with-modulo operation. Useful for crypto.
## [0.8.10](https://github.com/brick/math/releases/tag/0.8.10) - 2020-01-21
**New feature**
`BigInteger::mod()` returns the **modulo** of two numbers. The *modulo* differs from the *remainder* when the signs of the operands are different.
## [0.8.9](https://github.com/brick/math/releases/tag/0.8.9) - 2020-01-08
⚡️ **Performance improvements**
A few additional optimizations in `BigInteger` and `BigDecimal` when one of the operands can be returned as is. Thanks to @tomtomsen in #24.
## [0.8.8](https://github.com/brick/math/releases/tag/0.8.8) - 2019-04-25
🐛 **Bug fixes**
- `BigInteger::toBase()` could return an empty string for zero values (BCMath & Native calculators only, GMP calculator unaffected)
**New features**
- `BigInteger::toArbitraryBase()` converts a number to an arbitrary base, using a custom alphabet
- `BigInteger::fromArbitraryBase()` converts a string in an arbitrary base, using a custom alphabet, back to a number
These methods can be used as the foundation to convert strings between different bases/alphabets, using BigInteger as an intermediate representation.
💩 **Deprecations**
- `BigInteger::parse()` is now deprecated in favour of `fromBase()`
`BigInteger::fromBase()` works the same way as `parse()`, with 2 minor differences:
- the `$base` parameter is required, it does not default to `10`
- it throws a `NumberFormatException` instead of an `InvalidArgumentException` when the number is malformed
## [0.8.7](https://github.com/brick/math/releases/tag/0.8.7) - 2019-04-20
**Improvements**
- Safer conversion from `float` when using custom locales
- **Much faster** `NativeCalculator` implementation 🚀
You can expect **at least a 3x performance improvement** for common arithmetic operations when using the library on systems without GMP or BCMath; it gets exponentially faster on multiplications with a high number of digits. This is due to calculations now being performed on whole blocks of digits (the block size depending on the platform, 32-bit or 64-bit) instead of digit-by-digit as before.
## [0.8.6](https://github.com/brick/math/releases/tag/0.8.6) - 2019-04-11
**New method**
`BigNumber::sum()` returns the sum of one or more numbers.
## [0.8.5](https://github.com/brick/math/releases/tag/0.8.5) - 2019-02-12
**Bug fix**: `of()` factory methods could fail when passing a `float` in environments using a `LC_NUMERIC` locale with a decimal separator other than `'.'` (#20).
Thanks @manowark 👍
## [0.8.4](https://github.com/brick/math/releases/tag/0.8.4) - 2018-12-07
**New method**
`BigDecimal::sqrt()` calculates the square root of a decimal number, to a given scale.
## [0.8.3](https://github.com/brick/math/releases/tag/0.8.3) - 2018-12-06
**New method**
`BigInteger::sqrt()` calculates the square root of a number (thanks @peter279k).
**New exception**
`NegativeNumberException` is thrown when calling `sqrt()` on a negative number.
## [0.8.2](https://github.com/brick/math/releases/tag/0.8.2) - 2018-11-08
**Performance update**
- Further improvement of `toInt()` performance
- `NativeCalculator` can now perform some multiplications more efficiently
## [0.8.1](https://github.com/brick/math/releases/tag/0.8.1) - 2018-11-07
Performance optimization of `toInt()` methods.
## [0.8.0](https://github.com/brick/math/releases/tag/0.8.0) - 2018-10-13
**Breaking changes**
The following deprecated methods have been removed. Use the new method name instead:
| Method removed | Replacement method |
| --- | --- |
| `BigDecimal::getIntegral()` | `BigDecimal::getIntegralPart()` |
| `BigDecimal::getFraction()` | `BigDecimal::getFractionalPart()` |
---
**New features**
`BigInteger` has been augmented with 5 new methods for bitwise operations:
| New method | Description |
| --- | --- |
| `and()` | performs a bitwise `AND` operation on two numbers |
| `or()` | performs a bitwise `OR` operation on two numbers |
| `xor()` | performs a bitwise `XOR` operation on two numbers |
| `shiftedLeft()` | returns the number shifted left by a number of bits |
| `shiftedRight()` | returns the number shifted right by a number of bits |
Thanks to @DASPRiD 👍
## [0.7.3](https://github.com/brick/math/releases/tag/0.7.3) - 2018-08-20
**New method:** `BigDecimal::hasNonZeroFractionalPart()`
**Renamed/deprecated methods:**
- `BigDecimal::getIntegral()` has been renamed to `getIntegralPart()` and is now deprecated
- `BigDecimal::getFraction()` has been renamed to `getFractionalPart()` and is now deprecated
## [0.7.2](https://github.com/brick/math/releases/tag/0.7.2) - 2018-07-21
**Performance update**
`BigInteger::parse()` and `toBase()` now use GMP's built-in base conversion features when available.
## [0.7.1](https://github.com/brick/math/releases/tag/0.7.1) - 2018-03-01
This is a maintenance release, no code has been changed.
- When installed with `--no-dev`, the autoloader does not autoload tests anymore
- Tests and other files unnecessary for production are excluded from the dist package
This will help make installations more compact.
## [0.7.0](https://github.com/brick/math/releases/tag/0.7.0) - 2017-10-02
Methods renamed:
- `BigNumber:sign()` has been renamed to `getSign()`
- `BigDecimal::unscaledValue()` has been renamed to `getUnscaledValue()`
- `BigDecimal::scale()` has been renamed to `getScale()`
- `BigDecimal::integral()` has been renamed to `getIntegral()`
- `BigDecimal::fraction()` has been renamed to `getFraction()`
- `BigRational::numerator()` has been renamed to `getNumerator()`
- `BigRational::denominator()` has been renamed to `getDenominator()`
Classes renamed:
- `ArithmeticException` has been renamed to `MathException`
## [0.6.2](https://github.com/brick/math/releases/tag/0.6.2) - 2017-10-02
The base class for all exceptions is now `MathException`.
`ArithmeticException` has been deprecated, and will be removed in 0.7.0.
## [0.6.1](https://github.com/brick/math/releases/tag/0.6.1) - 2017-10-02
A number of methods have been renamed:
- `BigNumber:sign()` is deprecated; use `getSign()` instead
- `BigDecimal::unscaledValue()` is deprecated; use `getUnscaledValue()` instead
- `BigDecimal::scale()` is deprecated; use `getScale()` instead
- `BigDecimal::integral()` is deprecated; use `getIntegral()` instead
- `BigDecimal::fraction()` is deprecated; use `getFraction()` instead
- `BigRational::numerator()` is deprecated; use `getNumerator()` instead
- `BigRational::denominator()` is deprecated; use `getDenominator()` instead
The old methods will be removed in version 0.7.0.
## [0.6.0](https://github.com/brick/math/releases/tag/0.6.0) - 2017-08-25
- Minimum PHP version is now [7.1](https://gophp71.org/); for PHP 5.6 and PHP 7.0 support, use version `0.5`
- Deprecated method `BigDecimal::withScale()` has been removed; use `toScale()` instead
- Method `BigNumber::toInteger()` has been renamed to `toInt()`
## [0.5.4](https://github.com/brick/math/releases/tag/0.5.4) - 2016-10-17
`BigNumber` classes now implement [JsonSerializable](http://php.net/manual/en/class.jsonserializable.php).
The JSON output is always a string.
## [0.5.3](https://github.com/brick/math/releases/tag/0.5.3) - 2016-03-31
This is a bugfix release. Dividing by a negative power of 1 with the same scale as the dividend could trigger an incorrect optimization which resulted in a wrong result. See #6.
## [0.5.2](https://github.com/brick/math/releases/tag/0.5.2) - 2015-08-06
The `$scale` parameter of `BigDecimal::dividedBy()` is now optional again.
## [0.5.1](https://github.com/brick/math/releases/tag/0.5.1) - 2015-07-05
**New method: `BigNumber::toScale()`**
This allows to convert any `BigNumber` to a `BigDecimal` with a given scale, using rounding if necessary.
## [0.5.0](https://github.com/brick/math/releases/tag/0.5.0) - 2015-07-04
**New features**
- Common `BigNumber` interface for all classes, with the following methods:
- `sign()` and derived methods (`isZero()`, `isPositive()`, ...)
- `compareTo()` and derived methods (`isEqualTo()`, `isGreaterThan()`, ...) that work across different `BigNumber` types
- `toBigInteger()`, `toBigDecimal()`, `toBigRational`() conversion methods
- `toInteger()` and `toFloat()` conversion methods to native types
- Unified `of()` behaviour: every class now accepts any type of number, provided that it can be safely converted to the current type
- New method: `BigDecimal::exactlyDividedBy()`; this method automatically computes the scale of the result, provided that the division yields a finite number of digits
- New methods: `BigRational::quotient()` and `remainder()`
- Fine-grained exceptions: `DivisionByZeroException`, `RoundingNecessaryException`, `NumberFormatException`
- Factory methods `zero()`, `one()` and `ten()` available in all classes
- Rounding mode reintroduced in `BigInteger::dividedBy()`
This release also comes with many performance improvements.
---
**Breaking changes**
- `BigInteger`:
- `getSign()` is renamed to `sign()`
- `toString()` is renamed to `toBase()`
- `BigInteger::dividedBy()` now throws an exception by default if the remainder is not zero; use `quotient()` to get the previous behaviour
- `BigDecimal`:
- `getSign()` is renamed to `sign()`
- `getUnscaledValue()` is renamed to `unscaledValue()`
- `getScale()` is renamed to `scale()`
- `getIntegral()` is renamed to `integral()`
- `getFraction()` is renamed to `fraction()`
- `divideAndRemainder()` is renamed to `quotientAndRemainder()`
- `dividedBy()` now takes a **mandatory** `$scale` parameter **before** the rounding mode
- `toBigInteger()` does not accept a `$roundingMode` parameter any more
- `toBigRational()` does not simplify the fraction any more; explicitly add `->simplified()` to get the previous behaviour
- `BigRational`:
- `getSign()` is renamed to `sign()`
- `getNumerator()` is renamed to `numerator()`
- `getDenominator()` is renamed to `denominator()`
- `of()` is renamed to `nd()`, while `parse()` is renamed to `of()`
- Miscellaneous:
- `ArithmeticException` is moved to an `Exception\` sub-namespace
- `of()` factory methods now throw `NumberFormatException` instead of `InvalidArgumentException`
## [0.4.3](https://github.com/brick/math/releases/tag/0.4.3) - 2016-03-31
Backport of two bug fixes from the 0.5 branch:
- `BigInteger::parse()` did not always throw `InvalidArgumentException` as expected
- Dividing by a negative power of 1 with the same scale as the dividend could trigger an incorrect optimization which resulted in a wrong result. See #6.
## [0.4.2](https://github.com/brick/math/releases/tag/0.4.2) - 2015-06-16
New method: `BigDecimal::stripTrailingZeros()`
## [0.4.1](https://github.com/brick/math/releases/tag/0.4.1) - 2015-06-12
Introducing a `BigRational` class, to perform calculations on fractions of any size.
## [0.4.0](https://github.com/brick/math/releases/tag/0.4.0) - 2015-06-12
Rounding modes have been removed from `BigInteger`, and are now a concept specific to `BigDecimal`.
`BigInteger::dividedBy()` now always returns the quotient of the division.
## [0.3.5](https://github.com/brick/math/releases/tag/0.3.5) - 2016-03-31
Backport of two bug fixes from the 0.5 branch:
- `BigInteger::parse()` did not always throw `InvalidArgumentException` as expected
- Dividing by a negative power of 1 with the same scale as the dividend could trigger an incorrect optimization which resulted in a wrong result. See #6.
## [0.3.4](https://github.com/brick/math/releases/tag/0.3.4) - 2015-06-11
New methods:
- `BigInteger::remainder()` returns the remainder of a division only
- `BigInteger::gcd()` returns the greatest common divisor of two numbers
## [0.3.3](https://github.com/brick/math/releases/tag/0.3.3) - 2015-06-07
Fix `toString()` not handling negative numbers.
## [0.3.2](https://github.com/brick/math/releases/tag/0.3.2) - 2015-06-07
`BigInteger` and `BigDecimal` now have a `getSign()` method that returns:
- `-1` if the number is negative
- `0` if the number is zero
- `1` if the number is positive
## [0.3.1](https://github.com/brick/math/releases/tag/0.3.1) - 2015-06-05
Minor performance improvements
## [0.3.0](https://github.com/brick/math/releases/tag/0.3.0) - 2015-06-04
The `$roundingMode` and `$scale` parameters have been swapped in `BigDecimal::dividedBy()`.
## [0.2.2](https://github.com/brick/math/releases/tag/0.2.2) - 2015-06-04
Stronger immutability guarantee for `BigInteger` and `BigDecimal`.
So far, it would have been possible to break immutability of these classes by calling the `unserialize()` internal function. This release fixes that.
## [0.2.1](https://github.com/brick/math/releases/tag/0.2.1) - 2015-06-02
Added `BigDecimal::divideAndRemainder()`
## [0.2.0](https://github.com/brick/math/releases/tag/0.2.0) - 2015-05-22
- `min()` and `max()` do not accept an `array` any more, but a variable number of parameters
- **minimum PHP version is now 5.6**
- continuous integration with PHP 7
## [0.1.1](https://github.com/brick/math/releases/tag/0.1.1) - 2014-09-01
- Added `BigInteger::power()`
- Added HHVM support
## [0.1.0](https://github.com/brick/math/releases/tag/0.1.0) - 2014-08-31
First beta release.

20
vendor/brick/math/LICENSE vendored Normal file
View File

@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2013-present Benjamin Morel
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

17
vendor/brick/math/SECURITY.md vendored Normal file
View File

@@ -0,0 +1,17 @@
# Security Policy
## Supported Versions
Only the last two release streams are supported.
| Version | Supported |
| ------- | ------------------ |
| 0.9.x | :white_check_mark: |
| 0.8.x | :white_check_mark: |
| < 0.8 | :x: |
## Reporting a Vulnerability
To report a security vulnerability, please use the
[Tidelift security contact](https://tidelift.com/security).
Tidelift will coordinate the fix and disclosure.

35
vendor/brick/math/composer.json vendored Normal file
View File

@@ -0,0 +1,35 @@
{
"name": "brick/math",
"description": "Arbitrary-precision arithmetic library",
"type": "library",
"keywords": [
"Brick",
"Math",
"Arbitrary-precision",
"Arithmetic",
"BigInteger",
"BigDecimal",
"BigRational",
"Bignum"
],
"license": "MIT",
"require": {
"php": "^7.1 || ^8.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.5.15 || ^8.5 || ^9.0",
"php-coveralls/php-coveralls": "^2.2",
"vimeo/psalm": "4.9.2"
},
"autoload": {
"psr-4": {
"Brick\\Math\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Brick\\Math\\Tests\\": "tests/"
}
}
}

895
vendor/brick/math/src/BigDecimal.php vendored Normal file
View File

@@ -0,0 +1,895 @@
<?php
declare(strict_types=1);
namespace Brick\Math;
use Brick\Math\Exception\DivisionByZeroException;
use Brick\Math\Exception\MathException;
use Brick\Math\Exception\NegativeNumberException;
use Brick\Math\Internal\Calculator;
/**
* Immutable, arbitrary-precision signed decimal numbers.
*
* @psalm-immutable
*/
final class BigDecimal extends BigNumber
{
/**
* The unscaled value of this decimal number.
*
* This is a string of digits with an optional leading minus sign.
* No leading zero must be present.
* No leading minus sign must be present if the value is 0.
*
* @var string
*/
private $value;
/**
* The scale (number of digits after the decimal point) of this decimal number.
*
* This must be zero or more.
*
* @var int
*/
private $scale;
/**
* Protected constructor. Use a factory method to obtain an instance.
*
* @param string $value The unscaled value, validated.
* @param int $scale The scale, validated.
*/
protected function __construct(string $value, int $scale = 0)
{
$this->value = $value;
$this->scale = $scale;
}
/**
* Creates a BigDecimal of the given value.
*
* @param BigNumber|int|float|string $value
*
* @return BigDecimal
*
* @throws MathException If the value cannot be converted to a BigDecimal.
*
* @psalm-pure
*/
public static function of($value) : BigNumber
{
return parent::of($value)->toBigDecimal();
}
/**
* Creates a BigDecimal from an unscaled value and a scale.
*
* Example: `(12345, 3)` will result in the BigDecimal `12.345`.
*
* @param BigNumber|int|float|string $value The unscaled value. Must be convertible to a BigInteger.
* @param int $scale The scale of the number, positive or zero.
*
* @return BigDecimal
*
* @throws \InvalidArgumentException If the scale is negative.
*
* @psalm-pure
*/
public static function ofUnscaledValue($value, int $scale = 0) : BigDecimal
{
if ($scale < 0) {
throw new \InvalidArgumentException('The scale cannot be negative.');
}
return new BigDecimal((string) BigInteger::of($value), $scale);
}
/**
* Returns a BigDecimal representing zero, with a scale of zero.
*
* @return BigDecimal
*
* @psalm-pure
*/
public static function zero() : BigDecimal
{
/**
* @psalm-suppress ImpureStaticVariable
* @var BigDecimal|null $zero
*/
static $zero;
if ($zero === null) {
$zero = new BigDecimal('0');
}
return $zero;
}
/**
* Returns a BigDecimal representing one, with a scale of zero.
*
* @return BigDecimal
*
* @psalm-pure
*/
public static function one() : BigDecimal
{
/**
* @psalm-suppress ImpureStaticVariable
* @var BigDecimal|null $one
*/
static $one;
if ($one === null) {
$one = new BigDecimal('1');
}
return $one;
}
/**
* Returns a BigDecimal representing ten, with a scale of zero.
*
* @return BigDecimal
*
* @psalm-pure
*/
public static function ten() : BigDecimal
{
/**
* @psalm-suppress ImpureStaticVariable
* @var BigDecimal|null $ten
*/
static $ten;
if ($ten === null) {
$ten = new BigDecimal('10');
}
return $ten;
}
/**
* Returns the sum of this number and the given one.
*
* The result has a scale of `max($this->scale, $that->scale)`.
*
* @param BigNumber|int|float|string $that The number to add. Must be convertible to a BigDecimal.
*
* @return BigDecimal The result.
*
* @throws MathException If the number is not valid, or is not convertible to a BigDecimal.
*/
public function plus($that) : BigDecimal
{
$that = BigDecimal::of($that);
if ($that->value === '0' && $that->scale <= $this->scale) {
return $this;
}
if ($this->value === '0' && $this->scale <= $that->scale) {
return $that;
}
[$a, $b] = $this->scaleValues($this, $that);
$value = Calculator::get()->add($a, $b);
$scale = $this->scale > $that->scale ? $this->scale : $that->scale;
return new BigDecimal($value, $scale);
}
/**
* Returns the difference of this number and the given one.
*
* The result has a scale of `max($this->scale, $that->scale)`.
*
* @param BigNumber|int|float|string $that The number to subtract. Must be convertible to a BigDecimal.
*
* @return BigDecimal The result.
*
* @throws MathException If the number is not valid, or is not convertible to a BigDecimal.
*/
public function minus($that) : BigDecimal
{
$that = BigDecimal::of($that);
if ($that->value === '0' && $that->scale <= $this->scale) {
return $this;
}
[$a, $b] = $this->scaleValues($this, $that);
$value = Calculator::get()->sub($a, $b);
$scale = $this->scale > $that->scale ? $this->scale : $that->scale;
return new BigDecimal($value, $scale);
}
/**
* Returns the product of this number and the given one.
*
* The result has a scale of `$this->scale + $that->scale`.
*
* @param BigNumber|int|float|string $that The multiplier. Must be convertible to a BigDecimal.
*
* @return BigDecimal The result.
*
* @throws MathException If the multiplier is not a valid number, or is not convertible to a BigDecimal.
*/
public function multipliedBy($that) : BigDecimal
{
$that = BigDecimal::of($that);
if ($that->value === '1' && $that->scale === 0) {
return $this;
}
if ($this->value === '1' && $this->scale === 0) {
return $that;
}
$value = Calculator::get()->mul($this->value, $that->value);
$scale = $this->scale + $that->scale;
return new BigDecimal($value, $scale);
}
/**
* Returns the result of the division of this number by the given one, at the given scale.
*
* @param BigNumber|int|float|string $that The divisor.
* @param int|null $scale The desired scale, or null to use the scale of this number.
* @param int $roundingMode An optional rounding mode.
*
* @return BigDecimal
*
* @throws \InvalidArgumentException If the scale or rounding mode is invalid.
* @throws MathException If the number is invalid, is zero, or rounding was necessary.
*/
public function dividedBy($that, ?int $scale = null, int $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal
{
$that = BigDecimal::of($that);
if ($that->isZero()) {
throw DivisionByZeroException::divisionByZero();
}
if ($scale === null) {
$scale = $this->scale;
} elseif ($scale < 0) {
throw new \InvalidArgumentException('Scale cannot be negative.');
}
if ($that->value === '1' && $that->scale === 0 && $scale === $this->scale) {
return $this;
}
$p = $this->valueWithMinScale($that->scale + $scale);
$q = $that->valueWithMinScale($this->scale - $scale);
$result = Calculator::get()->divRound($p, $q, $roundingMode);
return new BigDecimal($result, $scale);
}
/**
* Returns the exact result of the division of this number by the given one.
*
* The scale of the result is automatically calculated to fit all the fraction digits.
*
* @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigDecimal.
*
* @return BigDecimal The result.
*
* @throws MathException If the divisor is not a valid number, is not convertible to a BigDecimal, is zero,
* or the result yields an infinite number of digits.
*/
public function exactlyDividedBy($that) : BigDecimal
{
$that = BigDecimal::of($that);
if ($that->value === '0') {
throw DivisionByZeroException::divisionByZero();
}
[, $b] = $this->scaleValues($this, $that);
$d = \rtrim($b, '0');
$scale = \strlen($b) - \strlen($d);
$calculator = Calculator::get();
foreach ([5, 2] as $prime) {
for (;;) {
$lastDigit = (int) $d[-1];
if ($lastDigit % $prime !== 0) {
break;
}
$d = $calculator->divQ($d, (string) $prime);
$scale++;
}
}
return $this->dividedBy($that, $scale)->stripTrailingZeros();
}
/**
* Returns this number exponentiated to the given value.
*
* The result has a scale of `$this->scale * $exponent`.
*
* @param int $exponent The exponent.
*
* @return BigDecimal The result.
*
* @throws \InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
*/
public function power(int $exponent) : BigDecimal
{
if ($exponent === 0) {
return BigDecimal::one();
}
if ($exponent === 1) {
return $this;
}
if ($exponent < 0 || $exponent > Calculator::MAX_POWER) {
throw new \InvalidArgumentException(\sprintf(
'The exponent %d is not in the range 0 to %d.',
$exponent,
Calculator::MAX_POWER
));
}
return new BigDecimal(Calculator::get()->pow($this->value, $exponent), $this->scale * $exponent);
}
/**
* Returns the quotient of the division of this number by this given one.
*
* The quotient has a scale of `0`.
*
* @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigDecimal.
*
* @return BigDecimal The quotient.
*
* @throws MathException If the divisor is not a valid decimal number, or is zero.
*/
public function quotient($that) : BigDecimal
{
$that = BigDecimal::of($that);
if ($that->isZero()) {
throw DivisionByZeroException::divisionByZero();
}
$p = $this->valueWithMinScale($that->scale);
$q = $that->valueWithMinScale($this->scale);
$quotient = Calculator::get()->divQ($p, $q);
return new BigDecimal($quotient, 0);
}
/**
* Returns the remainder of the division of this number by this given one.
*
* The remainder has a scale of `max($this->scale, $that->scale)`.
*
* @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigDecimal.
*
* @return BigDecimal The remainder.
*
* @throws MathException If the divisor is not a valid decimal number, or is zero.
*/
public function remainder($that) : BigDecimal
{
$that = BigDecimal::of($that);
if ($that->isZero()) {
throw DivisionByZeroException::divisionByZero();
}
$p = $this->valueWithMinScale($that->scale);
$q = $that->valueWithMinScale($this->scale);
$remainder = Calculator::get()->divR($p, $q);
$scale = $this->scale > $that->scale ? $this->scale : $that->scale;
return new BigDecimal($remainder, $scale);
}
/**
* Returns the quotient and remainder of the division of this number by the given one.
*
* The quotient has a scale of `0`, and the remainder has a scale of `max($this->scale, $that->scale)`.
*
* @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigDecimal.
*
* @return BigDecimal[] An array containing the quotient and the remainder.
*
* @throws MathException If the divisor is not a valid decimal number, or is zero.
*/
public function quotientAndRemainder($that) : array
{
$that = BigDecimal::of($that);
if ($that->isZero()) {
throw DivisionByZeroException::divisionByZero();
}
$p = $this->valueWithMinScale($that->scale);
$q = $that->valueWithMinScale($this->scale);
[$quotient, $remainder] = Calculator::get()->divQR($p, $q);
$scale = $this->scale > $that->scale ? $this->scale : $that->scale;
$quotient = new BigDecimal($quotient, 0);
$remainder = new BigDecimal($remainder, $scale);
return [$quotient, $remainder];
}
/**
* Returns the square root of this number, rounded down to the given number of decimals.
*
* @param int $scale
*
* @return BigDecimal
*
* @throws \InvalidArgumentException If the scale is negative.
* @throws NegativeNumberException If this number is negative.
*/
public function sqrt(int $scale) : BigDecimal
{
if ($scale < 0) {
throw new \InvalidArgumentException('Scale cannot be negative.');
}
if ($this->value === '0') {
return new BigDecimal('0', $scale);
}
if ($this->value[0] === '-') {
throw new NegativeNumberException('Cannot calculate the square root of a negative number.');
}
$value = $this->value;
$addDigits = 2 * $scale - $this->scale;
if ($addDigits > 0) {
// add zeros
$value .= \str_repeat('0', $addDigits);
} elseif ($addDigits < 0) {
// trim digits
if (-$addDigits >= \strlen($this->value)) {
// requesting a scale too low, will always yield a zero result
return new BigDecimal('0', $scale);
}
$value = \substr($value, 0, $addDigits);
}
$value = Calculator::get()->sqrt($value);
return new BigDecimal($value, $scale);
}
/**
* Returns a copy of this BigDecimal with the decimal point moved $n places to the left.
*
* @param int $n
*
* @return BigDecimal
*/
public function withPointMovedLeft(int $n) : BigDecimal
{
if ($n === 0) {
return $this;
}
if ($n < 0) {
return $this->withPointMovedRight(-$n);
}
return new BigDecimal($this->value, $this->scale + $n);
}
/**
* Returns a copy of this BigDecimal with the decimal point moved $n places to the right.
*
* @param int $n
*
* @return BigDecimal
*/
public function withPointMovedRight(int $n) : BigDecimal
{
if ($n === 0) {
return $this;
}
if ($n < 0) {
return $this->withPointMovedLeft(-$n);
}
$value = $this->value;
$scale = $this->scale - $n;
if ($scale < 0) {
if ($value !== '0') {
$value .= \str_repeat('0', -$scale);
}
$scale = 0;
}
return new BigDecimal($value, $scale);
}
/**
* Returns a copy of this BigDecimal with any trailing zeros removed from the fractional part.
*
* @return BigDecimal
*/
public function stripTrailingZeros() : BigDecimal
{
if ($this->scale === 0) {
return $this;
}
$trimmedValue = \rtrim($this->value, '0');
if ($trimmedValue === '') {
return BigDecimal::zero();
}
$trimmableZeros = \strlen($this->value) - \strlen($trimmedValue);
if ($trimmableZeros === 0) {
return $this;
}
if ($trimmableZeros > $this->scale) {
$trimmableZeros = $this->scale;
}
$value = \substr($this->value, 0, -$trimmableZeros);
$scale = $this->scale - $trimmableZeros;
return new BigDecimal($value, $scale);
}
/**
* Returns the absolute value of this number.
*
* @return BigDecimal
*/
public function abs() : BigDecimal
{
return $this->isNegative() ? $this->negated() : $this;
}
/**
* Returns the negated value of this number.
*
* @return BigDecimal
*/
public function negated() : BigDecimal
{
return new BigDecimal(Calculator::get()->neg($this->value), $this->scale);
}
/**
* {@inheritdoc}
*/
public function compareTo($that) : int
{
$that = BigNumber::of($that);
if ($that instanceof BigInteger) {
$that = $that->toBigDecimal();
}
if ($that instanceof BigDecimal) {
[$a, $b] = $this->scaleValues($this, $that);
return Calculator::get()->cmp($a, $b);
}
return - $that->compareTo($this);
}
/**
* {@inheritdoc}
*/
public function getSign() : int
{
return ($this->value === '0') ? 0 : (($this->value[0] === '-') ? -1 : 1);
}
/**
* @return BigInteger
*/
public function getUnscaledValue() : BigInteger
{
return BigInteger::create($this->value);
}
/**
* @return int
*/
public function getScale() : int
{
return $this->scale;
}
/**
* Returns a string representing the integral part of this decimal number.
*
* Example: `-123.456` => `-123`.
*
* @return string
*/
public function getIntegralPart() : string
{
if ($this->scale === 0) {
return $this->value;
}
$value = $this->getUnscaledValueWithLeadingZeros();
return \substr($value, 0, -$this->scale);
}
/**
* Returns a string representing the fractional part of this decimal number.
*
* If the scale is zero, an empty string is returned.
*
* Examples: `-123.456` => '456', `123` => ''.
*
* @return string
*/
public function getFractionalPart() : string
{
if ($this->scale === 0) {
return '';
}
$value = $this->getUnscaledValueWithLeadingZeros();
return \substr($value, -$this->scale);
}
/**
* Returns whether this decimal number has a non-zero fractional part.
*
* @return bool
*/
public function hasNonZeroFractionalPart() : bool
{
return $this->getFractionalPart() !== \str_repeat('0', $this->scale);
}
/**
* {@inheritdoc}
*/
public function toBigInteger() : BigInteger
{
$zeroScaleDecimal = $this->scale === 0 ? $this : $this->dividedBy(1, 0);
return BigInteger::create($zeroScaleDecimal->value);
}
/**
* {@inheritdoc}
*/
public function toBigDecimal() : BigDecimal
{
return $this;
}
/**
* {@inheritdoc}
*/
public function toBigRational() : BigRational
{
$numerator = BigInteger::create($this->value);
$denominator = BigInteger::create('1' . \str_repeat('0', $this->scale));
return BigRational::create($numerator, $denominator, false);
}
/**
* {@inheritdoc}
*/
public function toScale(int $scale, int $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal
{
if ($scale === $this->scale) {
return $this;
}
return $this->dividedBy(BigDecimal::one(), $scale, $roundingMode);
}
/**
* {@inheritdoc}
*/
public function toInt() : int
{
return $this->toBigInteger()->toInt();
}
/**
* {@inheritdoc}
*/
public function toFloat() : float
{
return (float) (string) $this;
}
/**
* {@inheritdoc}
*/
public function __toString() : string
{
if ($this->scale === 0) {
return $this->value;
}
$value = $this->getUnscaledValueWithLeadingZeros();
return \substr($value, 0, -$this->scale) . '.' . \substr($value, -$this->scale);
}
/**
* This method is required for serializing the object and SHOULD NOT be accessed directly.
*
* @internal
*
* @return array{value: string, scale: int}
*/
public function __serialize(): array
{
return ['value' => $this->value, 'scale' => $this->scale];
}
/**
* This method is only here to allow unserializing the object and cannot be accessed directly.
*
* @internal
* @psalm-suppress RedundantPropertyInitializationCheck
*
* @param array{value: string, scale: int} $data
*
* @return void
*
* @throws \LogicException
*/
public function __unserialize(array $data): void
{
if (isset($this->value)) {
throw new \LogicException('__unserialize() is an internal function, it must not be called directly.');
}
$this->value = $data['value'];
$this->scale = $data['scale'];
}
/**
* This method is required by interface Serializable and SHOULD NOT be accessed directly.
*
* @internal
*
* @return string
*/
public function serialize() : string
{
return $this->value . ':' . $this->scale;
}
/**
* This method is only here to implement interface Serializable and cannot be accessed directly.
*
* @internal
* @psalm-suppress RedundantPropertyInitializationCheck
*
* @param string $value
*
* @return void
*
* @throws \LogicException
*/
public function unserialize($value) : void
{
if (isset($this->value)) {
throw new \LogicException('unserialize() is an internal function, it must not be called directly.');
}
[$value, $scale] = \explode(':', $value);
$this->value = $value;
$this->scale = (int) $scale;
}
/**
* Puts the internal values of the given decimal numbers on the same scale.
*
* @param BigDecimal $x The first decimal number.
* @param BigDecimal $y The second decimal number.
*
* @return array{string, string} The scaled integer values of $x and $y.
*/
private function scaleValues(BigDecimal $x, BigDecimal $y) : array
{
$a = $x->value;
$b = $y->value;
if ($b !== '0' && $x->scale > $y->scale) {
$b .= \str_repeat('0', $x->scale - $y->scale);
} elseif ($a !== '0' && $x->scale < $y->scale) {
$a .= \str_repeat('0', $y->scale - $x->scale);
}
return [$a, $b];
}
/**
* @param int $scale
*
* @return string
*/
private function valueWithMinScale(int $scale) : string
{
$value = $this->value;
if ($this->value !== '0' && $scale > $this->scale) {
$value .= \str_repeat('0', $scale - $this->scale);
}
return $value;
}
/**
* Adds leading zeros if necessary to the unscaled value to represent the full decimal number.
*
* @return string
*/
private function getUnscaledValueWithLeadingZeros() : string
{
$value = $this->value;
$targetLength = $this->scale + 1;
$negative = ($value[0] === '-');
$length = \strlen($value);
if ($negative) {
$length--;
}
if ($length >= $targetLength) {
return $this->value;
}
if ($negative) {
$value = \substr($value, 1);
}
$value = \str_pad($value, $targetLength, '0', STR_PAD_LEFT);
if ($negative) {
$value = '-' . $value;
}
return $value;
}
}

1184
vendor/brick/math/src/BigInteger.php vendored Normal file

File diff suppressed because it is too large Load Diff

572
vendor/brick/math/src/BigNumber.php vendored Normal file
View File

@@ -0,0 +1,572 @@
<?php
declare(strict_types=1);
namespace Brick\Math;
use Brick\Math\Exception\DivisionByZeroException;
use Brick\Math\Exception\MathException;
use Brick\Math\Exception\NumberFormatException;
use Brick\Math\Exception\RoundingNecessaryException;
/**
* Common interface for arbitrary-precision rational numbers.
*
* @psalm-immutable
*/
abstract class BigNumber implements \Serializable, \JsonSerializable
{
/**
* The regular expression used to parse integer, decimal and rational numbers.
*/
private const PARSE_REGEXP =
'/^' .
'(?<sign>[\-\+])?' .
'(?:' .
'(?:' .
'(?<integral>[0-9]+)?' .
'(?<point>\.)?' .
'(?<fractional>[0-9]+)?' .
'(?:[eE](?<exponent>[\-\+]?[0-9]+))?' .
')|(?:' .
'(?<numerator>[0-9]+)' .
'\/?' .
'(?<denominator>[0-9]+)' .
')' .
')' .
'$/';
/**
* Creates a BigNumber of the given value.
*
* The concrete return type is dependent on the given value, with the following rules:
*
* - BigNumber instances are returned as is
* - integer numbers are returned as BigInteger
* - floating point numbers are converted to a string then parsed as such
* - strings containing a `/` character are returned as BigRational
* - strings containing a `.` character or using an exponential notation are returned as BigDecimal
* - strings containing only digits with an optional leading `+` or `-` sign are returned as BigInteger
*
* @param BigNumber|int|float|string $value
*
* @return BigNumber
*
* @throws NumberFormatException If the format of the number is not valid.
* @throws DivisionByZeroException If the value represents a rational number with a denominator of zero.
*
* @psalm-pure
*/
public static function of($value) : BigNumber
{
if ($value instanceof BigNumber) {
return $value;
}
if (\is_int($value)) {
return new BigInteger((string) $value);
}
/** @psalm-suppress RedundantCastGivenDocblockType We cannot trust the untyped $value here! */
$value = \is_float($value) ? self::floatToString($value) : (string) $value;
$throw = static function() use ($value) : void {
throw new NumberFormatException(\sprintf(
'The given value "%s" does not represent a valid number.',
$value
));
};
if (\preg_match(self::PARSE_REGEXP, $value, $matches) !== 1) {
$throw();
}
$getMatch = static function(string $value) use ($matches) : ?string {
return isset($matches[$value]) && $matches[$value] !== '' ? $matches[$value] : null;
};
$sign = $getMatch('sign');
$numerator = $getMatch('numerator');
$denominator = $getMatch('denominator');
if ($numerator !== null) {
assert($denominator !== null);
if ($sign !== null) {
$numerator = $sign . $numerator;
}
$numerator = self::cleanUp($numerator);
$denominator = self::cleanUp($denominator);
if ($denominator === '0') {
throw DivisionByZeroException::denominatorMustNotBeZero();
}
return new BigRational(
new BigInteger($numerator),
new BigInteger($denominator),
false
);
}
$point = $getMatch('point');
$integral = $getMatch('integral');
$fractional = $getMatch('fractional');
$exponent = $getMatch('exponent');
if ($integral === null && $fractional === null) {
$throw();
}
if ($integral === null) {
$integral = '0';
}
if ($point !== null || $exponent !== null) {
$fractional = ($fractional ?? '');
$exponent = ($exponent !== null) ? (int) $exponent : 0;
if ($exponent === PHP_INT_MIN || $exponent === PHP_INT_MAX) {
throw new NumberFormatException('Exponent too large.');
}
$unscaledValue = self::cleanUp(($sign ?? ''). $integral . $fractional);
$scale = \strlen($fractional) - $exponent;
if ($scale < 0) {
if ($unscaledValue !== '0') {
$unscaledValue .= \str_repeat('0', - $scale);
}
$scale = 0;
}
return new BigDecimal($unscaledValue, $scale);
}
$integral = self::cleanUp(($sign ?? '') . $integral);
return new BigInteger($integral);
}
/**
* Safely converts float to string, avoiding locale-dependent issues.
*
* @see https://github.com/brick/math/pull/20
*
* @param float $float
*
* @return string
*
* @psalm-pure
* @psalm-suppress ImpureFunctionCall
*/
private static function floatToString(float $float) : string
{
$currentLocale = \setlocale(LC_NUMERIC, '0');
\setlocale(LC_NUMERIC, 'C');
$result = (string) $float;
\setlocale(LC_NUMERIC, $currentLocale);
return $result;
}
/**
* Proxy method to access protected constructors from sibling classes.
*
* @internal
*
* @param mixed ...$args The arguments to the constructor.
*
* @return static
*
* @psalm-pure
* @psalm-suppress TooManyArguments
* @psalm-suppress UnsafeInstantiation
*/
protected static function create(... $args) : BigNumber
{
return new static(... $args);
}
/**
* Returns the minimum of the given values.
*
* @param BigNumber|int|float|string ...$values The numbers to compare. All the numbers need to be convertible
* to an instance of the class this method is called on.
*
* @return static The minimum value.
*
* @throws \InvalidArgumentException If no values are given.
* @throws MathException If an argument is not valid.
*
* @psalm-suppress LessSpecificReturnStatement
* @psalm-suppress MoreSpecificReturnType
* @psalm-pure
*/
public static function min(...$values) : BigNumber
{
$min = null;
foreach ($values as $value) {
$value = static::of($value);
if ($min === null || $value->isLessThan($min)) {
$min = $value;
}
}
if ($min === null) {
throw new \InvalidArgumentException(__METHOD__ . '() expects at least one value.');
}
return $min;
}
/**
* Returns the maximum of the given values.
*
* @param BigNumber|int|float|string ...$values The numbers to compare. All the numbers need to be convertible
* to an instance of the class this method is called on.
*
* @return static The maximum value.
*
* @throws \InvalidArgumentException If no values are given.
* @throws MathException If an argument is not valid.
*
* @psalm-suppress LessSpecificReturnStatement
* @psalm-suppress MoreSpecificReturnType
* @psalm-pure
*/
public static function max(...$values) : BigNumber
{
$max = null;
foreach ($values as $value) {
$value = static::of($value);
if ($max === null || $value->isGreaterThan($max)) {
$max = $value;
}
}
if ($max === null) {
throw new \InvalidArgumentException(__METHOD__ . '() expects at least one value.');
}
return $max;
}
/**
* Returns the sum of the given values.
*
* @param BigNumber|int|float|string ...$values The numbers to add. All the numbers need to be convertible
* to an instance of the class this method is called on.
*
* @return static The sum.
*
* @throws \InvalidArgumentException If no values are given.
* @throws MathException If an argument is not valid.
*
* @psalm-suppress LessSpecificReturnStatement
* @psalm-suppress MoreSpecificReturnType
* @psalm-pure
*/
public static function sum(...$values) : BigNumber
{
/** @var BigNumber|null $sum */
$sum = null;
foreach ($values as $value) {
$value = static::of($value);
$sum = $sum === null ? $value : self::add($sum, $value);
}
if ($sum === null) {
throw new \InvalidArgumentException(__METHOD__ . '() expects at least one value.');
}
return $sum;
}
/**
* Adds two BigNumber instances in the correct order to avoid a RoundingNecessaryException.
*
* @todo This could be better resolved by creating an abstract protected method in BigNumber, and leaving to
* concrete classes the responsibility to perform the addition themselves or delegate it to the given number,
* depending on their ability to perform the operation. This will also require a version bump because we're
* potentially breaking custom BigNumber implementations (if any...)
*
* @param BigNumber $a
* @param BigNumber $b
*
* @return BigNumber
*
* @psalm-pure
*/
private static function add(BigNumber $a, BigNumber $b) : BigNumber
{
if ($a instanceof BigRational) {
return $a->plus($b);
}
if ($b instanceof BigRational) {
return $b->plus($a);
}
if ($a instanceof BigDecimal) {
return $a->plus($b);
}
if ($b instanceof BigDecimal) {
return $b->plus($a);
}
/** @var BigInteger $a */
return $a->plus($b);
}
/**
* Removes optional leading zeros and + sign from the given number.
*
* @param string $number The number, validated as a non-empty string of digits with optional leading sign.
*
* @return string
*
* @psalm-pure
*/
private static function cleanUp(string $number) : string
{
$firstChar = $number[0];
if ($firstChar === '+' || $firstChar === '-') {
$number = \substr($number, 1);
}
$number = \ltrim($number, '0');
if ($number === '') {
return '0';
}
if ($firstChar === '-') {
return '-' . $number;
}
return $number;
}
/**
* Checks if this number is equal to the given one.
*
* @param BigNumber|int|float|string $that
*
* @return bool
*/
public function isEqualTo($that) : bool
{
return $this->compareTo($that) === 0;
}
/**
* Checks if this number is strictly lower than the given one.
*
* @param BigNumber|int|float|string $that
*
* @return bool
*/
public function isLessThan($that) : bool
{
return $this->compareTo($that) < 0;
}
/**
* Checks if this number is lower than or equal to the given one.
*
* @param BigNumber|int|float|string $that
*
* @return bool
*/
public function isLessThanOrEqualTo($that) : bool
{
return $this->compareTo($that) <= 0;
}
/**
* Checks if this number is strictly greater than the given one.
*
* @param BigNumber|int|float|string $that
*
* @return bool
*/
public function isGreaterThan($that) : bool
{
return $this->compareTo($that) > 0;
}
/**
* Checks if this number is greater than or equal to the given one.
*
* @param BigNumber|int|float|string $that
*
* @return bool
*/
public function isGreaterThanOrEqualTo($that) : bool
{
return $this->compareTo($that) >= 0;
}
/**
* Checks if this number equals zero.
*
* @return bool
*/
public function isZero() : bool
{
return $this->getSign() === 0;
}
/**
* Checks if this number is strictly negative.
*
* @return bool
*/
public function isNegative() : bool
{
return $this->getSign() < 0;
}
/**
* Checks if this number is negative or zero.
*
* @return bool
*/
public function isNegativeOrZero() : bool
{
return $this->getSign() <= 0;
}
/**
* Checks if this number is strictly positive.
*
* @return bool
*/
public function isPositive() : bool
{
return $this->getSign() > 0;
}
/**
* Checks if this number is positive or zero.
*
* @return bool
*/
public function isPositiveOrZero() : bool
{
return $this->getSign() >= 0;
}
/**
* Returns the sign of this number.
*
* @return int -1 if the number is negative, 0 if zero, 1 if positive.
*/
abstract public function getSign() : int;
/**
* Compares this number to the given one.
*
* @param BigNumber|int|float|string $that
*
* @return int [-1,0,1] If `$this` is lower than, equal to, or greater than `$that`.
*
* @throws MathException If the number is not valid.
*/
abstract public function compareTo($that) : int;
/**
* Converts this number to a BigInteger.
*
* @return BigInteger The converted number.
*
* @throws RoundingNecessaryException If this number cannot be converted to a BigInteger without rounding.
*/
abstract public function toBigInteger() : BigInteger;
/**
* Converts this number to a BigDecimal.
*
* @return BigDecimal The converted number.
*
* @throws RoundingNecessaryException If this number cannot be converted to a BigDecimal without rounding.
*/
abstract public function toBigDecimal() : BigDecimal;
/**
* Converts this number to a BigRational.
*
* @return BigRational The converted number.
*/
abstract public function toBigRational() : BigRational;
/**
* Converts this number to a BigDecimal with the given scale, using rounding if necessary.
*
* @param int $scale The scale of the resulting `BigDecimal`.
* @param int $roundingMode A `RoundingMode` constant.
*
* @return BigDecimal
*
* @throws RoundingNecessaryException If this number cannot be converted to the given scale without rounding.
* This only applies when RoundingMode::UNNECESSARY is used.
*/
abstract public function toScale(int $scale, int $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal;
/**
* Returns the exact value of this number as a native integer.
*
* If this number cannot be converted to a native integer without losing precision, an exception is thrown.
* Note that the acceptable range for an integer depends on the platform and differs for 32-bit and 64-bit.
*
* @return int The converted value.
*
* @throws MathException If this number cannot be exactly converted to a native integer.
*/
abstract public function toInt() : int;
/**
* Returns an approximation of this number as a floating-point value.
*
* Note that this method can discard information as the precision of a floating-point value
* is inherently limited.
*
* If the number is greater than the largest representable floating point number, positive infinity is returned.
* If the number is less than the smallest representable floating point number, negative infinity is returned.
*
* @return float The converted value.
*/
abstract public function toFloat() : float;
/**
* Returns a string representation of this number.
*
* The output of this method can be parsed by the `of()` factory method;
* this will yield an object equal to this one, without any information loss.
*
* @return string
*/
abstract public function __toString() : string;
/**
* {@inheritdoc}
*/
public function jsonSerialize() : string
{
return $this->__toString();
}
}

523
vendor/brick/math/src/BigRational.php vendored Normal file
View File

@@ -0,0 +1,523 @@
<?php
declare(strict_types=1);
namespace Brick\Math;
use Brick\Math\Exception\DivisionByZeroException;
use Brick\Math\Exception\MathException;
use Brick\Math\Exception\NumberFormatException;
use Brick\Math\Exception\RoundingNecessaryException;
/**
* An arbitrarily large rational number.
*
* This class is immutable.
*
* @psalm-immutable
*/
final class BigRational extends BigNumber
{
/**
* The numerator.
*
* @var BigInteger
*/
private $numerator;
/**
* The denominator. Always strictly positive.
*
* @var BigInteger
*/
private $denominator;
/**
* Protected constructor. Use a factory method to obtain an instance.
*
* @param BigInteger $numerator The numerator.
* @param BigInteger $denominator The denominator.
* @param bool $checkDenominator Whether to check the denominator for negative and zero.
*
* @throws DivisionByZeroException If the denominator is zero.
*/
protected function __construct(BigInteger $numerator, BigInteger $denominator, bool $checkDenominator)
{
if ($checkDenominator) {
if ($denominator->isZero()) {
throw DivisionByZeroException::denominatorMustNotBeZero();
}
if ($denominator->isNegative()) {
$numerator = $numerator->negated();
$denominator = $denominator->negated();
}
}
$this->numerator = $numerator;
$this->denominator = $denominator;
}
/**
* Creates a BigRational of the given value.
*
* @param BigNumber|int|float|string $value
*
* @return BigRational
*
* @throws MathException If the value cannot be converted to a BigRational.
*
* @psalm-pure
*/
public static function of($value) : BigNumber
{
return parent::of($value)->toBigRational();
}
/**
* Creates a BigRational out of a numerator and a denominator.
*
* If the denominator is negative, the signs of both the numerator and the denominator
* will be inverted to ensure that the denominator is always positive.
*
* @param BigNumber|int|float|string $numerator The numerator. Must be convertible to a BigInteger.
* @param BigNumber|int|float|string $denominator The denominator. Must be convertible to a BigInteger.
*
* @return BigRational
*
* @throws NumberFormatException If an argument does not represent a valid number.
* @throws RoundingNecessaryException If an argument represents a non-integer number.
* @throws DivisionByZeroException If the denominator is zero.
*
* @psalm-pure
*/
public static function nd($numerator, $denominator) : BigRational
{
$numerator = BigInteger::of($numerator);
$denominator = BigInteger::of($denominator);
return new BigRational($numerator, $denominator, true);
}
/**
* Returns a BigRational representing zero.
*
* @return BigRational
*
* @psalm-pure
*/
public static function zero() : BigRational
{
/**
* @psalm-suppress ImpureStaticVariable
* @var BigRational|null $zero
*/
static $zero;
if ($zero === null) {
$zero = new BigRational(BigInteger::zero(), BigInteger::one(), false);
}
return $zero;
}
/**
* Returns a BigRational representing one.
*
* @return BigRational
*
* @psalm-pure
*/
public static function one() : BigRational
{
/**
* @psalm-suppress ImpureStaticVariable
* @var BigRational|null $one
*/
static $one;
if ($one === null) {
$one = new BigRational(BigInteger::one(), BigInteger::one(), false);
}
return $one;
}
/**
* Returns a BigRational representing ten.
*
* @return BigRational
*
* @psalm-pure
*/
public static function ten() : BigRational
{
/**
* @psalm-suppress ImpureStaticVariable
* @var BigRational|null $ten
*/
static $ten;
if ($ten === null) {
$ten = new BigRational(BigInteger::ten(), BigInteger::one(), false);
}
return $ten;
}
/**
* @return BigInteger
*/
public function getNumerator() : BigInteger
{
return $this->numerator;
}
/**
* @return BigInteger
*/
public function getDenominator() : BigInteger
{
return $this->denominator;
}
/**
* Returns the quotient of the division of the numerator by the denominator.
*
* @return BigInteger
*/
public function quotient() : BigInteger
{
return $this->numerator->quotient($this->denominator);
}
/**
* Returns the remainder of the division of the numerator by the denominator.
*
* @return BigInteger
*/
public function remainder() : BigInteger
{
return $this->numerator->remainder($this->denominator);
}
/**
* Returns the quotient and remainder of the division of the numerator by the denominator.
*
* @return BigInteger[]
*/
public function quotientAndRemainder() : array
{
return $this->numerator->quotientAndRemainder($this->denominator);
}
/**
* Returns the sum of this number and the given one.
*
* @param BigNumber|int|float|string $that The number to add.
*
* @return BigRational The result.
*
* @throws MathException If the number is not valid.
*/
public function plus($that) : BigRational
{
$that = BigRational::of($that);
$numerator = $this->numerator->multipliedBy($that->denominator);
$numerator = $numerator->plus($that->numerator->multipliedBy($this->denominator));
$denominator = $this->denominator->multipliedBy($that->denominator);
return new BigRational($numerator, $denominator, false);
}
/**
* Returns the difference of this number and the given one.
*
* @param BigNumber|int|float|string $that The number to subtract.
*
* @return BigRational The result.
*
* @throws MathException If the number is not valid.
*/
public function minus($that) : BigRational
{
$that = BigRational::of($that);
$numerator = $this->numerator->multipliedBy($that->denominator);
$numerator = $numerator->minus($that->numerator->multipliedBy($this->denominator));
$denominator = $this->denominator->multipliedBy($that->denominator);
return new BigRational($numerator, $denominator, false);
}
/**
* Returns the product of this number and the given one.
*
* @param BigNumber|int|float|string $that The multiplier.
*
* @return BigRational The result.
*
* @throws MathException If the multiplier is not a valid number.
*/
public function multipliedBy($that) : BigRational
{
$that = BigRational::of($that);
$numerator = $this->numerator->multipliedBy($that->numerator);
$denominator = $this->denominator->multipliedBy($that->denominator);
return new BigRational($numerator, $denominator, false);
}
/**
* Returns the result of the division of this number by the given one.
*
* @param BigNumber|int|float|string $that The divisor.
*
* @return BigRational The result.
*
* @throws MathException If the divisor is not a valid number, or is zero.
*/
public function dividedBy($that) : BigRational
{
$that = BigRational::of($that);
$numerator = $this->numerator->multipliedBy($that->denominator);
$denominator = $this->denominator->multipliedBy($that->numerator);
return new BigRational($numerator, $denominator, true);
}
/**
* Returns this number exponentiated to the given value.
*
* @param int $exponent The exponent.
*
* @return BigRational The result.
*
* @throws \InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
*/
public function power(int $exponent) : BigRational
{
if ($exponent === 0) {
$one = BigInteger::one();
return new BigRational($one, $one, false);
}
if ($exponent === 1) {
return $this;
}
return new BigRational(
$this->numerator->power($exponent),
$this->denominator->power($exponent),
false
);
}
/**
* Returns the reciprocal of this BigRational.
*
* The reciprocal has the numerator and denominator swapped.
*
* @return BigRational
*
* @throws DivisionByZeroException If the numerator is zero.
*/
public function reciprocal() : BigRational
{
return new BigRational($this->denominator, $this->numerator, true);
}
/**
* Returns the absolute value of this BigRational.
*
* @return BigRational
*/
public function abs() : BigRational
{
return new BigRational($this->numerator->abs(), $this->denominator, false);
}
/**
* Returns the negated value of this BigRational.
*
* @return BigRational
*/
public function negated() : BigRational
{
return new BigRational($this->numerator->negated(), $this->denominator, false);
}
/**
* Returns the simplified value of this BigRational.
*
* @return BigRational
*/
public function simplified() : BigRational
{
$gcd = $this->numerator->gcd($this->denominator);
$numerator = $this->numerator->quotient($gcd);
$denominator = $this->denominator->quotient($gcd);
return new BigRational($numerator, $denominator, false);
}
/**
* {@inheritdoc}
*/
public function compareTo($that) : int
{
return $this->minus($that)->getSign();
}
/**
* {@inheritdoc}
*/
public function getSign() : int
{
return $this->numerator->getSign();
}
/**
* {@inheritdoc}
*/
public function toBigInteger() : BigInteger
{
$simplified = $this->simplified();
if (! $simplified->denominator->isEqualTo(1)) {
throw new RoundingNecessaryException('This rational number cannot be represented as an integer value without rounding.');
}
return $simplified->numerator;
}
/**
* {@inheritdoc}
*/
public function toBigDecimal() : BigDecimal
{
return $this->numerator->toBigDecimal()->exactlyDividedBy($this->denominator);
}
/**
* {@inheritdoc}
*/
public function toBigRational() : BigRational
{
return $this;
}
/**
* {@inheritdoc}
*/
public function toScale(int $scale, int $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal
{
return $this->numerator->toBigDecimal()->dividedBy($this->denominator, $scale, $roundingMode);
}
/**
* {@inheritdoc}
*/
public function toInt() : int
{
return $this->toBigInteger()->toInt();
}
/**
* {@inheritdoc}
*/
public function toFloat() : float
{
return $this->numerator->toFloat() / $this->denominator->toFloat();
}
/**
* {@inheritdoc}
*/
public function __toString() : string
{
$numerator = (string) $this->numerator;
$denominator = (string) $this->denominator;
if ($denominator === '1') {
return $numerator;
}
return $this->numerator . '/' . $this->denominator;
}
/**
* This method is required for serializing the object and SHOULD NOT be accessed directly.
*
* @internal
*
* @return array{numerator: BigInteger, denominator: BigInteger}
*/
public function __serialize(): array
{
return ['numerator' => $this->numerator, 'denominator' => $this->denominator];
}
/**
* This method is only here to allow unserializing the object and cannot be accessed directly.
*
* @internal
* @psalm-suppress RedundantPropertyInitializationCheck
*
* @param array{numerator: BigInteger, denominator: BigInteger} $data
*
* @return void
*
* @throws \LogicException
*/
public function __unserialize(array $data): void
{
if (isset($this->numerator)) {
throw new \LogicException('__unserialize() is an internal function, it must not be called directly.');
}
$this->numerator = $data['numerator'];
$this->denominator = $data['denominator'];
}
/**
* This method is required by interface Serializable and SHOULD NOT be accessed directly.
*
* @internal
*
* @return string
*/
public function serialize() : string
{
return $this->numerator . '/' . $this->denominator;
}
/**
* This method is only here to implement interface Serializable and cannot be accessed directly.
*
* @internal
* @psalm-suppress RedundantPropertyInitializationCheck
*
* @param string $value
*
* @return void
*
* @throws \LogicException
*/
public function unserialize($value) : void
{
if (isset($this->numerator)) {
throw new \LogicException('unserialize() is an internal function, it must not be called directly.');
}
[$numerator, $denominator] = \explode('/', $value);
$this->numerator = BigInteger::of($numerator);
$this->denominator = BigInteger::of($denominator);
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Brick\Math\Exception;
/**
* Exception thrown when a division by zero occurs.
*/
class DivisionByZeroException extends MathException
{
/**
* @return DivisionByZeroException
*
* @psalm-pure
*/
public static function divisionByZero() : DivisionByZeroException
{
return new self('Division by zero.');
}
/**
* @return DivisionByZeroException
*
* @psalm-pure
*/
public static function modulusMustNotBeZero() : DivisionByZeroException
{
return new self('The modulus must not be zero.');
}
/**
* @return DivisionByZeroException
*
* @psalm-pure
*/
public static function denominatorMustNotBeZero() : DivisionByZeroException
{
return new self('The denominator of a rational number cannot be zero.');
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Brick\Math\Exception;
use Brick\Math\BigInteger;
/**
* Exception thrown when an integer overflow occurs.
*/
class IntegerOverflowException extends MathException
{
/**
* @param BigInteger $value
*
* @return IntegerOverflowException
*
* @psalm-pure
*/
public static function toIntOverflow(BigInteger $value) : IntegerOverflowException
{
$message = '%s is out of range %d to %d and cannot be represented as an integer.';
return new self(\sprintf($message, (string) $value, PHP_INT_MIN, PHP_INT_MAX));
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Brick\Math\Exception;
/**
* Base class for all math exceptions.
*
* This class is abstract to ensure that only fine-grained exceptions are thrown throughout the code.
*/
class MathException extends \RuntimeException
{
}

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Brick\Math\Exception;
/**
* Exception thrown when attempting to perform an unsupported operation, such as a square root, on a negative number.
*/
class NegativeNumberException extends MathException
{
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Brick\Math\Exception;
/**
* Exception thrown when attempting to create a number from a string with an invalid format.
*/
class NumberFormatException extends MathException
{
/**
* @param string $char The failing character.
*
* @return NumberFormatException
*
* @psalm-pure
*/
public static function charNotInAlphabet(string $char) : self
{
$ord = \ord($char);
if ($ord < 32 || $ord > 126) {
$char = \strtoupper(\dechex($ord));
if ($ord < 10) {
$char = '0' . $char;
}
} else {
$char = '"' . $char . '"';
}
return new self(sprintf('Char %s is not a valid character in the given alphabet.', $char));
}
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Brick\Math\Exception;
/**
* Exception thrown when a number cannot be represented at the requested scale without rounding.
*/
class RoundingNecessaryException extends MathException
{
/**
* @return RoundingNecessaryException
*
* @psalm-pure
*/
public static function roundingNecessary() : RoundingNecessaryException
{
return new self('Rounding is necessary to represent the result of the operation at this scale.');
}
}

View File

@@ -0,0 +1,756 @@
<?php
declare(strict_types=1);
namespace Brick\Math\Internal;
use Brick\Math\Exception\RoundingNecessaryException;
use Brick\Math\RoundingMode;
/**
* Performs basic operations on arbitrary size integers.
*
* Unless otherwise specified, all parameters must be validated as non-empty strings of digits,
* without leading zero, and with an optional leading minus sign if the number is not zero.
*
* Any other parameter format will lead to undefined behaviour.
* All methods must return strings respecting this format, unless specified otherwise.
*
* @internal
*
* @psalm-immutable
*/
abstract class Calculator
{
/**
* The maximum exponent value allowed for the pow() method.
*/
public const MAX_POWER = 1000000;
/**
* The alphabet for converting from and to base 2 to 36, lowercase.
*/
public const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz';
/**
* The Calculator instance in use.
*
* @var Calculator|null
*/
private static $instance;
/**
* Sets the Calculator instance to use.
*
* An instance is typically set only in unit tests: the autodetect is usually the best option.
*
* @param Calculator|null $calculator The calculator instance, or NULL to revert to autodetect.
*
* @return void
*/
final public static function set(?Calculator $calculator) : void
{
self::$instance = $calculator;
}
/**
* Returns the Calculator instance to use.
*
* If none has been explicitly set, the fastest available implementation will be returned.
*
* @return Calculator
*
* @psalm-pure
* @psalm-suppress ImpureStaticProperty
*/
final public static function get() : Calculator
{
if (self::$instance === null) {
/** @psalm-suppress ImpureMethodCall */
self::$instance = self::detect();
}
return self::$instance;
}
/**
* Returns the fastest available Calculator implementation.
*
* @codeCoverageIgnore
*
* @return Calculator
*/
private static function detect() : Calculator
{
if (\extension_loaded('gmp')) {
return new Calculator\GmpCalculator();
}
if (\extension_loaded('bcmath')) {
return new Calculator\BcMathCalculator();
}
return new Calculator\NativeCalculator();
}
/**
* Extracts the sign & digits of the operands.
*
* @param string $a The first operand.
* @param string $b The second operand.
*
* @return array{bool, bool, string, string} Whether $a and $b are negative, followed by their digits.
*/
final protected function init(string $a, string $b) : array
{
return [
$aNeg = ($a[0] === '-'),
$bNeg = ($b[0] === '-'),
$aNeg ? \substr($a, 1) : $a,
$bNeg ? \substr($b, 1) : $b,
];
}
/**
* Returns the absolute value of a number.
*
* @param string $n The number.
*
* @return string The absolute value.
*/
final public function abs(string $n) : string
{
return ($n[0] === '-') ? \substr($n, 1) : $n;
}
/**
* Negates a number.
*
* @param string $n The number.
*
* @return string The negated value.
*/
final public function neg(string $n) : string
{
if ($n === '0') {
return '0';
}
if ($n[0] === '-') {
return \substr($n, 1);
}
return '-' . $n;
}
/**
* Compares two numbers.
*
* @param string $a The first number.
* @param string $b The second number.
*
* @return int [-1, 0, 1] If the first number is less than, equal to, or greater than the second number.
*/
final public function cmp(string $a, string $b) : int
{
[$aNeg, $bNeg, $aDig, $bDig] = $this->init($a, $b);
if ($aNeg && ! $bNeg) {
return -1;
}
if ($bNeg && ! $aNeg) {
return 1;
}
$aLen = \strlen($aDig);
$bLen = \strlen($bDig);
if ($aLen < $bLen) {
$result = -1;
} elseif ($aLen > $bLen) {
$result = 1;
} else {
$result = $aDig <=> $bDig;
}
return $aNeg ? -$result : $result;
}
/**
* Adds two numbers.
*
* @param string $a The augend.
* @param string $b The addend.
*
* @return string The sum.
*/
abstract public function add(string $a, string $b) : string;
/**
* Subtracts two numbers.
*
* @param string $a The minuend.
* @param string $b The subtrahend.
*
* @return string The difference.
*/
abstract public function sub(string $a, string $b) : string;
/**
* Multiplies two numbers.
*
* @param string $a The multiplicand.
* @param string $b The multiplier.
*
* @return string The product.
*/
abstract public function mul(string $a, string $b) : string;
/**
* Returns the quotient of the division of two numbers.
*
* @param string $a The dividend.
* @param string $b The divisor, must not be zero.
*
* @return string The quotient.
*/
abstract public function divQ(string $a, string $b) : string;
/**
* Returns the remainder of the division of two numbers.
*
* @param string $a The dividend.
* @param string $b The divisor, must not be zero.
*
* @return string The remainder.
*/
abstract public function divR(string $a, string $b) : string;
/**
* Returns the quotient and remainder of the division of two numbers.
*
* @param string $a The dividend.
* @param string $b The divisor, must not be zero.
*
* @return string[] An array containing the quotient and remainder.
*/
abstract public function divQR(string $a, string $b) : array;
/**
* Exponentiates a number.
*
* @param string $a The base number.
* @param int $e The exponent, validated as an integer between 0 and MAX_POWER.
*
* @return string The power.
*/
abstract public function pow(string $a, int $e) : string;
/**
* @param string $a
* @param string $b The modulus; must not be zero.
*
* @return string
*/
public function mod(string $a, string $b) : string
{
return $this->divR($this->add($this->divR($a, $b), $b), $b);
}
/**
* Returns the modular multiplicative inverse of $x modulo $m.
*
* If $x has no multiplicative inverse mod m, this method must return null.
*
* This method can be overridden by the concrete implementation if the underlying library has built-in support.
*
* @param string $x
* @param string $m The modulus; must not be negative or zero.
*
* @return string|null
*/
public function modInverse(string $x, string $m) : ?string
{
if ($m === '1') {
return '0';
}
$modVal = $x;
if ($x[0] === '-' || ($this->cmp($this->abs($x), $m) >= 0)) {
$modVal = $this->mod($x, $m);
}
$x = '0';
$y = '0';
$g = $this->gcdExtended($modVal, $m, $x, $y);
if ($g !== '1') {
return null;
}
return $this->mod($this->add($this->mod($x, $m), $m), $m);
}
/**
* Raises a number into power with modulo.
*
* @param string $base The base number; must be positive or zero.
* @param string $exp The exponent; must be positive or zero.
* @param string $mod The modulus; must be strictly positive.
*
* @return string The power.
*/
abstract public function modPow(string $base, string $exp, string $mod) : string;
/**
* Returns the greatest common divisor of the two numbers.
*
* This method can be overridden by the concrete implementation if the underlying library
* has built-in support for GCD calculations.
*
* @param string $a The first number.
* @param string $b The second number.
*
* @return string The GCD, always positive, or zero if both arguments are zero.
*/
public function gcd(string $a, string $b) : string
{
if ($a === '0') {
return $this->abs($b);
}
if ($b === '0') {
return $this->abs($a);
}
return $this->gcd($b, $this->divR($a, $b));
}
private function gcdExtended(string $a, string $b, string &$x, string &$y) : string
{
if ($a === '0') {
$x = '0';
$y = '1';
return $b;
}
$x1 = '0';
$y1 = '0';
$gcd = $this->gcdExtended($this->mod($b, $a), $a, $x1, $y1);
$x = $this->sub($y1, $this->mul($this->divQ($b, $a), $x1));
$y = $x1;
return $gcd;
}
/**
* Returns the square root of the given number, rounded down.
*
* The result is the largest x such that x² ≤ n.
* The input MUST NOT be negative.
*
* @param string $n The number.
*
* @return string The square root.
*/
abstract public function sqrt(string $n) : string;
/**
* Converts a number from an arbitrary base.
*
* This method can be overridden by the concrete implementation if the underlying library
* has built-in support for base conversion.
*
* @param string $number The number, positive or zero, non-empty, case-insensitively validated for the given base.
* @param int $base The base of the number, validated from 2 to 36.
*
* @return string The converted number, following the Calculator conventions.
*/
public function fromBase(string $number, int $base) : string
{
return $this->fromArbitraryBase(\strtolower($number), self::ALPHABET, $base);
}
/**
* Converts a number to an arbitrary base.
*
* This method can be overridden by the concrete implementation if the underlying library
* has built-in support for base conversion.
*
* @param string $number The number to convert, following the Calculator conventions.
* @param int $base The base to convert to, validated from 2 to 36.
*
* @return string The converted number, lowercase.
*/
public function toBase(string $number, int $base) : string
{
$negative = ($number[0] === '-');
if ($negative) {
$number = \substr($number, 1);
}
$number = $this->toArbitraryBase($number, self::ALPHABET, $base);
if ($negative) {
return '-' . $number;
}
return $number;
}
/**
* Converts a non-negative number in an arbitrary base using a custom alphabet, to base 10.
*
* @param string $number The number to convert, validated as a non-empty string,
* containing only chars in the given alphabet/base.
* @param string $alphabet The alphabet that contains every digit, validated as 2 chars minimum.
* @param int $base The base of the number, validated from 2 to alphabet length.
*
* @return string The number in base 10, following the Calculator conventions.
*/
final public function fromArbitraryBase(string $number, string $alphabet, int $base) : string
{
// remove leading "zeros"
$number = \ltrim($number, $alphabet[0]);
if ($number === '') {
return '0';
}
// optimize for "one"
if ($number === $alphabet[1]) {
return '1';
}
$result = '0';
$power = '1';
$base = (string) $base;
for ($i = \strlen($number) - 1; $i >= 0; $i--) {
$index = \strpos($alphabet, $number[$i]);
if ($index !== 0) {
$result = $this->add($result, ($index === 1)
? $power
: $this->mul($power, (string) $index)
);
}
if ($i !== 0) {
$power = $this->mul($power, $base);
}
}
return $result;
}
/**
* Converts a non-negative number to an arbitrary base using a custom alphabet.
*
* @param string $number The number to convert, positive or zero, following the Calculator conventions.
* @param string $alphabet The alphabet that contains every digit, validated as 2 chars minimum.
* @param int $base The base to convert to, validated from 2 to alphabet length.
*
* @return string The converted number in the given alphabet.
*/
final public function toArbitraryBase(string $number, string $alphabet, int $base) : string
{
if ($number === '0') {
return $alphabet[0];
}
$base = (string) $base;
$result = '';
while ($number !== '0') {
[$number, $remainder] = $this->divQR($number, $base);
$remainder = (int) $remainder;
$result .= $alphabet[$remainder];
}
return \strrev($result);
}
/**
* Performs a rounded division.
*
* Rounding is performed when the remainder of the division is not zero.
*
* @param string $a The dividend.
* @param string $b The divisor, must not be zero.
* @param int $roundingMode The rounding mode.
*
* @return string
*
* @throws \InvalidArgumentException If the rounding mode is invalid.
* @throws RoundingNecessaryException If RoundingMode::UNNECESSARY is provided but rounding is necessary.
*/
final public function divRound(string $a, string $b, int $roundingMode) : string
{
[$quotient, $remainder] = $this->divQR($a, $b);
$hasDiscardedFraction = ($remainder !== '0');
$isPositiveOrZero = ($a[0] === '-') === ($b[0] === '-');
$discardedFractionSign = function() use ($remainder, $b) : int {
$r = $this->abs($this->mul($remainder, '2'));
$b = $this->abs($b);
return $this->cmp($r, $b);
};
$increment = false;
switch ($roundingMode) {
case RoundingMode::UNNECESSARY:
if ($hasDiscardedFraction) {
throw RoundingNecessaryException::roundingNecessary();
}
break;
case RoundingMode::UP:
$increment = $hasDiscardedFraction;
break;
case RoundingMode::DOWN:
break;
case RoundingMode::CEILING:
$increment = $hasDiscardedFraction && $isPositiveOrZero;
break;
case RoundingMode::FLOOR:
$increment = $hasDiscardedFraction && ! $isPositiveOrZero;
break;
case RoundingMode::HALF_UP:
$increment = $discardedFractionSign() >= 0;
break;
case RoundingMode::HALF_DOWN:
$increment = $discardedFractionSign() > 0;
break;
case RoundingMode::HALF_CEILING:
$increment = $isPositiveOrZero ? $discardedFractionSign() >= 0 : $discardedFractionSign() > 0;
break;
case RoundingMode::HALF_FLOOR:
$increment = $isPositiveOrZero ? $discardedFractionSign() > 0 : $discardedFractionSign() >= 0;
break;
case RoundingMode::HALF_EVEN:
$lastDigit = (int) $quotient[-1];
$lastDigitIsEven = ($lastDigit % 2 === 0);
$increment = $lastDigitIsEven ? $discardedFractionSign() > 0 : $discardedFractionSign() >= 0;
break;
default:
throw new \InvalidArgumentException('Invalid rounding mode.');
}
if ($increment) {
return $this->add($quotient, $isPositiveOrZero ? '1' : '-1');
}
return $quotient;
}
/**
* Calculates bitwise AND of two numbers.
*
* This method can be overridden by the concrete implementation if the underlying library
* has built-in support for bitwise operations.
*
* @param string $a
* @param string $b
*
* @return string
*/
public function and(string $a, string $b) : string
{
return $this->bitwise('and', $a, $b);
}
/**
* Calculates bitwise OR of two numbers.
*
* This method can be overridden by the concrete implementation if the underlying library
* has built-in support for bitwise operations.
*
* @param string $a
* @param string $b
*
* @return string
*/
public function or(string $a, string $b) : string
{
return $this->bitwise('or', $a, $b);
}
/**
* Calculates bitwise XOR of two numbers.
*
* This method can be overridden by the concrete implementation if the underlying library
* has built-in support for bitwise operations.
*
* @param string $a
* @param string $b
*
* @return string
*/
public function xor(string $a, string $b) : string
{
return $this->bitwise('xor', $a, $b);
}
/**
* Performs a bitwise operation on a decimal number.
*
* @param string $operator The operator to use, must be "and", "or" or "xor".
* @param string $a The left operand.
* @param string $b The right operand.
*
* @return string
*/
private function bitwise(string $operator, string $a, string $b) : string
{
[$aNeg, $bNeg, $aDig, $bDig] = $this->init($a, $b);
$aBin = $this->toBinary($aDig);
$bBin = $this->toBinary($bDig);
$aLen = \strlen($aBin);
$bLen = \strlen($bBin);
if ($aLen > $bLen) {
$bBin = \str_repeat("\x00", $aLen - $bLen) . $bBin;
} elseif ($bLen > $aLen) {
$aBin = \str_repeat("\x00", $bLen - $aLen) . $aBin;
}
if ($aNeg) {
$aBin = $this->twosComplement($aBin);
}
if ($bNeg) {
$bBin = $this->twosComplement($bBin);
}
switch ($operator) {
case 'and':
$value = $aBin & $bBin;
$negative = ($aNeg and $bNeg);
break;
case 'or':
$value = $aBin | $bBin;
$negative = ($aNeg or $bNeg);
break;
case 'xor':
$value = $aBin ^ $bBin;
$negative = ($aNeg xor $bNeg);
break;
// @codeCoverageIgnoreStart
default:
throw new \InvalidArgumentException('Invalid bitwise operator.');
// @codeCoverageIgnoreEnd
}
if ($negative) {
$value = $this->twosComplement($value);
}
$result = $this->toDecimal($value);
return $negative ? $this->neg($result) : $result;
}
/**
* @param string $number A positive, binary number.
*
* @return string
*/
private function twosComplement(string $number) : string
{
$xor = \str_repeat("\xff", \strlen($number));
$number ^= $xor;
for ($i = \strlen($number) - 1; $i >= 0; $i--) {
$byte = \ord($number[$i]);
if (++$byte !== 256) {
$number[$i] = \chr($byte);
break;
}
$number[$i] = "\x00";
if ($i === 0) {
$number = "\x01" . $number;
}
}
return $number;
}
/**
* Converts a decimal number to a binary string.
*
* @param string $number The number to convert, positive or zero, only digits.
*
* @return string
*/
private function toBinary(string $number) : string
{
$result = '';
while ($number !== '0') {
[$number, $remainder] = $this->divQR($number, '256');
$result .= \chr((int) $remainder);
}
return \strrev($result);
}
/**
* Returns the positive decimal representation of a binary number.
*
* @param string $bytes The bytes representing the number.
*
* @return string
*/
private function toDecimal(string $bytes) : string
{
$result = '0';
$power = '1';
for ($i = \strlen($bytes) - 1; $i >= 0; $i--) {
$index = \ord($bytes[$i]);
if ($index !== 0) {
$result = $this->add($result, ($index === 1)
? $power
: $this->mul($power, (string) $index)
);
}
if ($i !== 0) {
$power = $this->mul($power, '256');
}
}
return $result;
}
}

View File

@@ -0,0 +1,116 @@
<?php
declare(strict_types=1);
namespace Brick\Math\Internal\Calculator;
use Brick\Math\Internal\Calculator;
/**
* Calculator implementation built around the bcmath library.
*
* @internal
*
* @psalm-immutable
*/
class BcMathCalculator extends Calculator
{
/**
* {@inheritdoc}
*/
public function add(string $a, string $b) : string
{
return \bcadd($a, $b, 0);
}
/**
* {@inheritdoc}
*/
public function sub(string $a, string $b) : string
{
return \bcsub($a, $b, 0);
}
/**
* {@inheritdoc}
*/
public function mul(string $a, string $b) : string
{
return \bcmul($a, $b, 0);
}
/**
* {@inheritdoc}
*
* @psalm-suppress InvalidNullableReturnType
* @psalm-suppress NullableReturnStatement
*/
public function divQ(string $a, string $b) : string
{
return \bcdiv($a, $b, 0);
}
/**
* {@inheritdoc}
*
* @psalm-suppress InvalidNullableReturnType
* @psalm-suppress NullableReturnStatement
*/
public function divR(string $a, string $b) : string
{
if (version_compare(PHP_VERSION, '7.2') >= 0) {
return \bcmod($a, $b, 0);
}
return \bcmod($a, $b);
}
/**
* {@inheritdoc}
*/
public function divQR(string $a, string $b) : array
{
$q = \bcdiv($a, $b, 0);
if (version_compare(PHP_VERSION, '7.2') >= 0) {
$r = \bcmod($a, $b, 0);
} else {
$r = \bcmod($a, $b);
}
assert($q !== null);
assert($r !== null);
return [$q, $r];
}
/**
* {@inheritdoc}
*/
public function pow(string $a, int $e) : string
{
return \bcpow($a, (string) $e, 0);
}
/**
* {@inheritdoc}
*
* @psalm-suppress InvalidNullableReturnType
* @psalm-suppress NullableReturnStatement
*/
public function modPow(string $base, string $exp, string $mod) : string
{
return \bcpowmod($base, $exp, $mod, 0);
}
/**
* {@inheritDoc}
*
* @psalm-suppress NullableReturnStatement
* @psalm-suppress InvalidNullableReturnType
*/
public function sqrt(string $n) : string
{
return \bcsqrt($n, 0);
}
}

View File

@@ -0,0 +1,156 @@
<?php
declare(strict_types=1);
namespace Brick\Math\Internal\Calculator;
use Brick\Math\Internal\Calculator;
/**
* Calculator implementation built around the GMP library.
*
* @internal
*
* @psalm-immutable
*/
class GmpCalculator extends Calculator
{
/**
* {@inheritdoc}
*/
public function add(string $a, string $b) : string
{
return \gmp_strval(\gmp_add($a, $b));
}
/**
* {@inheritdoc}
*/
public function sub(string $a, string $b) : string
{
return \gmp_strval(\gmp_sub($a, $b));
}
/**
* {@inheritdoc}
*/
public function mul(string $a, string $b) : string
{
return \gmp_strval(\gmp_mul($a, $b));
}
/**
* {@inheritdoc}
*/
public function divQ(string $a, string $b) : string
{
return \gmp_strval(\gmp_div_q($a, $b));
}
/**
* {@inheritdoc}
*/
public function divR(string $a, string $b) : string
{
return \gmp_strval(\gmp_div_r($a, $b));
}
/**
* {@inheritdoc}
*/
public function divQR(string $a, string $b) : array
{
[$q, $r] = \gmp_div_qr($a, $b);
return [
\gmp_strval($q),
\gmp_strval($r)
];
}
/**
* {@inheritdoc}
*/
public function pow(string $a, int $e) : string
{
return \gmp_strval(\gmp_pow($a, $e));
}
/**
* {@inheritdoc}
*/
public function modInverse(string $x, string $m) : ?string
{
$result = \gmp_invert($x, $m);
if ($result === false) {
return null;
}
return \gmp_strval($result);
}
/**
* {@inheritdoc}
*/
public function modPow(string $base, string $exp, string $mod) : string
{
return \gmp_strval(\gmp_powm($base, $exp, $mod));
}
/**
* {@inheritdoc}
*/
public function gcd(string $a, string $b) : string
{
return \gmp_strval(\gmp_gcd($a, $b));
}
/**
* {@inheritdoc}
*/
public function fromBase(string $number, int $base) : string
{
return \gmp_strval(\gmp_init($number, $base));
}
/**
* {@inheritdoc}
*/
public function toBase(string $number, int $base) : string
{
return \gmp_strval($number, $base);
}
/**
* {@inheritdoc}
*/
public function and(string $a, string $b) : string
{
return \gmp_strval(\gmp_and($a, $b));
}
/**
* {@inheritdoc}
*/
public function or(string $a, string $b) : string
{
return \gmp_strval(\gmp_or($a, $b));
}
/**
* {@inheritdoc}
*/
public function xor(string $a, string $b) : string
{
return \gmp_strval(\gmp_xor($a, $b));
}
/**
* {@inheritDoc}
*/
public function sqrt(string $n) : string
{
return \gmp_strval(\gmp_sqrt($n));
}
}

View File

@@ -0,0 +1,634 @@
<?php
declare(strict_types=1);
namespace Brick\Math\Internal\Calculator;
use Brick\Math\Internal\Calculator;
/**
* Calculator implementation using only native PHP code.
*
* @internal
*
* @psalm-immutable
*/
class NativeCalculator extends Calculator
{
/**
* The max number of digits the platform can natively add, subtract, multiply or divide without overflow.
* For multiplication, this represents the max sum of the lengths of both operands.
*
* For addition, it is assumed that an extra digit can hold a carry (1) without overflowing.
* Example: 32-bit: max number 1,999,999,999 (9 digits + carry)
* 64-bit: max number 1,999,999,999,999,999,999 (18 digits + carry)
*
* @var int
*/
private $maxDigits;
/**
* Class constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
switch (PHP_INT_SIZE) {
case 4:
$this->maxDigits = 9;
break;
case 8:
$this->maxDigits = 18;
break;
default:
throw new \RuntimeException('The platform is not 32-bit or 64-bit as expected.');
}
}
/**
* {@inheritdoc}
*/
public function add(string $a, string $b) : string
{
/**
* @psalm-var numeric-string $a
* @psalm-var numeric-string $b
*/
$result = $a + $b;
if (is_int($result)) {
return (string) $result;
}
if ($a === '0') {
return $b;
}
if ($b === '0') {
return $a;
}
[$aNeg, $bNeg, $aDig, $bDig] = $this->init($a, $b);
$result = $aNeg === $bNeg ? $this->doAdd($aDig, $bDig) : $this->doSub($aDig, $bDig);
if ($aNeg) {
$result = $this->neg($result);
}
return $result;
}
/**
* {@inheritdoc}
*/
public function sub(string $a, string $b) : string
{
return $this->add($a, $this->neg($b));
}
/**
* {@inheritdoc}
*/
public function mul(string $a, string $b) : string
{
/**
* @psalm-var numeric-string $a
* @psalm-var numeric-string $b
*/
$result = $a * $b;
if (is_int($result)) {
return (string) $result;
}
if ($a === '0' || $b === '0') {
return '0';
}
if ($a === '1') {
return $b;
}
if ($b === '1') {
return $a;
}
if ($a === '-1') {
return $this->neg($b);
}
if ($b === '-1') {
return $this->neg($a);
}
[$aNeg, $bNeg, $aDig, $bDig] = $this->init($a, $b);
$result = $this->doMul($aDig, $bDig);
if ($aNeg !== $bNeg) {
$result = $this->neg($result);
}
return $result;
}
/**
* {@inheritdoc}
*/
public function divQ(string $a, string $b) : string
{
return $this->divQR($a, $b)[0];
}
/**
* {@inheritdoc}
*/
public function divR(string $a, string $b): string
{
return $this->divQR($a, $b)[1];
}
/**
* {@inheritdoc}
*/
public function divQR(string $a, string $b) : array
{
if ($a === '0') {
return ['0', '0'];
}
if ($a === $b) {
return ['1', '0'];
}
if ($b === '1') {
return [$a, '0'];
}
if ($b === '-1') {
return [$this->neg($a), '0'];
}
/** @psalm-var numeric-string $a */
$na = $a * 1; // cast to number
if (is_int($na)) {
/** @psalm-var numeric-string $b */
$nb = $b * 1;
if (is_int($nb)) {
// the only division that may overflow is PHP_INT_MIN / -1,
// which cannot happen here as we've already handled a divisor of -1 above.
$r = $na % $nb;
$q = ($na - $r) / $nb;
assert(is_int($q));
return [
(string) $q,
(string) $r
];
}
}
[$aNeg, $bNeg, $aDig, $bDig] = $this->init($a, $b);
[$q, $r] = $this->doDiv($aDig, $bDig);
if ($aNeg !== $bNeg) {
$q = $this->neg($q);
}
if ($aNeg) {
$r = $this->neg($r);
}
return [$q, $r];
}
/**
* {@inheritdoc}
*/
public function pow(string $a, int $e) : string
{
if ($e === 0) {
return '1';
}
if ($e === 1) {
return $a;
}
$odd = $e % 2;
$e -= $odd;
$aa = $this->mul($a, $a);
/** @psalm-suppress PossiblyInvalidArgument We're sure that $e / 2 is an int now */
$result = $this->pow($aa, $e / 2);
if ($odd === 1) {
$result = $this->mul($result, $a);
}
return $result;
}
/**
* Algorithm from: https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
*
* {@inheritdoc}
*/
public function modPow(string $base, string $exp, string $mod) : string
{
// special case: the algorithm below fails with 0 power 0 mod 1 (returns 1 instead of 0)
if ($base === '0' && $exp === '0' && $mod === '1') {
return '0';
}
// special case: the algorithm below fails with power 0 mod 1 (returns 1 instead of 0)
if ($exp === '0' && $mod === '1') {
return '0';
}
$x = $base;
$res = '1';
// numbers are positive, so we can use remainder instead of modulo
$x = $this->divR($x, $mod);
while ($exp !== '0') {
if (in_array($exp[-1], ['1', '3', '5', '7', '9'])) { // odd
$res = $this->divR($this->mul($res, $x), $mod);
}
$exp = $this->divQ($exp, '2');
$x = $this->divR($this->mul($x, $x), $mod);
}
return $res;
}
/**
* Adapted from https://cp-algorithms.com/num_methods/roots_newton.html
*
* {@inheritDoc}
*/
public function sqrt(string $n) : string
{
if ($n === '0') {
return '0';
}
// initial approximation
$x = \str_repeat('9', \intdiv(\strlen($n), 2) ?: 1);
$decreased = false;
for (;;) {
$nx = $this->divQ($this->add($x, $this->divQ($n, $x)), '2');
if ($x === $nx || $this->cmp($nx, $x) > 0 && $decreased) {
break;
}
$decreased = $this->cmp($nx, $x) < 0;
$x = $nx;
}
return $x;
}
/**
* Performs the addition of two non-signed large integers.
*
* @param string $a The first operand.
* @param string $b The second operand.
*
* @return string
*/
private function doAdd(string $a, string $b) : string
{
[$a, $b, $length] = $this->pad($a, $b);
$carry = 0;
$result = '';
for ($i = $length - $this->maxDigits;; $i -= $this->maxDigits) {
$blockLength = $this->maxDigits;
if ($i < 0) {
$blockLength += $i;
/** @psalm-suppress LoopInvalidation */
$i = 0;
}
/** @psalm-var numeric-string $blockA */
$blockA = \substr($a, $i, $blockLength);
/** @psalm-var numeric-string $blockB */
$blockB = \substr($b, $i, $blockLength);
$sum = (string) ($blockA + $blockB + $carry);
$sumLength = \strlen($sum);
if ($sumLength > $blockLength) {
$sum = \substr($sum, 1);
$carry = 1;
} else {
if ($sumLength < $blockLength) {
$sum = \str_repeat('0', $blockLength - $sumLength) . $sum;
}
$carry = 0;
}
$result = $sum . $result;
if ($i === 0) {
break;
}
}
if ($carry === 1) {
$result = '1' . $result;
}
return $result;
}
/**
* Performs the subtraction of two non-signed large integers.
*
* @param string $a The first operand.
* @param string $b The second operand.
*
* @return string
*/
private function doSub(string $a, string $b) : string
{
if ($a === $b) {
return '0';
}
// Ensure that we always subtract to a positive result: biggest minus smallest.
$cmp = $this->doCmp($a, $b);
$invert = ($cmp === -1);
if ($invert) {
$c = $a;
$a = $b;
$b = $c;
}
[$a, $b, $length] = $this->pad($a, $b);
$carry = 0;
$result = '';
$complement = 10 ** $this->maxDigits;
for ($i = $length - $this->maxDigits;; $i -= $this->maxDigits) {
$blockLength = $this->maxDigits;
if ($i < 0) {
$blockLength += $i;
/** @psalm-suppress LoopInvalidation */
$i = 0;
}
/** @psalm-var numeric-string $blockA */
$blockA = \substr($a, $i, $blockLength);
/** @psalm-var numeric-string $blockB */
$blockB = \substr($b, $i, $blockLength);
$sum = $blockA - $blockB - $carry;
if ($sum < 0) {
$sum += $complement;
$carry = 1;
} else {
$carry = 0;
}
$sum = (string) $sum;
$sumLength = \strlen($sum);
if ($sumLength < $blockLength) {
$sum = \str_repeat('0', $blockLength - $sumLength) . $sum;
}
$result = $sum . $result;
if ($i === 0) {
break;
}
}
// Carry cannot be 1 when the loop ends, as a > b
assert($carry === 0);
$result = \ltrim($result, '0');
if ($invert) {
$result = $this->neg($result);
}
return $result;
}
/**
* Performs the multiplication of two non-signed large integers.
*
* @param string $a The first operand.
* @param string $b The second operand.
*
* @return string
*/
private function doMul(string $a, string $b) : string
{
$x = \strlen($a);
$y = \strlen($b);
$maxDigits = \intdiv($this->maxDigits, 2);
$complement = 10 ** $maxDigits;
$result = '0';
for ($i = $x - $maxDigits;; $i -= $maxDigits) {
$blockALength = $maxDigits;
if ($i < 0) {
$blockALength += $i;
/** @psalm-suppress LoopInvalidation */
$i = 0;
}
$blockA = (int) \substr($a, $i, $blockALength);
$line = '';
$carry = 0;
for ($j = $y - $maxDigits;; $j -= $maxDigits) {
$blockBLength = $maxDigits;
if ($j < 0) {
$blockBLength += $j;
/** @psalm-suppress LoopInvalidation */
$j = 0;
}
$blockB = (int) \substr($b, $j, $blockBLength);
$mul = $blockA * $blockB + $carry;
$value = $mul % $complement;
$carry = ($mul - $value) / $complement;
$value = (string) $value;
$value = \str_pad($value, $maxDigits, '0', STR_PAD_LEFT);
$line = $value . $line;
if ($j === 0) {
break;
}
}
if ($carry !== 0) {
$line = $carry . $line;
}
$line = \ltrim($line, '0');
if ($line !== '') {
$line .= \str_repeat('0', $x - $blockALength - $i);
$result = $this->add($result, $line);
}
if ($i === 0) {
break;
}
}
return $result;
}
/**
* Performs the division of two non-signed large integers.
*
* @param string $a The first operand.
* @param string $b The second operand.
*
* @return string[] The quotient and remainder.
*/
private function doDiv(string $a, string $b) : array
{
$cmp = $this->doCmp($a, $b);
if ($cmp === -1) {
return ['0', $a];
}
$x = \strlen($a);
$y = \strlen($b);
// we now know that a >= b && x >= y
$q = '0'; // quotient
$r = $a; // remainder
$z = $y; // focus length, always $y or $y+1
for (;;) {
$focus = \substr($a, 0, $z);
$cmp = $this->doCmp($focus, $b);
if ($cmp === -1) {
if ($z === $x) { // remainder < dividend
break;
}
$z++;
}
$zeros = \str_repeat('0', $x - $z);
$q = $this->add($q, '1' . $zeros);
$a = $this->sub($a, $b . $zeros);
$r = $a;
if ($r === '0') { // remainder == 0
break;
}
$x = \strlen($a);
if ($x < $y) { // remainder < dividend
break;
}
$z = $y;
}
return [$q, $r];
}
/**
* Compares two non-signed large numbers.
*
* @param string $a The first operand.
* @param string $b The second operand.
*
* @return int [-1, 0, 1]
*/
private function doCmp(string $a, string $b) : int
{
$x = \strlen($a);
$y = \strlen($b);
$cmp = $x <=> $y;
if ($cmp !== 0) {
return $cmp;
}
return \strcmp($a, $b) <=> 0; // enforce [-1, 0, 1]
}
/**
* Pads the left of one of the given numbers with zeros if necessary to make both numbers the same length.
*
* The numbers must only consist of digits, without leading minus sign.
*
* @param string $a The first operand.
* @param string $b The second operand.
*
* @return array{string, string, int}
*/
private function pad(string $a, string $b) : array
{
$x = \strlen($a);
$y = \strlen($b);
if ($x > $y) {
$b = \str_repeat('0', $x - $y) . $b;
return [$a, $b, $x];
}
if ($x < $y) {
$a = \str_repeat('0', $y - $x) . $a;
return [$a, $b, $y];
}
return [$a, $b, $x];
}
}

107
vendor/brick/math/src/RoundingMode.php vendored Normal file
View File

@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace Brick\Math;
/**
* Specifies a rounding behavior for numerical operations capable of discarding precision.
*
* Each rounding mode indicates how the least significant returned digit of a rounded result
* is to be calculated. If fewer digits are returned than the digits needed to represent the
* exact numerical result, the discarded digits will be referred to as the discarded fraction
* regardless the digits' contribution to the value of the number. In other words, considered
* as a numerical value, the discarded fraction could have an absolute value greater than one.
*/
final class RoundingMode
{
/**
* Private constructor. This class is not instantiable.
*
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Asserts that the requested operation has an exact result, hence no rounding is necessary.
*
* If this rounding mode is specified on an operation that yields a result that
* cannot be represented at the requested scale, a RoundingNecessaryException is thrown.
*/
public const UNNECESSARY = 0;
/**
* Rounds away from zero.
*
* Always increments the digit prior to a nonzero discarded fraction.
* Note that this rounding mode never decreases the magnitude of the calculated value.
*/
public const UP = 1;
/**
* Rounds towards zero.
*
* Never increments the digit prior to a discarded fraction (i.e., truncates).
* Note that this rounding mode never increases the magnitude of the calculated value.
*/
public const DOWN = 2;
/**
* Rounds towards positive infinity.
*
* If the result is positive, behaves as for UP; if negative, behaves as for DOWN.
* Note that this rounding mode never decreases the calculated value.
*/
public const CEILING = 3;
/**
* Rounds towards negative infinity.
*
* If the result is positive, behave as for DOWN; if negative, behave as for UP.
* Note that this rounding mode never increases the calculated value.
*/
public const FLOOR = 4;
/**
* Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
*
* Behaves as for UP if the discarded fraction is >= 0.5; otherwise, behaves as for DOWN.
* Note that this is the rounding mode commonly taught at school.
*/
public const HALF_UP = 5;
/**
* Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
*
* Behaves as for UP if the discarded fraction is > 0.5; otherwise, behaves as for DOWN.
*/
public const HALF_DOWN = 6;
/**
* Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round towards positive infinity.
*
* If the result is positive, behaves as for HALF_UP; if negative, behaves as for HALF_DOWN.
*/
public const HALF_CEILING = 7;
/**
* Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round towards negative infinity.
*
* If the result is positive, behaves as for HALF_DOWN; if negative, behaves as for HALF_UP.
*/
public const HALF_FLOOR = 8;
/**
* Rounds towards the "nearest neighbor" unless both neighbors are equidistant, in which case rounds towards the even neighbor.
*
* Behaves as for HALF_UP if the digit to the left of the discarded fraction is odd;
* behaves as for HALF_DOWN if it's even.
*
* Note that this is the rounding mode that statistically minimizes
* cumulative error when applied repeatedly over a sequence of calculations.
* It is sometimes known as "Banker's rounding", and is chiefly used in the USA.
*/
public const HALF_EVEN = 9;
}

View File

@@ -0,0 +1,3 @@
*.php linguist-language=PHP
*.js linguist-language=PHP
*.vue linguist-language=PHP

View File

@@ -0,0 +1,11 @@
/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log

View File

@@ -0,0 +1,22 @@
filter:
excluded_paths: [tests/*]
checks:
php:
code_rating: true
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true
tools:
external_code_coverage:
timeout: 600
runs: 2

View File

@@ -0,0 +1,20 @@
language: php
php:
- '7.2'
- '7.3'
before_script:
- travis_retry composer self-update
- travis_retry composer update --prefer-lowest --prefer-source --no-interaction
script:
- phpunit --coverage-text --coverage-clover=coverage.clover
after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
notifications:
on_success: never
on_failure: always

View File

@@ -0,0 +1,21 @@
# MIT License
Copyright (c) 2019 Md. Abu Ahsan Basir
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,92 @@
[![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://badges.mit-license.org)
[![Build Status](https://travis-ci.org/Codexshaper/laravel-woocommerce.svg?branch=master)](https://travis-ci.org/Codexshaper/laravel-woocommerce)
[![StyleCI](https://github.styleci.io/repos/180436811/shield?branch=master)](https://github.styleci.io/repos/180436811)
[![Quality Score](https://img.shields.io/scrutinizer/g/Codexshaper/laravel-woocommerce.svg?style=flat-square)](https://scrutinizer-ci.com/g/Codexshaper/laravel-woocommerce)
[![Downloads](https://poser.pugx.org/Codexshaper/laravel-woocommerce/d/total.svg)](https://packagist.org/packages/Codexshaper/laravel-woocommerce)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/Codexshaper/laravel-woocommerce.svg?style=flat-square)](https://packagist.org/packages/Codexshaper/laravel-woocommerce)
# Description
WooCommerce Rest API for Laravel. You can Get, Create, Update and Delete your woocommerce product using this package easily.
[Documentation](https://codexshaper.github.io/docs/laravel-woocommerce/)
## Authors
* **Md Abu Ahsan Basir** - [github](https://github.com/maab16)
## License
- **[MIT license](http://opensource.org/licenses/mit-license.php)**
- Copyright 2020 © <a href="https://github.com/Codexshaper/laravel-woocommerce/blob/master/LICENSE" target="_blank">CodexShaper</a>.
# Eloquent Style for Product, Customer and Order
```
// Where passing multiple parameters
$products = Product::where('title','hello')->get();
OR
// You can call field with where clause
$products = Product::whereTitle('hello')->get();
// Fields name are more than one words or seperate by underscore (_). For example field name is `min_price`
$products = Product::whereMinPrice(5)->get();
// Where passing an array
$orders = Order::where(['status' => 'processing']);
$orders = Order::where(['status' => 'processing', 'orderby' => 'id', 'order' => 'asc'])->get();
// Set Options
$orders = Order::options(['status' => 'processing', 'orderby' => 'id', 'order' => 'asc'])->get();
// You can set options by passing an array when call `all` method
$orders = Order::all(['status' => 'processing', 'orderby' => 'id', 'order' => 'asc']);
```
#Product Options: https://woocommerce.github.io/woocommerce-rest-api-docs/#products
#Customer Options: https://woocommerce.github.io/woocommerce-rest-api-docs/#customers
#Order Options: https://woocommerce.github.io/woocommerce-rest-api-docs/#orders
# You can also use ```WooCommerce``` Facade
```
use Codexshaper\WooCommerce\Facades\WooCommerce;
public function products()
{
return WooCommerce::all('products');
}
public function product( Request $request )
{
$product = WooCommerce::find('products/'.$request->id);
}
public function orders()
{
return WooCommerce::all('orders');
}
public function order( Request $request )
{
$order = WooCommerce::all('orders/'.$request->id);
}
public function customers()
{
return WooCommerce::all('customers');
}
public function customer( Request $request )
{
$customer = WooCommerce::all('customers/'.$request->id);
}
```
# Use Facade Alias
```
use WooCommerce // Same as use Codexshaper\WooCommerce\Facades\WooCommerce;
use Customer // Same as use Codexshaper\WooCommerce\Models\Customer;
use Order // Same as use Codexshaper\WooCommerce\Models\Order;
use Product // Same as Codexshaper\WooCommerce\Models\Product;
```

View File

@@ -0,0 +1,67 @@
{
"name": "codexshaper/laravel-woocommerce",
"description": "WooCommerce Rest API for Laravel",
"keywords": [
"woocommerce",
"laravel",
"laravel-woocommerce",
"woocommerce-api",
"woocommerce-rest-api",
"laravel-woocommerce-api",
"laravel-woocommerce-rest-api"
],
"type": "library",
"require": {
"automattic/woocommerce": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0|^8.0|^9.3",
"illuminate/support": "~5.5.40|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0"
},
"license": "MIT",
"authors": [
{
"name": "Md Abu Ahsan Basir",
"email": "maab.career@gmail.com"
}
],
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Codexshaper\\WooCommerce\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Codexshaper\\WooCommerce\\WooCommerceServiceProvider"
],
"aliases": {
"Attribute": "Codexshaper\\WooCommerce\\Models\\Attribute",
"Category": "Codexshaper\\WooCommerce\\Models\\Category",
"Coupon": "Codexshaper\\WooCommerce\\Models\\Coupon",
"Customer": "Codexshaper\\WooCommerce\\Models\\Customer",
"Note": "Codexshaper\\WooCommerce\\Models\\Note",
"Order": "Codexshaper\\WooCommerce\\Models\\Order",
"PaymentGateway": "Codexshaper\\WooCommerce\\Facades\\PaymentGateway",
"Product": "Codexshaper\\WooCommerce\\Models\\Product",
"Refund": "Codexshaper\\WooCommerce\\Models\\Refund",
"Report": "Codexshaper\\WooCommerce\\Models\\Report",
"Review": "Codexshaper\\WooCommerce\\Models\\Review",
"Setting": "Codexshaper\\WooCommerce\\Models\\Setting",
"ShippingMethod": "Codexshaper\\WooCommerce\\Models\\ShippingMethod",
"ShippingZone": "Codexshaper\\WooCommerce\\Models\\ShippingZone",
"ShippingZoneMethod": "Codexshaper\\WooCommerce\\Models\\ShippingZoneMethod",
"System": "Codexshaper\\WooCommerce\\Models\\System",
"Tag": "Codexshaper\\WooCommerce\\Models\\Tag",
"Tax": "Codexshaper\\WooCommerce\\Models\\Tax",
"TaxClass": "Codexshaper\\WooCommerce\\Models\\TaxClass",
"Term": "Codexshaper\\WooCommerce\\Models\\Term",
"Variation": "Codexshaper\\WooCommerce\\Models\\Variation",
"Webhook": "Codexshaper\\WooCommerce\\Facades\\Webhook",
"WooCommerce": "Codexshaper\\WooCommerce\\Facades\\WooCommerce",
"Query": "Codexshaper\\WooCommerce\\Facades\\Query"
}
}
}
}

66
vendor/codexshaper/laravel-woocommerce/composer.lock generated vendored Normal file
View File

@@ -0,0 +1,66 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "5673c61b72915f0a71a70d93634cb73d",
"packages": [
{
"name": "automattic/woocommerce",
"version": "3.0.0",
"source": {
"type": "git",
"url": "https://github.com/woocommerce/wc-api-php.git",
"reference": "a71aa95cc3de3f1d68c6303525d03c0557a96137"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/woocommerce/wc-api-php/zipball/a71aa95cc3de3f1d68c6303525d03c0557a96137",
"reference": "a71aa95cc3de3f1d68c6303525d03c0557a96137",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"php": ">= 5.4.0"
},
"require-dev": {
"phpunit/phpunit": "*",
"squizlabs/php_codesniffer": "3.*"
},
"type": "library",
"autoload": {
"psr-4": {
"Automattic\\WooCommerce\\": [
"src/WooCommerce"
]
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Claudio Sanches",
"email": "claudio.sanches@automattic.com"
}
],
"description": "A PHP wrapper for the WooCommerce REST API",
"keywords": [
"api",
"woocommerce"
],
"time": "2019-01-16T20:28:40+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Attribute extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Attribute';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Category extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Category';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Coupon extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Coupon';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Customer extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Customer';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Note extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Note';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Order extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Order';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class PaymentGateway extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\PaymentGateway';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Product extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Product';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Query extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Query';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Refund extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Refund';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Report extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Report';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Review extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Review';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Setting extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Setting';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class ShippingMethod extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\ShippingMethod';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class ShippingZone extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\ShippingZone';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class ShippingZoneMethod extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\ShippingZoneMethod';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class System extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\System';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Tag extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Tag';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Tax extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Tax';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class TaxClass extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\TaxClass';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Term extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Term';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Variation extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Variation';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Illuminate\Support\Facades\Facade;
class Webhook extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\WooCommerce\Models\Webhook';
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Codexshaper\WooCommerce\Facades;
use Codexshaper\WooCommerce\WooCommerceApi;
use Illuminate\Support\Facades\Facade;
class WooCommerce extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return WooCommerceApi::class;
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Codexshaper\Woocommerce\Facades;
use Illuminate\Support\Facades\Facade;
class WoocommerceFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Codexshaper\Woocommerce\WooCommerceApi';
}
}

View File

@@ -0,0 +1,106 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Facades\Query;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Attribute extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'products/attributes';
/**
* Retrieve all Items.
*
* @param int $attribute_id
* @param array $options
*
* @return array
*/
protected function getTerms($attribute_id, $options = [])
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->all($options);
}
/**
* Retrieve single Item.
*
* @param int $attribute_id
* @param int $term_id
* @param array $options
*
* @return object
*/
protected function getTerm($attribute_id, $term_id, $options = [])
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->find($term_id, $options);
}
/**
* Create new Item.
*
* @param int $attribute_id
* @param array $data
*
* @return object
*/
protected function addTerm($attribute_id, $data)
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->create($data);
}
/**
* Update Existing Item.
*
* @param int $attribute_id
* @param int $term_id
* @param array $data
*
* @return object
*/
protected function updateTerm($attribute_id, $term_id, $data)
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->update($term_id, $data);
}
/**
* Destroy Item.
*
* @param int $attribute_id
* @param int $term_id
* @param array $options
*
* @return object
*/
protected function deleteTerm($attribute_id, $term_id, $options = [])
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->delete($term_id, $options);
}
/**
* Batch Update.
*
* @param int $attribute_id
* @param array $data
*
* @return object
*/
protected function batchTerm($attribute_id, $data)
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->batch($data);
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Codexshaper\WooCommerce\Models;
class BaseModel
{
protected $properties = [];
/**
* Get Inaccessible Property.
*
* @param string $name
*
* @return int|string|array|object|null
*/
public function __get($name)
{
return $this->$name;
}
/**
* Set Option.
*
* @param string $name
* @param string $value
*
* @return void
*/
public function __set($name, $value)
{
$this->properties[$name] = $value;
}
public function __call($method, $parameters)
{
if (!method_exists($this, $method)) {
preg_match_all('/((?:^|[A-Z])[a-z]+)/', $method, $partials);
$method = array_shift($partials[0]);
if (!method_exists($this, $method)) {
throw new \Exception('Sorry! you are calling wrong method');
}
array_unshift($parameters, strtolower(implode('_', $partials[0])));
}
return $this->$method(...$parameters);
}
public static function __callStatic($method, $parameters)
{
return (new static())->$method(...$parameters);
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Category extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'products/categories';
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Coupon extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'coupons';
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Facades\Query;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Customer extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'customers';
/**
* Download.
*
* @param int $id
*
* @return object
*/
protected function downloads($id, $options = [])
{
return Query::init()
->setEndpoint("customers/{$id}/downloads")
->all($options);
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Facades\Query;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Note extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint;
/**
* Retrieve all Items.
*
* @param int $order_id
* @param array $options
*
* @return array
*/
protected function all($order_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/notes")
->all($options);
}
/**
* Retrieve single Item.
*
* @param int $order_id
* @param int $note_id
* @param array $options
*
* @return object
*/
protected function find($order_id, $note_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/notes")
->find($note_id, $options);
}
/**
* Create new Item.
*
* @param int $order_id
* @param array $data
*
* @return object
*/
protected function create($order_id, $data)
{
return Query::init()
->setEndpoint("orders/{$order_id}/notes")
->create($data);
}
/**
* Destroy Item.
*
* @param int $order_id
* @param int $note_id
* @param array $options
*
* @return object
*/
protected function delete($order_id, $note_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/notes")
->delete($note_id, $options);
}
/**
* Paginate results.
*
*
* @param int $order_id
* @param int $per_page
* @param int $current_page
* @param array $options
*
* @return array
*/
protected function paginate(
$order_id,
$per_page = 10,
$current_page = 1,
$options = []
) {
return Query::init()
->setEndpoint("orders/{$order_id}/notes")
->paginate($per_page, $current_page, $options);
}
/**
* Count all results.
*
* @param int $order_id
*
* @return int
*/
protected function count($order_id)
{
return Query::init()
->setEndpoint("orders/{$order_id}/notes")
->count();
}
/**
* Store data.
*
* @param int $order_id
*
* @return array
*/
public function save($order_id)
{
return Query::init()
->setEndpoint("orders/{$order_id}/notes")
->save();
}
}

View File

@@ -0,0 +1,137 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Facades\Query;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Order extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'orders';
/**
* Retrieve all notes.
*
* @param int $order_id
* @param array $options
*
* @return array
*/
protected function notes($order_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/notes")
->all($options);
}
/**
* Retreive a note.
*
* @param int $order_id
* @param int $note_id
* @param array $options
*
* @return object
*/
protected function note($order_id, $note_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/notes")
->find($note_id, $options);
}
/**
* Create a note.
*
* @param int $order_id
* @param array $data
*
* @return object
*/
protected function createNote($order_id, $data = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/notes")
->create($data);
}
/**
* Delete a note.
*
* @param int $order_id
* @param int $note_id
* @param array $options
*
* @return object
*/
protected function deleteNote($order_id, $note_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/notes")
->delete($note_id, $options);
}
/**
* Retrieve all refunds.
*
* @param int $order_id
* @param array $options
*
* @return array
*/
protected function refunds($order_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/refunds")
->all($options);
}
/**
* Retrieve a refund.
*
* @param int $order_id
* @param int $refund_id
* @param array $options
*
* @return object
*/
protected function refund($order_id, $refund_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/refunds")
->find($refund_id, $options);
}
/**
* Create refund.
*
* @param int $order_id
* @param array $data
*
* @return object
*/
protected function createRefund($order_id, $data = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/refunds")
->create($data);
}
/**
* Delete refund.
*
* @param int $order_id
* @param int $refund_id
* @param array $options
*
* @return object
*/
protected function deleteRefund($order_id, $refund_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/refunds")
->delete($refund_id, $options);
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class PaymentGateway extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'payment_gateways';
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Product extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'products';
}

View File

@@ -0,0 +1,124 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Facades\Query;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Refund extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint;
/**
* Retrieve all Items.
*
* @param int $order_id
* @param array $options
*
* @return array
*/
protected function all($order_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/refunds")
->all($options);
}
/**
* Retrieve single Item.
*
* @param int $order_id
* @param int $refund_id
* @param array $options
*
* @return object
*/
protected function find($order_id, $refund_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/refunds")
->find($refund_id, $options);
}
/**
* Create new Item.
*
* @param int $order_id
* @param array $data
*
* @return object
*/
protected function create($order_id, $data)
{
return Query::init()
->setEndpoint("orders/{$order_id}/refunds")
->create($data);
}
/**
* Destroy Item.
*
* @param int $order_id
* @param int $refund_id
* @param array $options
*
* @return object
*/
protected function delete($order_id, $refund_id, $options = [])
{
return Query::init()
->setEndpoint("orders/{$order_id}/refunds")
->delete($refund_id, $options);
}
/**
* Paginate results.
*
* @param int $order_id
* @param int $per_page
* @param int $current_page
* @param array $options
*
* @return array
*/
protected function paginate(
$order_id,
$per_page = 10,
$current_page = 1,
$options = []
) {
return Query::init()
->setEndpoint("orders/{$order_id}/refunds")
->paginate($per_page, $current_page, $options);
}
/**
* Count all results.
*
* @param int $order_id
*
* @return int
*/
protected function count($order_id)
{
return Query::init()
->setEndpoint("orders/{$order_id}/refunds")
->count();
}
/**
* Store data.
*
* @param int $order_id
*
* @return array
*/
public function save($order_id)
{
return Query::init()
->setEndpoint("orders/{$order_id}/refunds")
->save();
}
}

View File

@@ -0,0 +1,111 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Facades\Query;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Report extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'reports';
/**
* Retrieve all sales.
*
* @param array $options
*
* @return array
*/
protected function sales($options = [])
{
return Query::init()
->setEndpoint('reports/sales')
->all($options);
}
/**
* Retrieve all top sellers.
*
* @param array $options
*
* @return array
*/
protected function topSellers($options = [])
{
return Query::init()
->setEndpoint('reports/top_sellers')
->all($options);
}
/**
* Retrieve all coupons.
*
* @param array $options
*
* @return array
*/
protected function coupons($options = [])
{
return Query::init()
->setEndpoint('reports/coupons/totals')
->all($options);
}
/**
* Retrieve all customers.
*
* @param array $options
*
* @return array
*/
protected function customers($options = [])
{
return Query::init()
->setEndpoint('reports/customers/totals')
->all($options);
}
/**
* Retrieve all orders.
*
* @param array $options
*
* @return array
*/
protected function orders($options = [])
{
return Query::init()
->setEndpoint('reports/orders/totals')
->all($options);
}
/**
* Retrieve all products.
*
* @param array $options
*
* @return array
*/
protected function products($options = [])
{
return Query::init()
->setEndpoint('reports/products/totals')
->all($options);
}
/**
* Retrieve all reviews.
*
* @param array $options
*
* @return array
*/
protected function reviews($options = [])
{
return Query::init()
->setEndpoint('reports/reviews/totals')
->all($options);
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Review extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'products/reviews';
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Facades\Query;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Setting extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'settings';
/**
* Retrieve option.
*
* @param int $group_id
* @param int $id
* @param array $options
*
* @return array
*/
protected function option($group_id, $id, $options = [])
{
return Query::init()
->setEndpoint("settings/{$group_id}")
->find($id, $options);
}
/**
* Retrieve options.
*
* @param int $id
* @param array $options
*
* @return array
*/
protected function options($id, $options = [])
{
return Query::init()
->setEndpoint('settings')
->find($id, $options);
}
/**
* Update Existing Item.
*
* @param int $group_id
* @param int $id
* @param array $data
*
* @return object
*/
protected function update($group_id, $id, $data)
{
return Query::init()
->setEndpoint("settings/{$group_id}")
->update($id, $data);
}
/**
* Batch Update.
*
* @param array $data
*
* @return object
*/
protected function batch($id, $data)
{
return Query::init()
->setEndpoint("settings/{$id}")
->batch($data);
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class ShippingMethod extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'shipping_methods';
}

View File

@@ -0,0 +1,120 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Facades\Query;
use Codexshaper\WooCommerce\Facades\WooCommerce;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class ShippingZone extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'shipping/zones';
/**
* Retrieve all Items.
*
* @param int $id
* @param array $options
*
* @return array
*/
protected function getLocations($id, $options = [])
{
return Query::init()
->setEndpoint("shipping/zones/{$id}/locations")
->all($options);
}
/**
* Update Existing Item.
*
* @param int $id
* @param array $data
*
* @return object
*/
protected function updateLocations($id, $data = [])
{
return WooCommerce::update("shipping/zones/{$id}/locations", $data);
}
/**
* Create new Item.
*
* @param int $id
* @param array $data
*
* @return object
*/
protected function addShippingZoneMethod($id, $data)
{
return Query::init()
->setEndpoint("shipping/zones/{$id}/methods")
->create($data);
}
/**
* Retrieve single Item.
*
* @param int $zone_id
* @param int $id
* @param array $options
*
* @return object
*/
protected function getShippingZoneMethod($zone_id, $id, $options = [])
{
return Query::init()
->setEndpoint("shipping/zones/{$zone_id}/methods")
->find($id, $options);
}
/**
* Retrieve all Items.
*
* @param int $id
* @param array $options
*
* @return array
*/
protected function getShippingZoneMethods($id, $options = [])
{
return Query::init()
->setEndpoint("shipping/zones/{$id}/methods")
->all($options);
}
/**
* Update Existing Item.
*
* @param int $zone_id
* @param int $id
* @param array $data
*
* @return object
*/
protected function updateShippingZoneMethod($zone_id, $id, $data = [])
{
return Query::init()
->setEndpoint("shipping/zones/{$zone_id}/methods")
->update($id, $data);
}
/**
* Destroy Item.
*
* @param int $zone_id
* @param int $id
* @param array $options
*
* @return object
*/
protected function deleteShippingZoneMethod($zone_id, $id, $options = [])
{
return Query::init()
->setEndpoint("shipping/zones/{$zone_id}/methods")
->delete($id, $options);
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Facades\Query;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class System extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint;
/**
* Retrieve all Items.
*
* @param array $options
*
* @return array
*/
protected function status($options = [])
{
return Query::init()
->setEndpoint('system_status')
->all($options);
}
/**
* Retrieve single tool.
*
* @param int $id
* @param array $options
*
* @return object
*/
protected function tool($id, $options = [])
{
return Query::init()
->setEndpoint('system_status/tools')
->find($id, $options);
}
/**
* Retrieve all tools.
*
* @param array $options
*
* @return array
*/
protected function tools($options = [])
{
return Query::init()
->setEndpoint('system_status/tools')
->all($options);
}
/**
* Run tool.
*
* @param int $id
* @param array $data
*
* @return object
*/
protected function run($id, $data)
{
return Query::init()
->setEndpoint('system_status/tools')
->update($id, $data);
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Tag extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'products/tags';
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Tax extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'taxes';
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class TaxClass extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'taxes/classes';
}

View File

@@ -0,0 +1,155 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Facades\Query;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Term extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint;
/**
* Retrieve all Items.
*
* @param int $attribute_id
* @param array $options
*
* @return array
*/
protected function all($attribute_id, $options = [])
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->all($options);
}
/**
* Retrieve single Item.
*
* @param int $attribute_id
* @param int $id
* @param array $options
*
* @return object
*/
protected function find($attribute_id, $id, $options = [])
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->find($id, $options);
}
/**
* Create new Item.
*
* @param int $attribute_id
* @param array $data
*
* @return object
*/
protected function create($attribute_id, $data)
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->create($data);
}
/**
* Update Existing Item.
*
* @param int $attribute_id
* @param int $id
* @param array $data
*
* @return object
*/
protected function update($attribute_id, $id, $data)
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->update($id, $data);
}
/**
* Destroy Item.
*
* @param int $attribute_id
* @param int $id
* @param array $options
*
* @return object
*/
protected function delete($attribute_id, $id, $options = [])
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->delete($id, $options);
}
/**
* Batch Update.
*
* @param int $attribute_id
* @param array $data
*
* @return object
*/
protected function batch($attribute_id, $data)
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->batch($data);
}
/**
* Paginate results.
*
* @param int $attribute_id
* @param int $per_page
* @param int $current_page
* @param array $options
*
* @return array
*/
protected function paginate(
$attribute_id,
$per_page = 10,
$current_page = 1,
$options = []
) {
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->paginate($per_page, $current_page, $options);
}
/**
* Count all results.
*
* @param int $attribute_id
*
* @return int
*/
protected function count($attribute_id)
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->count();
}
/**
* Store data.
*
* @param int $attribute_id
*
* @return array
*/
public function save($attribute_id)
{
return Query::init()
->setEndpoint("products/attributes/{$attribute_id}/terms")
->save();
}
}

View File

@@ -0,0 +1,155 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Facades\Query;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Variation extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint;
/**
* Retrieve all Items.
*
* @param int $product_id
* @param array $options
*
* @return array
*/
protected function all($product_id, $options = [])
{
return Query::init()
->setEndpoint("products/{$product_id}/variations")
->all($options);
}
/**
* Retrieve single Item.
*
* @param int $product_id
* @param int $id
* @param array $options
*
* @return object
*/
protected function find($product_id, $id, $options = [])
{
return Query::init()
->setEndpoint("products/{$product_id}/variations")
->find($id, $options);
}
/**
* Create new Item.
*
* @param int $product_id
* @param array $data
*
* @return object
*/
protected function create($product_id, $data)
{
return Query::init()
->setEndpoint("products/{$product_id}/variations")
->create($data);
}
/**
* Update Existing Item.
*
* @param int $product_id
* @param int $id
* @param array $data
*
* @return object
*/
protected function update($product_id, $id, $data)
{
return Query::init()
->setEndpoint("products/{$product_id}/variations")
->update($id, $data);
}
/**
* Destroy Item.
*
* @param int $product_id
* @param int $id
* @param array $options
*
* @return object
*/
protected function delete($product_id, $id, $options = [])
{
return Query::init()
->setEndpoint("products/{$product_id}/variations")
->delete($id, $options);
}
/**
* Batch Update.
*
* @param int $product_id
* @param array $data
*
* @return object
*/
protected function batch($product_id, $data)
{
return Query::init()
->setEndpoint("products/{$product_id}/variations")
->batch($data);
}
/**
* Paginate results.
*
* @param int $product_id
* @param int $per_page
* @param int $current_page
* @param array $options
*
* @return array
*/
protected function paginate(
$product_id,
$per_page = 10,
$current_page = 1,
$options = []
) {
return Query::init()
->setEndpoint("products/{$product_id}/variations")
->paginate($per_page, $current_page, $options);
}
/**
* Count all results.
*
* @param int $product_id
*
* @return int
*/
protected function count($product_id)
{
return Query::init()
->setEndpoint("products/{$product_id}/variations")
->count();
}
/**
* Store data.
*
* @param int $product_id
*
* @return array
*/
public function save($product_id)
{
return Query::init()
->setEndpoint("products/{$product_id}/variations")
->save();
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Codexshaper\WooCommerce\Models;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Webhook extends BaseModel
{
use QueryBuilderTrait;
protected $endpoint = 'webhooks';
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Codexshaper\WooCommerce;
use Codexshaper\WooCommerce\Models\BaseMOdel;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
class Query extends BaseMOdel
{
use QueryBuilderTrait;
protected $endpoint;
protected static $instance = null;
public function __construct($endpoint = '')
{
$this->endpoint = $endpoint;
}
public function setEndpoint($endpoint)
{
$this->endpoint = $endpoint;
return $this;
}
public function init()
{
if (!static::$instance) {
static::$instance = new static();
}
return static::$instance;
}
}

Some files were not shown because too many files have changed in this diff Show More