Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

20
vendor/league/csv/LICENSE vendored Normal file
View File

@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2013-2015 ignace nyamagana butera
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.

21
vendor/league/csv/autoload.php vendored Normal file
View File

@@ -0,0 +1,21 @@
<?php
spl_autoload_register(function ($class) {
$prefix = 'League\Csv\\';
if (0 !== strpos($class, $prefix)) {
return;
}
$file = __DIR__
.DIRECTORY_SEPARATOR
.'src'
.DIRECTORY_SEPARATOR
.str_replace('\\', DIRECTORY_SEPARATOR, substr($class, strlen($prefix)))
.'.php';
if (!is_readable($file)) {
return;
}
require $file;
});

49
vendor/league/csv/composer.json vendored Normal file
View File

@@ -0,0 +1,49 @@
{
"name": "league/csv",
"type": "library",
"description" : "Csv data manipulation made easy in PHP",
"keywords": ["csv", "import", "export", "read", "write", "filter"],
"license": "MIT",
"homepage" : "http://csv.thephpleague.com",
"authors": [
{
"name" : "Ignace Nyamagana Butera",
"email" : "nyamsprod@gmail.com",
"homepage" : "https://github.com/nyamsprod/",
"role" : "Developer"
}
],
"support": {
"forum": "https://groups.google.com/forum/#!forum/thephpleague",
"issues": "https://github.com/thephpleague/csv/issues"
},
"require": {
"php" : ">=5.5.0",
"ext-mbstring" : "*"
},
"require-dev": {
"phpunit/phpunit" : "^4.0",
"friendsofphp/php-cs-fixer": "^1.9"
},
"autoload": {
"psr-4": {
"League\\Csv\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"League\\Csv\\Test\\": "test",
"lib\\": "examples\\lib"
}
},
"scripts": {
"test": "phpunit --coverage-text; php-cs-fixer fix -v --diff --dry-run;",
"phpunit": "phpunit --coverage-text",
"phpcs": "php-cs-fixer fix -v --diff --dry-run;"
},
"extra": {
"branch-alias": {
"dev-master": "8.2-dev"
}
}
}

273
vendor/league/csv/src/AbstractCsv.php vendored Normal file
View File

@@ -0,0 +1,273 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv;
use InvalidArgumentException;
use IteratorAggregate;
use JsonSerializable;
use League\Csv\Config\Controls;
use League\Csv\Config\Output;
use League\Csv\Modifier\QueryFilter;
use League\Csv\Modifier\StreamFilter;
use League\Csv\Modifier\StreamIterator;
use SplFileInfo;
use SplFileObject;
use SplTempFileObject;
/**
* An abstract class to enable basic CSV manipulation
*
* @package League.csv
* @since 4.0.0
*
*/
abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
{
use Controls;
use Output;
use QueryFilter;
use StreamFilter;
/**
* UTF-8 BOM sequence
*/
const BOM_UTF8 = "\xEF\xBB\xBF";
/**
* UTF-16 BE BOM sequence
*/
const BOM_UTF16_BE = "\xFE\xFF";
/**
* UTF-16 LE BOM sequence
*/
const BOM_UTF16_LE = "\xFF\xFE";
/**
* UTF-32 BE BOM sequence
*/
const BOM_UTF32_BE = "\x00\x00\xFE\xFF";
/**
* UTF-32 LE BOM sequence
*/
const BOM_UTF32_LE = "\xFF\xFE\x00\x00";
/**
* The path
*
* can be a StreamIterator object, a SplFileObject object or the string path to a file
*
* @var StreamIterator|SplFileObject|string
*/
protected $path;
/**
* The file open mode flag
*
* @var string
*/
protected $open_mode;
/**
* Creates a new instance
*
* The path must be an SplFileInfo object
* an object that implements the `__toString` method
* a path to a file
*
* @param StreamIterator|SplFileObject|string $path The file path
* @param string $open_mode The file open mode flag
*/
protected function __construct($path, $open_mode = 'r+')
{
$this->open_mode = strtolower($open_mode);
$this->path = $path;
$this->initStreamFilter($this->path);
}
/**
* The destructor
*/
public function __destruct()
{
$this->path = null;
}
/**
* Return a new {@link AbstractCsv} from a SplFileObject
*
* @param SplFileObject $file
*
* @return static
*/
public static function createFromFileObject(SplFileObject $file)
{
$csv = new static($file);
$controls = $file->getCsvControl();
$csv->setDelimiter($controls[0]);
$csv->setEnclosure($controls[1]);
if (isset($controls[2])) {
$csv->setEscape($controls[2]);
}
return $csv;
}
/**
* Return a new {@link AbstractCsv} from a PHP resource stream or a StreamIterator
*
* @param resource $stream
*
* @return static
*/
public static function createFromStream($stream)
{
return new static(new StreamIterator($stream));
}
/**
* Return a new {@link AbstractCsv} from a string
*
* The string must be an object that implements the `__toString` method,
* or a string
*
* @param string $str the string
*
* @return static
*/
public static function createFromString($str)
{
$file = new SplTempFileObject();
$file->fwrite(static::validateString($str));
return new static($file);
}
/**
* validate a string
*
* @param mixed $str the value to evaluate as a string
*
* @throws InvalidArgumentException if the submitted data can not be converted to string
*
* @return string
*/
protected static function validateString($str)
{
if (is_string($str) || (is_object($str) && method_exists($str, '__toString'))) {
return (string) $str;
}
throw new InvalidArgumentException('Expected data must be a string or stringable');
}
/**
* Return a new {@link AbstractCsv} from a file path
*
* @param mixed $path file path
* @param string $open_mode the file open mode flag
*
* @throws InvalidArgumentException If $path is a SplTempFileObject object
*
* @return static
*/
public static function createFromPath($path, $open_mode = 'r+')
{
if ($path instanceof SplTempFileObject) {
throw new InvalidArgumentException('an `SplTempFileObject` object does not contain a valid path');
}
if ($path instanceof SplFileInfo) {
$path = $path->getPath().'/'.$path->getBasename();
}
return new static(static::validateString($path), $open_mode);
}
/**
* Return a new {@link AbstractCsv} instance from another {@link AbstractCsv} object
*
* @param string $class the class to be instantiated
* @param string $open_mode the file open mode flag
*
* @return static
*/
protected function newInstance($class, $open_mode)
{
$csv = new $class($this->path, $open_mode);
$csv->delimiter = $this->delimiter;
$csv->enclosure = $this->enclosure;
$csv->escape = $this->escape;
$csv->input_encoding = $this->input_encoding;
$csv->input_bom = $this->input_bom;
$csv->output_bom = $this->output_bom;
$csv->newline = $this->newline;
return $csv;
}
/**
* Return a new {@link Writer} instance from a {@link AbstractCsv} object
*
* @param string $open_mode the file open mode flag
*
* @return Writer
*/
public function newWriter($open_mode = 'r+')
{
return $this->newInstance(Writer::class, $open_mode);
}
/**
* Return a new {@link Reader} instance from a {@link AbstractCsv} object
*
* @param string $open_mode the file open mode flag
*
* @return Reader
*/
public function newReader($open_mode = 'r+')
{
return $this->newInstance(Reader::class, $open_mode);
}
/**
* Returns the inner CSV Document Iterator object
*
* @return StreamIterator|SplFileObject
*/
public function getIterator()
{
$iterator = $this->setIterator();
$iterator->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
$iterator->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);
return $iterator;
}
/**
* Set the Inner Iterator
*
* @return StreamIterator|SplFileObject
*/
protected function setIterator()
{
if ($this->path instanceof StreamIterator || $this->path instanceof SplFileObject) {
return $this->path;
}
return new SplFileObject($this->getStreamFilterPath(), $this->open_mode);
}
}

View File

@@ -0,0 +1,235 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv\Config;
use CallbackFilterIterator;
use InvalidArgumentException;
use LimitIterator;
use SplFileObject;
/**
* A trait to configure and check CSV file and content
*
* @package League.csv
* @since 6.0.0
*
*/
trait Controls
{
/**
* the field delimiter (one character only)
*
* @var string
*/
protected $delimiter = ',';
/**
* the field enclosure character (one character only)
*
* @var string
*/
protected $enclosure = '"';
/**
* the field escape character (one character only)
*
* @var string
*/
protected $escape = '\\';
/**
* newline character
*
* @var string
*/
protected $newline = "\n";
/**
* Sets the field delimiter
*
* @param string $delimiter
*
* @throws InvalidArgumentException If $delimiter is not a single character
*
* @return $this
*/
public function setDelimiter($delimiter)
{
if (!$this->isValidCsvControls($delimiter)) {
throw new InvalidArgumentException('The delimiter must be a single character');
}
$this->delimiter = $delimiter;
return $this;
}
/**
* Tell whether the submitted string is a valid CSV Control character
*
* @param string $str The submitted string
*
* @return bool
*/
protected function isValidCsvControls($str)
{
return 1 == mb_strlen($str);
}
/**
* Returns the current field delimiter
*
* @return string
*/
public function getDelimiter()
{
return $this->delimiter;
}
/**
* Detect Delimiters occurences in the CSV
*
* Returns a associative array where each key represents
* a valid delimiter and each value the number of occurences
*
* @param string[] $delimiters the delimiters to consider
* @param int $nb_rows Detection is made using $nb_rows of the CSV
*
* @return array
*/
public function fetchDelimitersOccurrence(array $delimiters, $nb_rows = 1)
{
$nb_rows = $this->validateInteger($nb_rows, 1, 'The number of rows to consider must be a valid positive integer');
$filter_row = function ($row) {
return is_array($row) && count($row) > 1;
};
$delimiters = array_unique(array_filter($delimiters, [$this, 'isValidCsvControls']));
$csv = $this->getIterator();
$res = [];
foreach ($delimiters as $delim) {
$csv->setCsvControl($delim, $this->enclosure, $this->escape);
$iterator = new CallbackFilterIterator(new LimitIterator($csv, 0, $nb_rows), $filter_row);
$res[$delim] = count(iterator_to_array($iterator, false), COUNT_RECURSIVE);
}
arsort($res, SORT_NUMERIC);
return $res;
}
/**
* Validate an integer
*
* @param int $int
* @param int $minValue
* @param string $errorMessage
*
* @throws InvalidArgumentException If the value is invalid
*
* @return int
*/
protected function validateInteger($int, $minValue, $errorMessage)
{
if (false === ($int = filter_var($int, FILTER_VALIDATE_INT, ['options' => ['min_range' => $minValue]]))) {
throw new InvalidArgumentException($errorMessage);
}
return $int;
}
/**
* Returns the CSV Iterator
*
* @return SplFileObject
*/
abstract public function getIterator();
/**
* Sets the field enclosure
*
* @param string $enclosure
*
* @throws InvalidArgumentException If $enclosure is not a single character
*
* @return $this
*/
public function setEnclosure($enclosure)
{
if (!$this->isValidCsvControls($enclosure)) {
throw new InvalidArgumentException('The enclosure must be a single character');
}
$this->enclosure = $enclosure;
return $this;
}
/**
* Returns the current field enclosure
*
* @return string
*/
public function getEnclosure()
{
return $this->enclosure;
}
/**
* Sets the field escape character
*
* @param string $escape
*
* @throws InvalidArgumentException If $escape is not a single character
*
* @return $this
*/
public function setEscape($escape)
{
if (!$this->isValidCsvControls($escape)) {
throw new InvalidArgumentException('The escape character must be a single character');
}
$this->escape = $escape;
return $this;
}
/**
* Returns the current field escape character
*
* @return string
*/
public function getEscape()
{
return $this->escape;
}
/**
* Sets the newline sequence characters
*
* @param string $newline
*
* @return static
*/
public function setNewline($newline)
{
$this->newline = (string) $newline;
return $this;
}
/**
* Returns the current newline sequence characters
*
* @return string
*/
public function getNewline()
{
return $this->newline;
}
}

309
vendor/league/csv/src/Config/Output.php vendored Normal file
View File

@@ -0,0 +1,309 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv\Config;
use DomDocument;
use InvalidArgumentException;
use Iterator;
use League\Csv\AbstractCsv;
use League\Csv\Modifier\MapIterator;
use SplFileObject;
/**
* A trait to output CSV
*
* @package League.csv
* @since 6.3.0
*
*/
trait Output
{
/**
* Charset Encoding for the CSV
*
* @var string
*/
protected $input_encoding = 'UTF-8';
/**
* The Input file BOM character
* @var string
*/
protected $input_bom;
/**
* The Output file BOM character
* @var string
*/
protected $output_bom = '';
/**
* Sets the CSV encoding charset
*
* @param string $str
*
* @return static
*/
public function setInputEncoding($str)
{
$str = str_replace('_', '-', $str);
$str = filter_var($str, FILTER_SANITIZE_STRING, ['flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH]);
if (empty($str)) {
throw new InvalidArgumentException('you should use a valid charset');
}
$this->input_encoding = strtoupper($str);
return $this;
}
/**
* Sets the CSV encoding charset
*
* DEPRECATION WARNING! This method will be removed in the next major point release
*
* @deprecated deprecated since version 8.1
*
* @param string $str
*
* @return static
*/
public function setEncodingFrom($str)
{
return $this->setInputEncoding($str);
}
/**
* Gets the source CSV encoding charset
*
* @return string
*/
public function getInputEncoding()
{
return $this->input_encoding;
}
/**
* Gets the source CSV encoding charset
*
* DEPRECATION WARNING! This method will be removed in the next major point release
*
* @deprecated deprecated since version 8.1
*
* @return string
*/
public function getEncodingFrom()
{
return $this->getInputEncoding();
}
/**
* Sets the BOM sequence to prepend the CSV on output
*
* @param string $str The BOM sequence
*
* @return static
*/
public function setOutputBOM($str)
{
if (empty($str)) {
$this->output_bom = '';
return $this;
}
$this->output_bom = (string) $str;
return $this;
}
/**
* Returns the BOM sequence in use on Output methods
*
* @return string
*/
public function getOutputBOM()
{
return $this->output_bom;
}
/**
* Returns the BOM sequence of the given CSV
*
* @return string
*/
public function getInputBOM()
{
if (null === $this->input_bom) {
$bom = [
AbstractCsv::BOM_UTF32_BE, AbstractCsv::BOM_UTF32_LE,
AbstractCsv::BOM_UTF16_BE, AbstractCsv::BOM_UTF16_LE, AbstractCsv::BOM_UTF8,
];
$csv = $this->getIterator();
$csv->setFlags(SplFileObject::READ_CSV);
$csv->rewind();
$line = $csv->fgets();
$res = array_filter($bom, function ($sequence) use ($line) {
return strpos($line, $sequence) === 0;
});
$this->input_bom = (string) array_shift($res);
}
return $this->input_bom;
}
/**
* @inheritdoc
*/
abstract public function getIterator();
/**
* Outputs all data on the CSV file
*
* @param string $filename CSV downloaded name if present adds extra headers
*
* @return int Returns the number of characters read from the handle
* and passed through to the output.
*/
public function output($filename = null)
{
if (null !== $filename) {
$filename = filter_var($filename, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
header('Content-Type: text/csv');
header('Content-Transfer-Encoding: binary');
header("Content-Disposition: attachment; filename=\"$filename\"");
}
return $this->fpassthru();
}
/**
* Outputs all data from the CSV
*
* @return int Returns the number of characters read from the handle
* and passed through to the output.
*/
protected function fpassthru()
{
$bom = '';
$input_bom = $this->getInputBOM();
if ($this->output_bom && $input_bom != $this->output_bom) {
$bom = $this->output_bom;
}
$csv = $this->getIterator();
$csv->setFlags(SplFileObject::READ_CSV);
$csv->rewind();
if (!empty($bom)) {
$csv->fseek(mb_strlen($input_bom));
}
echo $bom;
$res = $csv->fpassthru();
return $res + strlen($bom);
}
/**
* Retrieves the CSV content
*
* @return string
*/
public function __toString()
{
ob_start();
$this->fpassthru();
return ob_get_clean();
}
/**
* @inheritdoc
*/
public function jsonSerialize()
{
return iterator_to_array($this->convertToUtf8($this->getQueryIterator()), false);
}
/**
* Returns the CSV Iterator
*
* @return Iterator
*/
abstract protected function getQueryIterator();
/**
* Convert Csv file into UTF-8
*
* @param Iterator $iterator
*
* @return Iterator
*/
protected function convertToUtf8(Iterator $iterator)
{
if (stripos($this->input_encoding, 'UTF-8') !== false) {
return $iterator;
}
$convert_cell = function ($value) {
return mb_convert_encoding($value, 'UTF-8', $this->input_encoding);
};
$convert_row = function (array $row) use ($convert_cell) {
return array_map($convert_cell, $row);
};
return new MapIterator($iterator, $convert_row);
}
/**
* Returns a HTML table representation of the CSV Table
*
* @param string $class_attr optional classname
*
* @return string
*/
public function toHTML($class_attr = 'table-csv-data')
{
$doc = $this->toXML('table', 'tr', 'td');
$doc->documentElement->setAttribute('class', $class_attr);
return $doc->saveHTML($doc->documentElement);
}
/**
* Transforms a CSV into a XML
*
* @param string $root_name XML root node name
* @param string $row_name XML row node name
* @param string $cell_name XML cell node name
*
* @return DomDocument
*/
public function toXML($root_name = 'csv', $row_name = 'row', $cell_name = 'cell')
{
$doc = new DomDocument('1.0', 'UTF-8');
$root = $doc->createElement($root_name);
foreach ($this->convertToUtf8($this->getQueryIterator()) as $row) {
$rowElement = $doc->createElement($row_name);
array_walk($row, function ($value) use (&$rowElement, $doc, $cell_name) {
$content = $doc->createTextNode($value);
$cell = $doc->createElement($cell_name);
$cell->appendChild($content);
$rowElement->appendChild($cell);
});
$root->appendChild($rowElement);
}
$doc->appendChild($root);
return $doc;
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv\Exception;
use InvalidArgumentException;
/**
* Thrown when a data is not validated prior to insertion
*
* @package League.csv
* @since 7.0.0
*
*/
class InvalidRowException extends InvalidArgumentException
{
/**
* Validator which did not validated the data
* @var string
*/
private $name;
/**
* Validator Data which caused the error
* @var array
*/
private $data;
/**
* New Instance
*
* @param string $name validator name
* @param array $data invalid data
* @param string $message exception message
*/
public function __construct($name, array $data = [], $message = '')
{
parent::__construct($message);
$this->name = $name;
$this->data = $data;
}
/**
* return the validator name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* return the invalid data submitted
*
* @return array
*/
public function getData()
{
return $this->data;
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv\Modifier;
use Iterator;
use IteratorIterator;
/**
* A simple MapIterator
*
* @package League.csv
* @since 3.3.0
* @internal used internally to modify CSV content
*
*/
class MapIterator extends IteratorIterator
{
/**
* The function to be apply on all InnerIterator element
*
* @var callable
*/
private $callable;
/**
* The Constructor
*
* @param Iterator $iterator
* @param callable $callable
*/
public function __construct(Iterator $iterator, callable $callable)
{
parent::__construct($iterator);
$this->callable = $callable;
}
/**
* Get the value of the current element
*/
public function current()
{
$iterator = $this->getInnerIterator();
return call_user_func($this->callable, $iterator->current(), $iterator->key(), $iterator);
}
}

View File

@@ -0,0 +1,298 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv\Modifier;
use ArrayIterator;
use CallbackFilterIterator;
use Iterator;
use LimitIterator;
/**
* A Trait to Query rows against a SplFileObject
*
* @package League.csv
* @since 4.2.1
*
*/
trait QueryFilter
{
/**
* Callables to filter the iterator
*
* @var callable[]
*/
protected $iterator_filters = [];
/**
* Callables to sort the iterator
*
* @var callable[]
*/
protected $iterator_sort_by = [];
/**
* iterator Offset
*
* @var int
*/
protected $iterator_offset = 0;
/**
* iterator maximum length
*
* @var int
*/
protected $iterator_limit = -1;
/**
* Stripping BOM status
*
* @var boolean
*/
protected $strip_bom = false;
/**
* Stripping BOM setter
*
* @param bool $status
*
* @return $this
*/
public function stripBom($status)
{
$this->strip_bom = (bool) $status;
return $this;
}
/**
* @inheritdoc
*/
abstract public function getInputBOM();
/**
* Set LimitIterator Offset
*
* @param $offset
*
* @return $this
*/
public function setOffset($offset = 0)
{
$this->iterator_offset = $this->validateInteger($offset, 0, 'the offset must be a positive integer or 0');
return $this;
}
/**
* @inheritdoc
*/
abstract protected function validateInteger($int, $minValue, $errorMessage);
/**
* Set LimitIterator Count
*
* @param int $limit
*
* @return $this
*/
public function setLimit($limit = -1)
{
$this->iterator_limit = $this->validateInteger($limit, -1, 'the limit must an integer greater or equals to -1');
return $this;
}
/**
* Set an Iterator sorting callable function
*
* @param callable $callable
*
* @return $this
*/
public function addSortBy(callable $callable)
{
$this->iterator_sort_by[] = $callable;
return $this;
}
/**
* Set the Iterator filter method
*
* @param callable $callable
*
* @return $this
*/
public function addFilter(callable $callable)
{
$this->iterator_filters[] = $callable;
return $this;
}
/**
* @inheritdoc
*/
abstract public function getEnclosure();
/**
* Returns the CSV Iterator
*
* @return Iterator
*/
protected function getQueryIterator()
{
$normalizedCsv = function ($row) {
return is_array($row) && $row != [null];
};
array_unshift($this->iterator_filters, $normalizedCsv);
$iterator = $this->getIterator();
$iterator = $this->applyBomStripping($iterator);
$iterator = $this->applyIteratorFilter($iterator);
$iterator = $this->applyIteratorSortBy($iterator);
$iterator = $this->applyIteratorInterval($iterator);
return $iterator;
}
/**
* @inheritdoc
*/
abstract public function getIterator();
/**
* Remove the BOM sequence from the CSV
*
* @param Iterator $iterator
*
* @return Iterator
*/
protected function applyBomStripping(Iterator $iterator)
{
if (!$this->strip_bom) {
return $iterator;
}
if (!$this->isBomStrippable()) {
$this->strip_bom = false;
return $iterator;
}
$this->strip_bom = false;
return $this->getStripBomIterator($iterator);
}
/**
* Tell whether we can strip or not the leading BOM sequence
*
* @return bool
*/
protected function isBomStrippable()
{
return !empty($this->getInputBOM()) && $this->strip_bom;
}
/**
* Return the Iterator without the BOM sequence
*
* @param Iterator $iterator
*
* @return Iterator
*/
protected function getStripBomIterator(Iterator $iterator)
{
$bom_length = mb_strlen($this->getInputBOM());
$enclosure = $this->getEnclosure();
$strip_bom = function ($row, $index) use ($bom_length, $enclosure) {
if (0 != $index) {
return $row;
}
$row[0] = mb_substr($row[0], $bom_length);
if (mb_substr($row[0], 0, 1) === $enclosure && mb_substr($row[0], -1, 1) === $enclosure) {
$row[0] = mb_substr($row[0], 1, -1);
}
return $row;
};
return new MapIterator($iterator, $strip_bom);
}
/**
* Filter the Iterator
*
* @param Iterator $iterator
*
* @return Iterator
*/
protected function applyIteratorFilter(Iterator $iterator)
{
$reducer = function ($iterator, $callable) {
return new CallbackFilterIterator($iterator, $callable);
};
$iterator = array_reduce($this->iterator_filters, $reducer, $iterator);
$this->iterator_filters = [];
return $iterator;
}
/**
* Sort the Iterator
*
* @param Iterator $iterator
*
* @return Iterator
*/
protected function applyIteratorSortBy(Iterator $iterator)
{
if (!$this->iterator_sort_by) {
return $iterator;
}
$obj = new ArrayIterator(iterator_to_array($iterator));
$obj->uasort(function ($row_a, $row_b) {
$res = 0;
foreach ($this->iterator_sort_by as $compare) {
if (0 !== ($res = call_user_func($compare, $row_a, $row_b))) {
break;
}
}
return $res;
});
$this->iterator_sort_by = [];
return $obj;
}
/**
* Sort the Iterator
*
* @param Iterator $iterator
*
* @return Iterator
*/
protected function applyIteratorInterval(Iterator $iterator)
{
$offset = $this->iterator_offset;
$limit = $this->iterator_limit;
$this->iterator_limit = -1;
$this->iterator_offset = 0;
return new LimitIterator($iterator, $offset, $limit);
}
}

View File

@@ -0,0 +1,187 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv\Modifier;
use League\Csv\Exception\InvalidRowException;
/**
* Trait to format and validate the row before insertion
*
* @package League.csv
* @since 7.0.0
*
*/
trait RowFilter
{
/**
* Callables to validate the row before insertion
*
* @var callable[]
*/
protected $validators = [];
/**
* Callables to format the row before insertion
*
* @var callable[]
*/
protected $formatters = [];
/**
* add a formatter to the collection
*
* @param callable $callable
*
* @return $this
*/
public function addFormatter(callable $callable)
{
$this->formatters[] = $callable;
return $this;
}
/**
* Remove a formatter from the collection
*
* @param callable $callable
*
* @return $this
*/
public function removeFormatter(callable $callable)
{
$res = array_search($callable, $this->formatters, true);
unset($this->formatters[$res]);
return $this;
}
/**
* Detect if the formatter is already registered
*
* @param callable $callable
*
* @return bool
*/
public function hasFormatter(callable $callable)
{
return false !== array_search($callable, $this->formatters, true);
}
/**
* Remove all registered formatter
*
* @return $this
*/
public function clearFormatters()
{
$this->formatters = [];
return $this;
}
/**
* add a Validator to the collection
*
* @param callable $callable
* @param string $name the rule name
*
* @return $this
*/
public function addValidator(callable $callable, $name)
{
$name = $this->validateString($name);
$this->validators[$name] = $callable;
return $this;
}
/**
* @inheritdoc
*/
abstract protected function validateString($str);
/**
* Remove a validator from the collection
*
* @param string $name the validator name
*
* @return $this
*/
public function removeValidator($name)
{
$name = $this->validateString($name);
unset($this->validators[$name]);
return $this;
}
/**
* Detect if a validator is already registered
*
* @param string $name the validator name
*
* @return bool
*/
public function hasValidator($name)
{
$name = $this->validateString($name);
return isset($this->validators[$name]);
}
/**
* Remove all registered validators
*
* @return $this
*/
public function clearValidators()
{
$this->validators = [];
return $this;
}
/**
* Format the given row
*
* @param array $row
*
* @return array
*/
protected function formatRow(array $row)
{
foreach ($this->formatters as $formatter) {
$row = call_user_func($formatter, $row);
}
return $row;
}
/**
* Validate a row
*
* @param array $row
*
* @throws InvalidRowException If the validation failed
*/
protected function validateRow(array $row)
{
foreach ($this->validators as $name => $validator) {
if (true !== call_user_func($validator, $row)) {
throw new InvalidRowException($name, $row, 'row validation failed');
}
}
}
}

View File

@@ -0,0 +1,299 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv\Modifier;
use LogicException;
use OutOfBoundsException;
use SplFileObject;
/**
* A Trait to ease PHP Stream Filters manipulation
* with a SplFileObject
*
* @package League.csv
* @since 6.0.0
*
*/
trait StreamFilter
{
/**
* collection of stream filters
*
* @var array
*/
protected $stream_filters = [];
/**
* Stream filtering mode to apply on all filters
*
* @var int
*/
protected $stream_filter_mode = STREAM_FILTER_ALL;
/**
*the real path
*
* @var string the real path to the file
*
*/
protected $stream_uri;
/**
* PHP Stream Filter Regex
*
* @var string
*/
protected $stream_regex = ',^
php://filter/
(?P<mode>:?read=|write=)? # The resource open mode
(?P<filters>.*?) # The resource registered filters
/resource=(?P<resource>.*) # The resource path
$,ix';
/**
* Internal path setter
*
* The path must be an SplFileInfo object
* an object that implements the `__toString` method
* a path to a file
*
* @param StreamIterator|SplFileObject|string $path The file path
*/
protected function initStreamFilter($path)
{
$this->stream_filters = [];
if (!is_string($path)) {
$this->stream_uri = null;
return;
}
if (!preg_match($this->stream_regex, $path, $matches)) {
$this->stream_uri = $path;
return;
}
$this->stream_uri = $matches['resource'];
$this->stream_filters = array_map('urldecode', explode('|', $matches['filters']));
$this->stream_filter_mode = $this->fetchStreamModeAsInt($matches['mode']);
}
/**
* Get the stream mode
*
* @param string $mode
*
* @return int
*/
protected function fetchStreamModeAsInt($mode)
{
$mode = strtolower($mode);
$mode = rtrim($mode, '=');
if ('write' == $mode) {
return STREAM_FILTER_WRITE;
}
if ('read' == $mode) {
return STREAM_FILTER_READ;
}
return STREAM_FILTER_ALL;
}
/**
* Check if the trait methods can be used
*
* @throws LogicException If the API can not be use
*/
protected function assertStreamable()
{
if (!is_string($this->stream_uri)) {
throw new LogicException('The stream filter API can not be used');
}
}
/**
* Tells whether the stream filter capabilities can be used
*
* @return bool
*/
public function isActiveStreamFilter()
{
return is_string($this->stream_uri);
}
/**
* stream filter mode Setter
*
* Set the new Stream Filter mode and remove all
* previously attached stream filters
*
* @param int $mode
*
* @throws OutOfBoundsException If the mode is invalid
*
* @return $this
*/
public function setStreamFilterMode($mode)
{
$this->assertStreamable();
if (!in_array($mode, [STREAM_FILTER_ALL, STREAM_FILTER_READ, STREAM_FILTER_WRITE])) {
throw new OutOfBoundsException('the $mode should be a valid `STREAM_FILTER_*` constant');
}
$this->stream_filter_mode = $mode;
$this->stream_filters = [];
return $this;
}
/**
* stream filter mode getter
*
* @return int
*/
public function getStreamFilterMode()
{
$this->assertStreamable();
return $this->stream_filter_mode;
}
/**
* append a stream filter
*
* @param string $filter_name a string or an object that implements the '__toString' method
*
* @return $this
*/
public function appendStreamFilter($filter_name)
{
$this->assertStreamable();
$this->stream_filters[] = $this->sanitizeStreamFilter($filter_name);
return $this;
}
/**
* prepend a stream filter
*
* @param string $filter_name a string or an object that implements the '__toString' method
*
* @return $this
*/
public function prependStreamFilter($filter_name)
{
$this->assertStreamable();
array_unshift($this->stream_filters, $this->sanitizeStreamFilter($filter_name));
return $this;
}
/**
* Sanitize the stream filter name
*
* @param string $filter_name the stream filter name
*
* @return string
*/
protected function sanitizeStreamFilter($filter_name)
{
return urldecode($this->validateString($filter_name));
}
/**
* @inheritdoc
*/
abstract public function validateString($str);
/**
* Detect if the stream filter is already present
*
* @param string $filter_name
*
* @return bool
*/
public function hasStreamFilter($filter_name)
{
$this->assertStreamable();
return false !== array_search(urldecode($filter_name), $this->stream_filters, true);
}
/**
* Remove a filter from the collection
*
* @param string $filter_name
*
* @return $this
*/
public function removeStreamFilter($filter_name)
{
$this->assertStreamable();
$res = array_search(urldecode($filter_name), $this->stream_filters, true);
if (false !== $res) {
unset($this->stream_filters[$res]);
}
return $this;
}
/**
* Remove all registered stream filter
*
* @return $this
*/
public function clearStreamFilter()
{
$this->assertStreamable();
$this->stream_filters = [];
return $this;
}
/**
* Return the filter path
*
* @return string
*/
protected function getStreamFilterPath()
{
$this->assertStreamable();
if (!$this->stream_filters) {
return $this->stream_uri;
}
return 'php://filter/'
.$this->getStreamFilterPrefix()
.implode('|', array_map('urlencode', $this->stream_filters))
.'/resource='.$this->stream_uri;
}
/**
* Return PHP stream filter prefix
*
* @return string
*/
protected function getStreamFilterPrefix()
{
if (STREAM_FILTER_READ == $this->stream_filter_mode) {
return 'read=';
}
if (STREAM_FILTER_WRITE == $this->stream_filter_mode) {
return 'write=';
}
return '';
}
}

View File

@@ -0,0 +1,334 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv\Modifier;
use InvalidArgumentException;
use Iterator;
use SplFileObject;
/**
* A Stream Iterator
*
* @package League.csv
* @since 8.2.0
* @internal used internally to iterate over a stream resource
*
*/
class StreamIterator implements Iterator
{
/**
* Stream pointer
*
* @var resource
*/
protected $stream;
/**
* Current iterator value
*
* @var mixed
*/
protected $current_line;
/**
* Current iterator key
*
* @var int
*/
protected $current_line_number;
/**
* Flags for the StreamIterator
*
* @var int
*/
protected $flags = 0;
/**
* the field delimiter (one character only)
*
* @var string
*/
protected $delimiter = ',';
/**
* the field enclosure character (one character only)
*
* @var string
*/
protected $enclosure = '"';
/**
* the field escape character (one character only)
*
* @var string
*/
protected $escape = '\\';
/**
* New instance
*
* @param resource $stream stream type resource
*/
public function __construct($stream)
{
if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
throw new InvalidArgumentException(sprintf(
'Expected resource to be a stream, received %s instead',
is_object($stream) ? get_class($stream) : gettype($stream)
));
}
$data = stream_get_meta_data($stream);
if (!$data['seekable']) {
throw new InvalidArgumentException('The stream must be seekable');
}
$this->stream = $stream;
}
/**
* Set CSV control
*
* @see http://php.net/manual/en/splfileobject.setcsvcontrol.php
*
* @param string $delimiter
* @param string $enclosure
* @param string $escape
*/
public function setCsvControl($delimiter = ',', $enclosure = '"', $escape = '\\')
{
$this->delimiter = $this->filterControl($delimiter, 'delimiter');
$this->enclosure = $this->filterControl($enclosure, 'enclosure');
$this->escape = $this->filterControl($escape, 'escape');
}
/**
* Filter Csv control character
*
* @param string $char Csv control character
* @param string $type Csv control character type
*
* @throws InvalidArgumentException If the Csv control character is not one character only.
*
* @return string
*/
private function filterControl($char, $type)
{
if (1 == strlen($char)) {
return $char;
}
throw new InvalidArgumentException(sprintf('The %s character must be a single character', $type));
}
/**
* Set Flags
*
* @see http://php.net/manual/en/splfileobject.setflags.php
*
* @param int $flags
*/
public function setFlags($flags)
{
if (false === filter_var($flags, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
throw new InvalidArgumentException('The flags must be a positive integer');
}
$this->flags = $flags;
}
/**
* Write a field array as a CSV line
*
* @see http://php.net/manual/en/splfileobject.fputcsv.php
*
* @param array $fields
* @param string $delimiter
* @param string $enclosure
* @param string $escape
*
* @return int
*/
public function fputcsv(array $fields, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
return fputcsv(
$this->stream,
$fields,
$this->filterControl($delimiter, 'delimiter'),
$this->filterControl($enclosure, 'enclosure'),
$this->filterControl($escape, 'escape')
);
}
/**
* Retrieves the current line of the file.
*
* @return mixed
*/
public function current()
{
if (false !== $this->current_line) {
return $this->current_line;
}
if (($this->flags & SplFileObject::READ_CSV) == SplFileObject::READ_CSV) {
$this->current_line = $this->getCurrentRecord();
return $this->current_line;
}
$this->current_line = $this->getCurrentLine();
return $this->current_line;
}
/**
* Retrieves the current line as a CSV Record
*
* @return array
*/
protected function getCurrentRecord()
{
do {
$ret = fgetcsv($this->stream, 0, $this->delimiter, $this->enclosure, $this->escape);
} while ($this->flags & SplFileObject::SKIP_EMPTY && $ret !== false && $ret[0] === null);
return $ret;
}
/**
* Retrieves the current line as a string
*
* @return string
*/
protected function getCurrentLine()
{
do {
$line = fgets($this->stream);
} while ($this->flags & SplFileObject::SKIP_EMPTY && $line !== false && rtrim($line, "\r\n") !== '');
return $line;
}
/**
* Get line number
*
* @return int
*/
public function key()
{
return $this->current_line_number;
}
/**
* Read next line
*/
public function next()
{
$this->current_line = false;
$this->current_line_number++;
}
/**
* Rewind the file to the first line
*/
public function rewind()
{
rewind($this->stream);
$this->current_line_number = 0;
$this->current_line = false;
if ($this->flags & SplFileObject::READ_AHEAD) {
$this->current();
}
}
/**
* Not at EOF
*
* @return bool
*/
public function valid()
{
if ($this->flags & SplFileObject::READ_AHEAD) {
return $this->current() !== false;
}
return !feof($this->stream);
}
/**
* Gets line from file
*
* @see http://php.net/manual/en/splfileobject.fgets.php
*
* @return string
*/
public function fgets()
{
if (false !== $this->current_line) {
$this->next();
}
return $this->current_line = $this->getCurrentLine();
}
/**
* Output all remaining data on a file pointer
*
* @see http://php.net/manual/en/splfileobject.fpatssthru.php
*
* @return int
*/
public function fpassthru()
{
return fpassthru($this->stream);
}
/**
* Seek to a position
*
* @see http://php.net/manual/en/splfileobject.fseek.php
*
* @param int $offset
* @param int $whence
*
* @return int
*/
public function fseek($offset, $whence = SEEK_SET)
{
return fseek($this->stream, $offset, $whence);
}
/**
* Write to stream
*
* @see http://php.net/manual/en/splfileobject.fwrite.php
*
* @param string $str
* @param int $length
*
* @return int
*/
public function fwrite($str, $length = 0)
{
return fwrite($this->stream, $str, $length);
}
/**
* close the file pointer
*/
public function __destruct()
{
$this->stream = null;
}
}

View File

@@ -0,0 +1,100 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv\Plugin;
use InvalidArgumentException;
/**
* A class to manage column consistency on data insertion into a CSV
*
* @package League.csv
* @since 7.0.0
*
*/
class ColumnConsistencyValidator
{
/**
* The number of column per row
*
* @var int
*/
private $columns_count = -1;
/**
* should the class detect the column count based the inserted row
*
* @var bool
*/
private $detect_columns_count = false;
/**
* Set Inserted row column count
*
* @param int $value
*
* @throws InvalidArgumentException If $value is lesser than -1
*
*/
public function setColumnsCount($value)
{
if (false === filter_var($value, FILTER_VALIDATE_INT, ['options' => ['min_range' => -1]])) {
throw new InvalidArgumentException('the column count must an integer greater or equals to -1');
}
$this->detect_columns_count = false;
$this->columns_count = $value;
}
/**
* Column count getter
*
* @return int
*/
public function getColumnsCount()
{
return $this->columns_count;
}
/**
* The method will set the $columns_count property according to the next inserted row
* and therefore will also validate the next line whatever length it has no matter
* the current $columns_count property value.
*
*/
public function autodetectColumnsCount()
{
$this->detect_columns_count = true;
}
/**
* Is the submitted row valid
*
* @param array $row
*
* @return bool
*/
public function __invoke(array $row)
{
if ($this->detect_columns_count) {
$this->columns_count = count($row);
$this->detect_columns_count = false;
return true;
}
if (-1 == $this->columns_count) {
return true;
}
return count($row) === $this->columns_count;
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv\Plugin;
/**
* A class to validate null value handling on data insertion into a CSV
*
* @package League.csv
* @since 7.0.0
*
*/
class ForbiddenNullValuesValidator
{
/**
* Is the submitted row valid
*
* @param array $row
*
* @return bool
*/
public function __invoke(array $row)
{
$res = array_filter($row, function ($value) {
return null === $value;
});
return !$res;
}
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv\Plugin;
/**
* A class to remove null value from data before insertion into a CSV
*
* @package League.csv
* @since 7.0.0
*
*/
class SkipNullValuesFormatter
{
/**
* remove null value form the submitted array
*
* @param array $row
*
* @return array
*/
public function __invoke(array $row)
{
return array_filter($row, function ($value) {
return null !== $value;
});
}
}

362
vendor/league/csv/src/Reader.php vendored Normal file
View File

@@ -0,0 +1,362 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv;
use Generator;
use InvalidArgumentException;
use Iterator;
use League\Csv\Modifier\MapIterator;
use LimitIterator;
/**
* A class to manage extracting and filtering a CSV
*
* @package League.csv
* @since 3.0.0
*
*/
class Reader extends AbstractCsv
{
/**
* @inheritdoc
*/
protected $stream_filter_mode = STREAM_FILTER_READ;
/**
* Returns a sequential array of all CSV lines
*
* The callable function will be applied to each Iterator item
*
* @param callable|null $callable a callable function
*
* @return array
*/
public function fetchAll(callable $callable = null)
{
return iterator_to_array($this->applyCallable($this->getQueryIterator(), $callable), false);
}
/**
* Fetch the next row from a result set
*
* @param callable|null $callable a callable function to be applied to each Iterator item
*
* @return Iterator
*/
public function fetch(callable $callable = null)
{
return $this->applyCallable($this->getQueryIterator(), $callable);
}
/**
* Apply The callable function
*
* @param Iterator $iterator
* @param callable|null $callable
*
* @return Iterator
*/
protected function applyCallable(Iterator $iterator, callable $callable = null)
{
if (null !== $callable) {
return new MapIterator($iterator, $callable);
}
return $iterator;
}
/**
* Applies a callback function on the CSV
*
* The callback function must return TRUE in order to continue
* iterating over the iterator.
*
* @param callable $callable a callable function to apply to each selected CSV rows
*
* @return int the iteration count
*/
public function each(callable $callable)
{
$index = 0;
$iterator = $this->getQueryIterator();
$iterator->rewind();
while ($iterator->valid() && true === call_user_func(
$callable,
$iterator->current(),
$iterator->key(),
$iterator
)) {
++$index;
$iterator->next();
}
return $index;
}
/**
* Returns a single row from the CSV
*
* By default if no offset is provided the first row of the CSV is selected
*
* @param int $offset the CSV row offset
*
* @return array
*/
public function fetchOne($offset = 0)
{
$this->setOffset($offset);
$this->setLimit(1);
$iterator = $this->getQueryIterator();
$iterator->rewind();
return (array) $iterator->current();
}
/**
* Returns the next value from a single CSV column
*
* The callable function will be applied to each value to be return
*
* By default if no column index is provided the first column of the CSV is selected
*
* @param int $column_index CSV column index
* @param callable|null $callable A callable to be applied to each of the value to be returned.
*
* @return Iterator
*/
public function fetchColumn($column_index = 0, callable $callable = null)
{
$column_index = $this->validateInteger($column_index, 0, 'the column index must be a positive integer or 0');
$filter_column = function ($row) use ($column_index) {
return isset($row[$column_index]);
};
$select_column = function ($row) use ($column_index) {
return $row[$column_index];
};
$this->addFilter($filter_column);
return $this->applyCallable(new MapIterator($this->getQueryIterator(), $select_column), $callable);
}
/**
* Retrieve CSV data as pairs
*
* DEPRECATION WARNING! This method will be removed in the next major point release
*
* @deprecated deprecated since version 8.2
* @see Reader::fetchPairs
*
* Fetches an associative array of all rows as key-value pairs (first
* column is the key, second column is the value).
*
* By default if no column index is provided:
* - the first CSV column is used to provide the keys
* - the second CSV column is used to provide the value
*
* If the value from the column key index is duplicated its corresponding value will
* be overwritten
*
* @param int $offset_index The column index to serve as offset
* @param int $value_index The column index to serve as value
* @param callable|null $callable A callable to be applied to each of the rows to be returned.
*
* @return array
*/
public function fetchPairsWithoutDuplicates($offset_index = 0, $value_index = 1, callable $callable = null)
{
return iterator_to_array($this->fetchPairs($offset_index, $value_index, $callable), true);
}
/**
* Fetches the next key-value pairs from a result set (first
* column is the key, second column is the value).
*
* By default if no column index is provided:
* - the first CSV column is used to provide the keys
* - the second CSV column is used to provide the value
*
* @param int $offset_index The column index to serve as offset
* @param int $value_index The column index to serve as value
* @param callable|null $callable A callable to be applied to each of the rows to be returned.
*
* @return Generator
*/
public function fetchPairs($offset_index = 0, $value_index = 1, callable $callable = null)
{
$offset_index = $this->validateInteger($offset_index, 0, 'the offset column index must be a positive integer or 0');
$value_index = $this->validateInteger($value_index, 0, 'the value column index must be a positive integer or 0');
$filter_pairs = function ($row) use ($offset_index) {
return isset($row[$offset_index]);
};
$select_pairs = function ($row) use ($offset_index, $value_index) {
return [
$row[$offset_index],
isset($row[$value_index]) ? $row[$value_index] : null,
];
};
$this->addFilter($filter_pairs);
$iterator = $this->applyCallable(new MapIterator($this->getQueryIterator(), $select_pairs), $callable);
foreach ($iterator as $row) {
yield $row[0] => $row[1];
}
}
/**
* Fetch the next row from a result set
*
* The rows are presented as associated arrays
* The callable function will be applied to each row
*
* @param int|array $offset_or_keys the name for each key member OR the row Index to be
* used as the associated named keys
*
* @param callable $callable A callable to be applied to each of the rows to be returned.
*
* @throws InvalidArgumentException If the submitted keys are invalid
*
* @return Iterator
*/
public function fetchAssoc($offset_or_keys = 0, callable $callable = null)
{
$keys = $this->getAssocKeys($offset_or_keys);
$keys_count = count($keys);
$combine_array = function (array $row) use ($keys, $keys_count) {
if ($keys_count != count($row)) {
$row = array_slice(array_pad($row, $keys_count, null), 0, $keys_count);
}
return array_combine($keys, $row);
};
return $this->applyCallable(new MapIterator($this->getQueryIterator(), $combine_array), $callable);
}
/**
* Selects the array to be used as key for the fetchAssoc method
*
* @param int|array $offset_or_keys the assoc key OR the row Index to be used
* as the key index
*
* @throws InvalidArgumentException If the row index and/or the resulting array is invalid
*
* @return array
*/
protected function getAssocKeys($offset_or_keys)
{
if (is_array($offset_or_keys)) {
return $this->validateKeys($offset_or_keys);
}
$offset_or_keys = $this->validateInteger(
$offset_or_keys,
0,
'the row index must be a positive integer, 0 or a non empty array'
);
$keys = $this->validateKeys($this->getRow($offset_or_keys));
$filterOutRow = function ($row, $rowIndex) use ($offset_or_keys) {
return $rowIndex != $offset_or_keys;
};
$this->addFilter($filterOutRow);
return $keys;
}
/**
* Validates the array to be used by the fetchAssoc method
*
* @param array $keys
*
* @throws InvalidArgumentException If the submitted array fails the assertion
*
* @return array
*/
protected function validateKeys(array $keys)
{
if (empty($keys) || $keys !== array_unique(array_filter($keys, [$this, 'isValidKey']))) {
throw new InvalidArgumentException('Use a flat array with unique string values');
}
return $keys;
}
/**
* Returns whether the submitted value can be used as string
*
* @param mixed $value
*
* @return bool
*/
protected function isValidKey($value)
{
return is_scalar($value) || (is_object($value) && method_exists($value, '__toString'));
}
/**
* Returns a single row from the CSV without filtering
*
* @param int $offset
*
* @throws InvalidArgumentException If the $offset is not valid or the row does not exist
*
* @return array
*/
protected function getRow($offset)
{
$row = $this->seekRow($offset);
if (empty($row)) {
throw new InvalidArgumentException('the specified row does not exist or is empty');
}
if (0 !== $offset || !$this->isBomStrippable()) {
return $row;
}
$bom_length = mb_strlen($this->getInputBOM());
$row[0] = mb_substr($row[0], $bom_length);
if ($this->enclosure == mb_substr($row[0], 0, 1) && $this->enclosure == mb_substr($row[0], -1, 1)) {
$row[0] = mb_substr($row[0], 1, -1);
}
return $row;
}
/**
* Returns the row at a given offset
*
* @param int $offset
*
* @return mixed
*/
protected function seekRow($offset)
{
$stream = $this->getIterator();
$stream->rewind();
//Workaround for SplFileObject::seek bug in PHP7.2+ see https://bugs.php.net/bug.php?id=75917
if (PHP_VERSION_ID > 70200 && !$stream instanceof StreamIterator) {
while ($offset !== $stream->key() && $stream->valid()) {
$stream->next();
}
return $stream->current();
}
$iterator = new LimitIterator($stream, $offset, 1);
$iterator->rewind();
return $iterator->current();
}
}

170
vendor/league/csv/src/Writer.php vendored Normal file
View File

@@ -0,0 +1,170 @@
<?php
/**
* This file is part of the League.csv library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/csv/
* @version 8.2.3
* @package League.csv
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Csv;
use InvalidArgumentException;
use League\Csv\Modifier\RowFilter;
use League\Csv\Modifier\StreamIterator;
use ReflectionMethod;
use RuntimeException;
use SplFileObject;
use Traversable;
/**
* A class to manage data insertion into a CSV
*
* @package League.csv
* @since 4.0.0
*
*/
class Writer extends AbstractCsv
{
use RowFilter;
/**
* @inheritdoc
*/
protected $stream_filter_mode = STREAM_FILTER_WRITE;
/**
* The CSV object holder
*
* @var SplFileObject|StreamIterator
*/
protected $csv;
/**
* fputcsv method from SplFileObject or StreamIterator
*
* @var ReflectionMethod
*/
protected $fputcsv;
/**
* Nb parameters for SplFileObject::fputcsv method
*
* @var integer
*/
protected $fputcsv_param_count;
/**
* Adds multiple lines to the CSV document
*
* a simple wrapper method around insertOne
*
* @param Traversable|array $rows a multidimensional array or a Traversable object
*
* @throws InvalidArgumentException If the given rows format is invalid
*
* @return static
*/
public function insertAll($rows)
{
if (!is_array($rows) && !$rows instanceof Traversable) {
throw new InvalidArgumentException(
'the provided data must be an array OR a `Traversable` object'
);
}
foreach ($rows as $row) {
$this->insertOne($row);
}
return $this;
}
/**
* Adds a single line to a CSV document
*
* @param string[]|string $row a string, an array or an object implementing to '__toString' method
*
* @return static
*/
public function insertOne($row)
{
if (!is_array($row)) {
$row = str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape);
}
$row = $this->formatRow($row);
$this->validateRow($row);
$this->addRow($row);
return $this;
}
/**
* Add new record to the CSV document
*
* @param array $row record to add
*/
protected function addRow(array $row)
{
$this->initCsv();
if (!$this->fputcsv->invokeArgs($this->csv, $this->getFputcsvParameters($row))) {
throw new RuntimeException('Unable to write record to the CSV document.');
}
if ("\n" !== $this->newline) {
$this->csv->fseek(-1, SEEK_CUR);
$this->csv->fwrite($this->newline, strlen($this->newline));
}
}
/**
* Initialize the CSV object and settings
*/
protected function initCsv()
{
if (null !== $this->csv) {
return;
}
$this->csv = $this->getIterator();
$this->fputcsv = new ReflectionMethod(get_class($this->csv), 'fputcsv');
$this->fputcsv_param_count = $this->fputcsv->getNumberOfParameters();
}
/**
* returns the parameters for SplFileObject::fputcsv
*
* @param array $fields The fields to be add
*
* @return array
*/
protected function getFputcsvParameters(array $fields)
{
$parameters = [$fields, $this->delimiter, $this->enclosure];
if (4 == $this->fputcsv_param_count) {
$parameters[] = $this->escape;
}
return $parameters;
}
/**
* {@inheritdoc}
*/
public function isActiveStreamFilter()
{
return parent::isActiveStreamFilter() && null === $this->csv;
}
/**
* {@inheritdoc}
*/
public function __destruct()
{
$this->csv = null;
parent::__destruct();
}
}

21
vendor/league/oauth2-client/LICENSE vendored Normal file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2013-2020 Alex Bilbie <hello@alexbilbie.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.

58
vendor/league/oauth2-client/README.md vendored Normal file
View File

@@ -0,0 +1,58 @@
# OAuth 2.0 Client
This package provides a base for integrating with [OAuth 2.0](http://oauth.net/2/) service providers.
[![Gitter Chat](https://img.shields.io/badge/gitter-join_chat-brightgreen.svg?style=flat-square)](https://gitter.im/thephpleague/oauth2-client)
[![Source Code](https://img.shields.io/badge/source-thephpleague/oauth2--client-blue.svg?style=flat-square)](https://github.com/thephpleague/oauth2-client)
[![Latest Version](https://img.shields.io/github/release/thephpleague/oauth2-client.svg?style=flat-square)](https://github.com/thephpleague/oauth2-client/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/thephpleague/oauth2-client/blob/master/LICENSE)
[![Build Status](https://img.shields.io/github/actions/workflow/status/thephpleague/oauth2-client/continuous-integration.yml?label=CI&logo=github&style=flat-square)](https://github.com/thephpleague/oauth2-client/actions?query=workflow%3ACI)
[![Codecov Code Coverage](https://img.shields.io/codecov/c/gh/thephpleague/oauth2-client?label=codecov&logo=codecov&style=flat-square)](https://codecov.io/gh/thephpleague/oauth2-client)
[![Total Downloads](https://img.shields.io/packagist/dt/league/oauth2-client.svg?style=flat-square)](https://packagist.org/packages/league/oauth2-client)
---
The OAuth 2.0 login flow, seen commonly around the web in the form of "Connect with Facebook/Google/etc." buttons, is a common integration added to web applications, but it can be tricky and tedious to do right. To help, we've created the `league/oauth2-client` package, which provides a base for integrating with various OAuth 2.0 providers, without overburdening your application with the concerns of [RFC 6749](http://tools.ietf.org/html/rfc6749).
This OAuth 2.0 client library will work with any OAuth 2.0 provider that conforms to the OAuth 2.0 Authorization Framework. Out-of-the-box, we provide a `GenericProvider` class to connect to any service provider that uses [Bearer tokens](http://tools.ietf.org/html/rfc6750). See our [basic usage guide](https://oauth2-client.thephpleague.com/usage/) for examples using `GenericProvider`.
Many service providers provide additional functionality above and beyond the OAuth 2.0 specification. For this reason, you may extend and wrap this library to support additional behavior. There are already many [official](https://oauth2-client.thephpleague.com/providers/league/) and [third-party](https://oauth2-client.thephpleague.com/providers/thirdparty/) provider clients available (e.g., Facebook, GitHub, Google, Instagram, LinkedIn, etc.). If your provider isn't in the list, feel free to add it.
This package is compliant with [PSR-1][], [PSR-2][], [PSR-4][], and [PSR-7][]. If you notice compliance oversights, please send a patch via pull request. If you're interested in contributing to this library, please take a look at our [contributing guidelines](https://github.com/thephpleague/oauth2-client/blob/master/CONTRIBUTING.md).
## Requirements
We support the following versions of PHP:
* PHP 8.1
* PHP 8.0
* PHP 7.4
* PHP 7.3
* PHP 7.2
* PHP 7.1
* PHP 7.0
* PHP 5.6
## Provider Clients
We provide a list of [official PHP League provider clients](https://oauth2-client.thephpleague.com/providers/league/), as well as [third-party provider clients](https://oauth2-client.thephpleague.com/providers/thirdparty/).
To build your own provider client, please refer to "[Implementing a Provider Client](https://oauth2-client.thephpleague.com/providers/implementing/)."
## Usage
For usage and code examples, check out our [basic usage guide](https://oauth2-client.thephpleague.com/usage/).
## Contributing
Please see [our contributing guidelines](https://github.com/thephpleague/oauth2-client/blob/master/CONTRIBUTING.md) for details.
## License
The MIT License (MIT). Please see [LICENSE](https://github.com/thephpleague/oauth2-client/blob/master/LICENSE) for more information.
[PSR-1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md
[PSR-2]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md
[PSR-4]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md
[PSR-7]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md

View File

@@ -0,0 +1,58 @@
{
"name": "league/oauth2-client",
"description": "OAuth 2.0 Client Library",
"license": "MIT",
"config": {
"sort-packages": true
},
"require": {
"php": "^5.6 || ^7.0 || ^8.0",
"guzzlehttp/guzzle": "^6.0 || ^7.0",
"paragonie/random_compat": "^1 || ^2 || ^9.99"
},
"require-dev": {
"mockery/mockery": "^1.3.5",
"php-parallel-lint/php-parallel-lint": "^1.3.1",
"phpunit/phpunit": "^5.7 || ^6.0 || ^9.5",
"squizlabs/php_codesniffer": "^2.3 || ^3.0"
},
"keywords": [
"oauth",
"oauth2",
"authorization",
"authentication",
"idp",
"identity",
"sso",
"single sign on"
],
"authors": [
{
"name": "Alex Bilbie",
"email": "hello@alexbilbie.com",
"homepage": "http://www.alexbilbie.com",
"role": "Developer"
},
{
"name": "Woody Gilk",
"homepage": "https://github.com/shadowhand",
"role": "Contributor"
}
],
"autoload": {
"psr-4": {
"League\\OAuth2\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"League\\OAuth2\\Client\\Test\\": "test/src/"
}
},
"extra": {
"branch-alias": {
"dev-2.x": "2.0.x-dev"
}
}
}

View File

@@ -0,0 +1,80 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Grant;
use League\OAuth2\Client\Tool\RequiredParameterTrait;
/**
* Represents a type of authorization grant.
*
* An authorization grant is a credential representing the resource
* owner's authorization (to access its protected resources) used by the
* client to obtain an access token. OAuth 2.0 defines four
* grant types -- authorization code, implicit, resource owner password
* credentials, and client credentials -- as well as an extensibility
* mechanism for defining additional types.
*
* @link http://tools.ietf.org/html/rfc6749#section-1.3 Authorization Grant (RFC 6749, §1.3)
*/
abstract class AbstractGrant
{
use RequiredParameterTrait;
/**
* Returns the name of this grant, eg. 'grant_name', which is used as the
* grant type when encoding URL query parameters.
*
* @return string
*/
abstract protected function getName();
/**
* Returns a list of all required request parameters.
*
* @return array
*/
abstract protected function getRequiredRequestParameters();
/**
* Returns this grant's name as its string representation. This allows for
* string interpolation when building URL query parameters.
*
* @return string
*/
public function __toString()
{
return $this->getName();
}
/**
* Prepares an access token request's parameters by checking that all
* required parameters are set, then merging with any given defaults.
*
* @param array $defaults
* @param array $options
* @return array
*/
public function prepareRequestParameters(array $defaults, array $options)
{
$defaults['grant_type'] = $this->getName();
$required = $this->getRequiredRequestParameters();
$provided = array_merge($defaults, $options);
$this->checkRequiredParameters($required, $provided);
return $provided;
}
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Grant;
/**
* Represents an authorization code grant.
*
* @link http://tools.ietf.org/html/rfc6749#section-1.3.1 Authorization Code (RFC 6749, §1.3.1)
*/
class AuthorizationCode extends AbstractGrant
{
/**
* @inheritdoc
*/
protected function getName()
{
return 'authorization_code';
}
/**
* @inheritdoc
*/
protected function getRequiredRequestParameters()
{
return [
'code',
];
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Grant;
/**
* Represents a client credentials grant.
*
* @link http://tools.ietf.org/html/rfc6749#section-1.3.4 Client Credentials (RFC 6749, §1.3.4)
*/
class ClientCredentials extends AbstractGrant
{
/**
* @inheritdoc
*/
protected function getName()
{
return 'client_credentials';
}
/**
* @inheritdoc
*/
protected function getRequiredRequestParameters()
{
return [];
}
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Grant\Exception;
use InvalidArgumentException;
/**
* Exception thrown if the grant does not extend from AbstractGrant.
*
* @see League\OAuth2\Client\Grant\AbstractGrant
*/
class InvalidGrantException extends InvalidArgumentException
{
}

View File

@@ -0,0 +1,104 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Grant;
use League\OAuth2\Client\Grant\Exception\InvalidGrantException;
/**
* Represents a factory used when retrieving an authorization grant type.
*/
class GrantFactory
{
/**
* @var array
*/
protected $registry = [];
/**
* Defines a grant singleton in the registry.
*
* @param string $name
* @param AbstractGrant $grant
* @return self
*/
public function setGrant($name, AbstractGrant $grant)
{
$this->registry[$name] = $grant;
return $this;
}
/**
* Returns a grant singleton by name.
*
* If the grant has not be registered, a default grant will be loaded.
*
* @param string $name
* @return AbstractGrant
*/
public function getGrant($name)
{
if (empty($this->registry[$name])) {
$this->registerDefaultGrant($name);
}
return $this->registry[$name];
}
/**
* Registers a default grant singleton by name.
*
* @param string $name
* @return self
*/
protected function registerDefaultGrant($name)
{
// PascalCase the grant. E.g: 'authorization_code' becomes 'AuthorizationCode'
$class = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $name)));
$class = 'League\\OAuth2\\Client\\Grant\\' . $class;
$this->checkGrant($class);
return $this->setGrant($name, new $class);
}
/**
* Determines if a variable is a valid grant.
*
* @param mixed $class
* @return boolean
*/
public function isGrant($class)
{
return is_subclass_of($class, AbstractGrant::class);
}
/**
* Checks if a variable is a valid grant.
*
* @throws InvalidGrantException
* @param mixed $class
* @return void
*/
public function checkGrant($class)
{
if (!$this->isGrant($class)) {
throw new InvalidGrantException(sprintf(
'Grant "%s" must extend AbstractGrant',
is_object($class) ? get_class($class) : $class
));
}
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Grant;
/**
* Represents a resource owner password credentials grant.
*
* @link http://tools.ietf.org/html/rfc6749#section-1.3.3 Resource Owner Password Credentials (RFC 6749, §1.3.3)
*/
class Password extends AbstractGrant
{
/**
* @inheritdoc
*/
protected function getName()
{
return 'password';
}
/**
* @inheritdoc
*/
protected function getRequiredRequestParameters()
{
return [
'username',
'password',
];
}
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Grant;
/**
* Represents a refresh token grant.
*
* @link http://tools.ietf.org/html/rfc6749#section-6 Refreshing an Access Token (RFC 6749, §6)
*/
class RefreshToken extends AbstractGrant
{
/**
* @inheritdoc
*/
protected function getName()
{
return 'refresh_token';
}
/**
* @inheritdoc
*/
protected function getRequiredRequestParameters()
{
return [
'refresh_token',
];
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\OptionProvider;
use InvalidArgumentException;
/**
* Add http basic auth into access token request options
* @link https://tools.ietf.org/html/rfc6749#section-2.3.1
*/
class HttpBasicAuthOptionProvider extends PostAuthOptionProvider
{
/**
* @inheritdoc
*/
public function getAccessTokenOptions($method, array $params)
{
if (empty($params['client_id']) || empty($params['client_secret'])) {
throw new InvalidArgumentException('clientId and clientSecret are required for http basic auth');
}
$encodedCredentials = base64_encode(sprintf('%s:%s', $params['client_id'], $params['client_secret']));
unset($params['client_id'], $params['client_secret']);
$options = parent::getAccessTokenOptions($method, $params);
$options['headers']['Authorization'] = 'Basic ' . $encodedCredentials;
return $options;
}
}

View File

@@ -0,0 +1,30 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\OptionProvider;
/**
* Interface for access token options provider
*/
interface OptionProviderInterface
{
/**
* Builds request options used for requesting an access token.
*
* @param string $method
* @param array $params
* @return array
*/
public function getAccessTokenOptions($method, array $params);
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\OptionProvider;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Tool\QueryBuilderTrait;
/**
* Provide options for access token
*/
class PostAuthOptionProvider implements OptionProviderInterface
{
use QueryBuilderTrait;
/**
* @inheritdoc
*/
public function getAccessTokenOptions($method, array $params)
{
$options = ['headers' => ['content-type' => 'application/x-www-form-urlencoded']];
if ($method === AbstractProvider::METHOD_POST) {
$options['body'] = $this->getAccessTokenBody($params);
}
return $options;
}
/**
* Returns the request body for requesting an access token.
*
* @param array $params
* @return string
*/
protected function getAccessTokenBody(array $params)
{
return $this->buildQueryString($params);
}
}

View File

@@ -0,0 +1,941 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Provider;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\ClientInterface as HttpClientInterface;
use GuzzleHttp\Exception\BadResponseException;
use InvalidArgumentException;
use League\OAuth2\Client\Grant\AbstractGrant;
use League\OAuth2\Client\Grant\GrantFactory;
use League\OAuth2\Client\OptionProvider\OptionProviderInterface;
use League\OAuth2\Client\OptionProvider\PostAuthOptionProvider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Token\AccessTokenInterface;
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
use League\OAuth2\Client\Tool\GuardedPropertyTrait;
use League\OAuth2\Client\Tool\QueryBuilderTrait;
use League\OAuth2\Client\Tool\RequestFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use UnexpectedValueException;
/**
* Represents a service provider (authorization server).
*
* @link http://tools.ietf.org/html/rfc6749#section-1.1 Roles (RFC 6749, §1.1)
*/
abstract class AbstractProvider
{
use ArrayAccessorTrait;
use GuardedPropertyTrait;
use QueryBuilderTrait;
/**
* @var string|null Key used in a token response to identify the resource owner.
*/
const ACCESS_TOKEN_RESOURCE_OWNER_ID = null;
/**
* @var string HTTP method used to fetch access tokens.
*/
const METHOD_GET = 'GET';
/**
* @var string HTTP method used to fetch access tokens.
*/
const METHOD_POST = 'POST';
/**
* @var string PKCE method used to fetch authorization token.
* The PKCE code challenge will be hashed with sha256 (recommended).
*/
const PKCE_METHOD_S256 = 'S256';
/**
* @var string PKCE method used to fetch authorization token.
* The PKCE code challenge will be sent as plain text, this is NOT recommended.
* Only use `plain` if no other option is possible.
*/
const PKCE_METHOD_PLAIN = 'plain';
/**
* @var string
*/
protected $clientId;
/**
* @var string
*/
protected $clientSecret;
/**
* @var string
*/
protected $redirectUri;
/**
* @var string
*/
protected $state;
/**
* @var string|null
*/
protected $pkceCode = null;
/**
* @var GrantFactory
*/
protected $grantFactory;
/**
* @var RequestFactory
*/
protected $requestFactory;
/**
* @var HttpClientInterface
*/
protected $httpClient;
/**
* @var OptionProviderInterface
*/
protected $optionProvider;
/**
* Constructs an OAuth 2.0 service provider.
*
* @param array $options An array of options to set on this provider.
* Options include `clientId`, `clientSecret`, `redirectUri`, and `state`.
* Individual providers may introduce more options, as needed.
* @param array $collaborators An array of collaborators that may be used to
* override this provider's default behavior. Collaborators include
* `grantFactory`, `requestFactory`, and `httpClient`.
* Individual providers may introduce more collaborators, as needed.
*/
public function __construct(array $options = [], array $collaborators = [])
{
// We'll let the GuardedPropertyTrait handle mass assignment of incoming
// options, skipping any blacklisted properties defined in the provider
$this->fillProperties($options);
if (empty($collaborators['grantFactory'])) {
$collaborators['grantFactory'] = new GrantFactory();
}
$this->setGrantFactory($collaborators['grantFactory']);
if (empty($collaborators['requestFactory'])) {
$collaborators['requestFactory'] = new RequestFactory();
}
$this->setRequestFactory($collaborators['requestFactory']);
if (empty($collaborators['httpClient'])) {
$client_options = $this->getAllowedClientOptions($options);
$collaborators['httpClient'] = new HttpClient(
array_intersect_key($options, array_flip($client_options))
);
}
$this->setHttpClient($collaborators['httpClient']);
if (empty($collaborators['optionProvider'])) {
$collaborators['optionProvider'] = new PostAuthOptionProvider();
}
$this->setOptionProvider($collaborators['optionProvider']);
}
/**
* Returns the list of options that can be passed to the HttpClient
*
* @param array $options An array of options to set on this provider.
* Options include `clientId`, `clientSecret`, `redirectUri`, and `state`.
* Individual providers may introduce more options, as needed.
* @return array The options to pass to the HttpClient constructor
*/
protected function getAllowedClientOptions(array $options)
{
$client_options = ['timeout', 'proxy'];
// Only allow turning off ssl verification if it's for a proxy
if (!empty($options['proxy'])) {
$client_options[] = 'verify';
}
return $client_options;
}
/**
* Sets the grant factory instance.
*
* @param GrantFactory $factory
* @return self
*/
public function setGrantFactory(GrantFactory $factory)
{
$this->grantFactory = $factory;
return $this;
}
/**
* Returns the current grant factory instance.
*
* @return GrantFactory
*/
public function getGrantFactory()
{
return $this->grantFactory;
}
/**
* Sets the request factory instance.
*
* @param RequestFactory $factory
* @return self
*/
public function setRequestFactory(RequestFactory $factory)
{
$this->requestFactory = $factory;
return $this;
}
/**
* Returns the request factory instance.
*
* @return RequestFactory
*/
public function getRequestFactory()
{
return $this->requestFactory;
}
/**
* Sets the HTTP client instance.
*
* @param HttpClientInterface $client
* @return self
*/
public function setHttpClient(HttpClientInterface $client)
{
$this->httpClient = $client;
return $this;
}
/**
* Returns the HTTP client instance.
*
* @return HttpClientInterface
*/
public function getHttpClient()
{
return $this->httpClient;
}
/**
* Sets the option provider instance.
*
* @param OptionProviderInterface $provider
* @return self
*/
public function setOptionProvider(OptionProviderInterface $provider)
{
$this->optionProvider = $provider;
return $this;
}
/**
* Returns the option provider instance.
*
* @return OptionProviderInterface
*/
public function getOptionProvider()
{
return $this->optionProvider;
}
/**
* Returns the current value of the state parameter.
*
* This can be accessed by the redirect handler during authorization.
*
* @return string
*/
public function getState()
{
return $this->state;
}
/**
* Set the value of the pkceCode parameter.
*
* When using PKCE this should be set before requesting an access token.
*
* @param string $pkceCode
* @return self
*/
public function setPkceCode($pkceCode)
{
$this->pkceCode = $pkceCode;
return $this;
}
/**
* Returns the current value of the pkceCode parameter.
*
* This can be accessed by the redirect handler during authorization.
*
* @return string|null
*/
public function getPkceCode()
{
return $this->pkceCode;
}
/**
* Returns the base URL for authorizing a client.
*
* Eg. https://oauth.service.com/authorize
*
* @return string
*/
abstract public function getBaseAuthorizationUrl();
/**
* Returns the base URL for requesting an access token.
*
* Eg. https://oauth.service.com/token
*
* @param array $params
* @return string
*/
abstract public function getBaseAccessTokenUrl(array $params);
/**
* Returns the URL for requesting the resource owner's details.
*
* @param AccessToken $token
* @return string
*/
abstract public function getResourceOwnerDetailsUrl(AccessToken $token);
/**
* Returns a new random string to use as the state parameter in an
* authorization flow.
*
* @param int $length Length of the random string to be generated.
* @return string
*/
protected function getRandomState($length = 32)
{
// Converting bytes to hex will always double length. Hence, we can reduce
// the amount of bytes by half to produce the correct length.
return bin2hex(random_bytes($length / 2));
}
/**
* Returns a new random string to use as PKCE code_verifier and
* hashed as code_challenge parameters in an authorization flow.
* Must be between 43 and 128 characters long.
*
* @param int $length Length of the random string to be generated.
* @return string
*/
protected function getRandomPkceCode($length = 64)
{
return substr(
strtr(
base64_encode(random_bytes($length)),
'+/',
'-_'
),
0,
$length
);
}
/**
* Returns the default scopes used by this provider.
*
* This should only be the scopes that are required to request the details
* of the resource owner, rather than all the available scopes.
*
* @return array
*/
abstract protected function getDefaultScopes();
/**
* Returns the string that should be used to separate scopes when building
* the URL for requesting an access token.
*
* @return string Scope separator, defaults to ','
*/
protected function getScopeSeparator()
{
return ',';
}
/**
* @return string|null
*/
protected function getPkceMethod()
{
return null;
}
/**
* Returns authorization parameters based on provided options.
*
* @param array $options
* @return array Authorization parameters
*/
protected function getAuthorizationParameters(array $options)
{
if (empty($options['state'])) {
$options['state'] = $this->getRandomState();
}
if (empty($options['scope'])) {
$options['scope'] = $this->getDefaultScopes();
}
$options += [
'response_type' => 'code',
'approval_prompt' => 'auto'
];
if (is_array($options['scope'])) {
$separator = $this->getScopeSeparator();
$options['scope'] = implode($separator, $options['scope']);
}
// Store the state as it may need to be accessed later on.
$this->state = $options['state'];
$pkceMethod = $this->getPkceMethod();
if (!empty($pkceMethod)) {
$this->pkceCode = $this->getRandomPkceCode();
if ($pkceMethod === static::PKCE_METHOD_S256) {
$options['code_challenge'] = trim(
strtr(
base64_encode(hash('sha256', $this->pkceCode, true)),
'+/',
'-_'
),
'='
);
} elseif ($pkceMethod === static::PKCE_METHOD_PLAIN) {
$options['code_challenge'] = $this->pkceCode;
} else {
throw new InvalidArgumentException('Unknown PKCE method "' . $pkceMethod . '".');
}
$options['code_challenge_method'] = $pkceMethod;
}
// Business code layer might set a different redirect_uri parameter
// depending on the context, leave it as-is
if (!isset($options['redirect_uri'])) {
$options['redirect_uri'] = $this->redirectUri;
}
$options['client_id'] = $this->clientId;
return $options;
}
/**
* Builds the authorization URL's query string.
*
* @param array $params Query parameters
* @return string Query string
*/
protected function getAuthorizationQuery(array $params)
{
return $this->buildQueryString($params);
}
/**
* Builds the authorization URL.
*
* @param array $options
* @return string Authorization URL
*/
public function getAuthorizationUrl(array $options = [])
{
$base = $this->getBaseAuthorizationUrl();
$params = $this->getAuthorizationParameters($options);
$query = $this->getAuthorizationQuery($params);
return $this->appendQuery($base, $query);
}
/**
* Redirects the client for authorization.
*
* @param array $options
* @param callable|null $redirectHandler
* @return mixed
*/
public function authorize(
array $options = [],
callable $redirectHandler = null
) {
$url = $this->getAuthorizationUrl($options);
if ($redirectHandler) {
return $redirectHandler($url, $this);
}
// @codeCoverageIgnoreStart
header('Location: ' . $url);
exit;
// @codeCoverageIgnoreEnd
}
/**
* Appends a query string to a URL.
*
* @param string $url The URL to append the query to
* @param string $query The HTTP query string
* @return string The resulting URL
*/
protected function appendQuery($url, $query)
{
$query = trim($query, '?&');
if ($query) {
$glue = strstr($url, '?') === false ? '?' : '&';
return $url . $glue . $query;
}
return $url;
}
/**
* Returns the method to use when requesting an access token.
*
* @return string HTTP method
*/
protected function getAccessTokenMethod()
{
return self::METHOD_POST;
}
/**
* Returns the key used in the access token response to identify the resource owner.
*
* @return string|null Resource owner identifier key
*/
protected function getAccessTokenResourceOwnerId()
{
return static::ACCESS_TOKEN_RESOURCE_OWNER_ID;
}
/**
* Builds the access token URL's query string.
*
* @param array $params Query parameters
* @return string Query string
*/
protected function getAccessTokenQuery(array $params)
{
return $this->buildQueryString($params);
}
/**
* Checks that a provided grant is valid, or attempts to produce one if the
* provided grant is a string.
*
* @param AbstractGrant|string $grant
* @return AbstractGrant
*/
protected function verifyGrant($grant)
{
if (is_string($grant)) {
return $this->grantFactory->getGrant($grant);
}
$this->grantFactory->checkGrant($grant);
return $grant;
}
/**
* Returns the full URL to use when requesting an access token.
*
* @param array $params Query parameters
* @return string
*/
protected function getAccessTokenUrl(array $params)
{
$url = $this->getBaseAccessTokenUrl($params);
if ($this->getAccessTokenMethod() === self::METHOD_GET) {
$query = $this->getAccessTokenQuery($params);
return $this->appendQuery($url, $query);
}
return $url;
}
/**
* Returns a prepared request for requesting an access token.
*
* @param array $params Query string parameters
* @return RequestInterface
*/
protected function getAccessTokenRequest(array $params)
{
$method = $this->getAccessTokenMethod();
$url = $this->getAccessTokenUrl($params);
$options = $this->optionProvider->getAccessTokenOptions($this->getAccessTokenMethod(), $params);
return $this->getRequest($method, $url, $options);
}
/**
* Requests an access token using a specified grant and option set.
*
* @param mixed $grant
* @param array<string, mixed> $options
* @throws IdentityProviderException
* @return AccessTokenInterface
*/
public function getAccessToken($grant, array $options = [])
{
$grant = $this->verifyGrant($grant);
$params = [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'redirect_uri' => $this->redirectUri,
];
if (!empty($this->pkceCode)) {
$params['code_verifier'] = $this->pkceCode;
}
$params = $grant->prepareRequestParameters($params, $options);
$request = $this->getAccessTokenRequest($params);
$response = $this->getParsedResponse($request);
if (false === is_array($response)) {
throw new UnexpectedValueException(
'Invalid response received from Authorization Server. Expected JSON.'
);
}
$prepared = $this->prepareAccessTokenResponse($response);
$token = $this->createAccessToken($prepared, $grant);
return $token;
}
/**
* Returns a PSR-7 request instance that is not authenticated.
*
* @param string $method
* @param string $url
* @param array $options
* @return RequestInterface
*/
public function getRequest($method, $url, array $options = [])
{
return $this->createRequest($method, $url, null, $options);
}
/**
* Returns an authenticated PSR-7 request instance.
*
* @param string $method
* @param string $url
* @param AccessTokenInterface|string|null $token
* @param array $options Any of "headers", "body", and "protocolVersion".
* @return RequestInterface
*/
public function getAuthenticatedRequest($method, $url, $token, array $options = [])
{
return $this->createRequest($method, $url, $token, $options);
}
/**
* Creates a PSR-7 request instance.
*
* @param string $method
* @param string $url
* @param AccessTokenInterface|string|null $token
* @param array $options
* @return RequestInterface
*/
protected function createRequest($method, $url, $token, array $options)
{
$defaults = [
'headers' => $this->getHeaders($token),
];
$options = array_merge_recursive($defaults, $options);
$factory = $this->getRequestFactory();
return $factory->getRequestWithOptions($method, $url, $options);
}
/**
* Sends a request instance and returns a response instance.
*
* WARNING: This method does not attempt to catch exceptions caused by HTTP
* errors! It is recommended to wrap this method in a try/catch block.
*
* @param RequestInterface $request
* @return ResponseInterface
*/
public function getResponse(RequestInterface $request)
{
return $this->getHttpClient()->send($request);
}
/**
* Sends a request and returns the parsed response.
*
* @param RequestInterface $request
* @throws IdentityProviderException
* @return mixed
*/
public function getParsedResponse(RequestInterface $request)
{
try {
$response = $this->getResponse($request);
} catch (BadResponseException $e) {
$response = $e->getResponse();
}
$parsed = $this->parseResponse($response);
$this->checkResponse($response, $parsed);
return $parsed;
}
/**
* Attempts to parse a JSON response.
*
* @param string $content JSON content from response body
* @return array Parsed JSON data
* @throws UnexpectedValueException if the content could not be parsed
*/
protected function parseJson($content)
{
$content = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new UnexpectedValueException(sprintf(
"Failed to parse JSON response: %s",
json_last_error_msg()
));
}
return $content;
}
/**
* Returns the content type header of a response.
*
* @param ResponseInterface $response
* @return string Semi-colon separated join of content-type headers.
*/
protected function getContentType(ResponseInterface $response)
{
return join(';', (array) $response->getHeader('content-type'));
}
/**
* Parses the response according to its content-type header.
*
* @throws UnexpectedValueException
* @param ResponseInterface $response
* @return array
*/
protected function parseResponse(ResponseInterface $response)
{
$content = (string) $response->getBody();
$type = $this->getContentType($response);
if (strpos($type, 'urlencoded') !== false) {
parse_str($content, $parsed);
return $parsed;
}
// Attempt to parse the string as JSON regardless of content type,
// since some providers use non-standard content types. Only throw an
// exception if the JSON could not be parsed when it was expected to.
try {
return $this->parseJson($content);
} catch (UnexpectedValueException $e) {
if (strpos($type, 'json') !== false) {
throw $e;
}
if ($response->getStatusCode() == 500) {
throw new UnexpectedValueException(
'An OAuth server error was encountered that did not contain a JSON body',
0,
$e
);
}
return $content;
}
}
/**
* Checks a provider response for errors.
*
* @throws IdentityProviderException
* @param ResponseInterface $response
* @param array|string $data Parsed response data
* @return void
*/
abstract protected function checkResponse(ResponseInterface $response, $data);
/**
* Prepares an parsed access token response for a grant.
*
* Custom mapping of expiration, etc should be done here. Always call the
* parent method when overloading this method.
*
* @param mixed $result
* @return array
*/
protected function prepareAccessTokenResponse(array $result)
{
if ($this->getAccessTokenResourceOwnerId() !== null) {
$result['resource_owner_id'] = $this->getValueByKey(
$result,
$this->getAccessTokenResourceOwnerId()
);
}
return $result;
}
/**
* Creates an access token from a response.
*
* The grant that was used to fetch the response can be used to provide
* additional context.
*
* @param array $response
* @param AbstractGrant $grant
* @return AccessTokenInterface
*/
protected function createAccessToken(array $response, AbstractGrant $grant)
{
return new AccessToken($response);
}
/**
* Generates a resource owner object from a successful resource owner
* details request.
*
* @param array $response
* @param AccessToken $token
* @return ResourceOwnerInterface
*/
abstract protected function createResourceOwner(array $response, AccessToken $token);
/**
* Requests and returns the resource owner of given access token.
*
* @param AccessToken $token
* @return ResourceOwnerInterface
*/
public function getResourceOwner(AccessToken $token)
{
$response = $this->fetchResourceOwnerDetails($token);
return $this->createResourceOwner($response, $token);
}
/**
* Requests resource owner details.
*
* @param AccessToken $token
* @return mixed
*/
protected function fetchResourceOwnerDetails(AccessToken $token)
{
$url = $this->getResourceOwnerDetailsUrl($token);
$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token);
$response = $this->getParsedResponse($request);
if (false === is_array($response)) {
throw new UnexpectedValueException(
'Invalid response received from Authorization Server. Expected JSON.'
);
}
return $response;
}
/**
* Returns the default headers used by this provider.
*
* Typically this is used to set 'Accept' or 'Content-Type' headers.
*
* @return array
*/
protected function getDefaultHeaders()
{
return [];
}
/**
* Returns the authorization headers used by this provider.
*
* Typically this is "Bearer" or "MAC". For more information see:
* http://tools.ietf.org/html/rfc6749#section-7.1
*
* No default is provided, providers must overload this method to activate
* authorization headers.
*
* @param mixed|null $token Either a string or an access token instance
* @return array
*/
protected function getAuthorizationHeaders($token = null)
{
return [];
}
/**
* Returns all headers used by this provider for a request.
*
* The request will be authenticated if an access token is provided.
*
* @param mixed|null $token object or string
* @return array
*/
public function getHeaders($token = null)
{
if ($token) {
return array_merge(
$this->getDefaultHeaders(),
$this->getAuthorizationHeaders($token)
);
}
return $this->getDefaultHeaders();
}
}

View File

@@ -0,0 +1,48 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Provider\Exception;
/**
* Exception thrown if the provider response contains errors.
*/
class IdentityProviderException extends \Exception
{
/**
* @var mixed
*/
protected $response;
/**
* @param string $message
* @param int $code
* @param mixed $response The response body
*/
public function __construct($message, $code, $response)
{
$this->response = $response;
parent::__construct($message, $code);
}
/**
* Returns the exception's response body.
*
* @return mixed
*/
public function getResponseBody()
{
return $this->response;
}
}

View File

@@ -0,0 +1,247 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Provider;
use InvalidArgumentException;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use Psr\Http\Message\ResponseInterface;
/**
* Represents a generic service provider that may be used to interact with any
* OAuth 2.0 service provider, using Bearer token authentication.
*/
class GenericProvider extends AbstractProvider
{
use BearerAuthorizationTrait;
/**
* @var string
*/
private $urlAuthorize;
/**
* @var string
*/
private $urlAccessToken;
/**
* @var string
*/
private $urlResourceOwnerDetails;
/**
* @var string
*/
private $accessTokenMethod;
/**
* @var string
*/
private $accessTokenResourceOwnerId;
/**
* @var array|null
*/
private $scopes = null;
/**
* @var string
*/
private $scopeSeparator;
/**
* @var string
*/
private $responseError = 'error';
/**
* @var string
*/
private $responseCode;
/**
* @var string
*/
private $responseResourceOwnerId = 'id';
/**
* @var string|null
*/
private $pkceMethod = null;
/**
* @param array $options
* @param array $collaborators
*/
public function __construct(array $options = [], array $collaborators = [])
{
$this->assertRequiredOptions($options);
$possible = $this->getConfigurableOptions();
$configured = array_intersect_key($options, array_flip($possible));
foreach ($configured as $key => $value) {
$this->$key = $value;
}
// Remove all options that are only used locally
$options = array_diff_key($options, $configured);
parent::__construct($options, $collaborators);
}
/**
* Returns all options that can be configured.
*
* @return array
*/
protected function getConfigurableOptions()
{
return array_merge($this->getRequiredOptions(), [
'accessTokenMethod',
'accessTokenResourceOwnerId',
'scopeSeparator',
'responseError',
'responseCode',
'responseResourceOwnerId',
'scopes',
'pkceMethod',
]);
}
/**
* Returns all options that are required.
*
* @return array
*/
protected function getRequiredOptions()
{
return [
'urlAuthorize',
'urlAccessToken',
'urlResourceOwnerDetails',
];
}
/**
* Verifies that all required options have been passed.
*
* @param array $options
* @return void
* @throws InvalidArgumentException
*/
private function assertRequiredOptions(array $options)
{
$missing = array_diff_key(array_flip($this->getRequiredOptions()), $options);
if (!empty($missing)) {
throw new InvalidArgumentException(
'Required options not defined: ' . implode(', ', array_keys($missing))
);
}
}
/**
* @inheritdoc
*/
public function getBaseAuthorizationUrl()
{
return $this->urlAuthorize;
}
/**
* @inheritdoc
*/
public function getBaseAccessTokenUrl(array $params)
{
return $this->urlAccessToken;
}
/**
* @inheritdoc
*/
public function getResourceOwnerDetailsUrl(AccessToken $token)
{
return $this->urlResourceOwnerDetails;
}
/**
* @inheritdoc
*/
public function getDefaultScopes()
{
return $this->scopes;
}
/**
* @inheritdoc
*/
protected function getAccessTokenMethod()
{
return $this->accessTokenMethod ?: parent::getAccessTokenMethod();
}
/**
* @inheritdoc
*/
protected function getAccessTokenResourceOwnerId()
{
return $this->accessTokenResourceOwnerId ?: parent::getAccessTokenResourceOwnerId();
}
/**
* @inheritdoc
*/
protected function getScopeSeparator()
{
return $this->scopeSeparator ?: parent::getScopeSeparator();
}
/**
* @inheritdoc
*/
protected function getPkceMethod()
{
return $this->pkceMethod ?: parent::getPkceMethod();
}
/**
* @inheritdoc
*/
protected function checkResponse(ResponseInterface $response, $data)
{
if (!empty($data[$this->responseError])) {
$error = $data[$this->responseError];
if (!is_string($error)) {
$error = var_export($error, true);
}
$code = $this->responseCode && !empty($data[$this->responseCode])? $data[$this->responseCode] : 0;
if (!is_int($code)) {
$code = intval($code);
}
throw new IdentityProviderException($error, $code, $data);
}
}
/**
* @inheritdoc
*/
protected function createResourceOwner(array $response, AccessToken $token)
{
return new GenericResourceOwner($response, $this->responseResourceOwnerId);
}
}

View File

@@ -0,0 +1,61 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Provider;
/**
* Represents a generic resource owner for use with the GenericProvider.
*/
class GenericResourceOwner implements ResourceOwnerInterface
{
/**
* @var array
*/
protected $response;
/**
* @var string
*/
protected $resourceOwnerId;
/**
* @param array $response
* @param string $resourceOwnerId
*/
public function __construct(array $response, $resourceOwnerId)
{
$this->response = $response;
$this->resourceOwnerId = $resourceOwnerId;
}
/**
* Returns the identifier of the authorized resource owner.
*
* @return mixed
*/
public function getId()
{
return $this->response[$this->resourceOwnerId];
}
/**
* Returns the raw resource owner response.
*
* @return array
*/
public function toArray()
{
return $this->response;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Provider;
/**
* Classes implementing `ResourceOwnerInterface` may be used to represent
* the resource owner authenticated with a service provider.
*/
interface ResourceOwnerInterface
{
/**
* Returns the identifier of the authorized resource owner.
*
* @return mixed
*/
public function getId();
/**
* Return all of the owner details available as an array.
*
* @return array
*/
public function toArray();
}

View File

@@ -0,0 +1,243 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Token;
use InvalidArgumentException;
use RuntimeException;
/**
* Represents an access token.
*
* @link http://tools.ietf.org/html/rfc6749#section-1.4 Access Token (RFC 6749, §1.4)
*/
class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInterface
{
/**
* @var string
*/
protected $accessToken;
/**
* @var int
*/
protected $expires;
/**
* @var string
*/
protected $refreshToken;
/**
* @var string
*/
protected $resourceOwnerId;
/**
* @var array
*/
protected $values = [];
/**
* @var int
*/
private static $timeNow;
/**
* Set the time now. This should only be used for testing purposes.
*
* @param int $timeNow the time in seconds since epoch
* @return void
*/
public static function setTimeNow($timeNow)
{
self::$timeNow = $timeNow;
}
/**
* Reset the time now if it was set for test purposes.
*
* @return void
*/
public static function resetTimeNow()
{
self::$timeNow = null;
}
/**
* @return int
*/
public function getTimeNow()
{
return self::$timeNow ? self::$timeNow : time();
}
/**
* Constructs an access token.
*
* @param array $options An array of options returned by the service provider
* in the access token request. The `access_token` option is required.
* @throws InvalidArgumentException if `access_token` is not provided in `$options`.
*/
public function __construct(array $options = [])
{
if (empty($options['access_token'])) {
throw new InvalidArgumentException('Required option not passed: "access_token"');
}
$this->accessToken = $options['access_token'];
if (!empty($options['resource_owner_id'])) {
$this->resourceOwnerId = $options['resource_owner_id'];
}
if (!empty($options['refresh_token'])) {
$this->refreshToken = $options['refresh_token'];
}
// We need to know when the token expires. Show preference to
// 'expires_in' since it is defined in RFC6749 Section 5.1.
// Defer to 'expires' if it is provided instead.
if (isset($options['expires_in'])) {
if (!is_numeric($options['expires_in'])) {
throw new \InvalidArgumentException('expires_in value must be an integer');
}
$this->expires = $options['expires_in'] != 0 ? $this->getTimeNow() + $options['expires_in'] : 0;
} elseif (!empty($options['expires'])) {
// Some providers supply the seconds until expiration rather than
// the exact timestamp. Take a best guess at which we received.
$expires = $options['expires'];
if (!$this->isExpirationTimestamp($expires)) {
$expires += $this->getTimeNow();
}
$this->expires = $expires;
}
// Capture any additional values that might exist in the token but are
// not part of the standard response. Vendors will sometimes pass
// additional user data this way.
$this->values = array_diff_key($options, array_flip([
'access_token',
'resource_owner_id',
'refresh_token',
'expires_in',
'expires',
]));
}
/**
* Check if a value is an expiration timestamp or second value.
*
* @param integer $value
* @return bool
*/
protected function isExpirationTimestamp($value)
{
// If the given value is larger than the original OAuth 2 draft date,
// assume that it is meant to be a (possible expired) timestamp.
$oauth2InceptionDate = 1349067600; // 2012-10-01
return ($value > $oauth2InceptionDate);
}
/**
* @inheritdoc
*/
public function getToken()
{
return $this->accessToken;
}
/**
* @inheritdoc
*/
public function getRefreshToken()
{
return $this->refreshToken;
}
/**
* @inheritdoc
*/
public function getExpires()
{
return $this->expires;
}
/**
* @inheritdoc
*/
public function getResourceOwnerId()
{
return $this->resourceOwnerId;
}
/**
* @inheritdoc
*/
public function hasExpired()
{
$expires = $this->getExpires();
if (empty($expires)) {
throw new RuntimeException('"expires" is not set on the token');
}
return $expires < time();
}
/**
* @inheritdoc
*/
public function getValues()
{
return $this->values;
}
/**
* @inheritdoc
*/
public function __toString()
{
return (string) $this->getToken();
}
/**
* @inheritdoc
*/
public function jsonSerialize()
{
$parameters = $this->values;
if ($this->accessToken) {
$parameters['access_token'] = $this->accessToken;
}
if ($this->refreshToken) {
$parameters['refresh_token'] = $this->refreshToken;
}
if ($this->expires) {
$parameters['expires'] = $this->expires;
}
if ($this->resourceOwnerId) {
$parameters['resource_owner_id'] = $this->resourceOwnerId;
}
return $parameters;
}
}

View File

@@ -0,0 +1,74 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Token;
use JsonSerializable;
use ReturnTypeWillChange;
use RuntimeException;
interface AccessTokenInterface extends JsonSerializable
{
/**
* Returns the access token string of this instance.
*
* @return string
*/
public function getToken();
/**
* Returns the refresh token, if defined.
*
* @return string|null
*/
public function getRefreshToken();
/**
* Returns the expiration timestamp in seconds, if defined.
*
* @return integer|null
*/
public function getExpires();
/**
* Checks if this token has expired.
*
* @return boolean true if the token has expired, false otherwise.
* @throws RuntimeException if 'expires' is not set on the token.
*/
public function hasExpired();
/**
* Returns additional vendor values stored in the token.
*
* @return array
*/
public function getValues();
/**
* Returns a string representation of the access token
*
* @return string
*/
public function __toString();
/**
* Returns an array of parameters to serialize when this is serialized with
* json_encode().
*
* @return array
*/
#[ReturnTypeWillChange]
public function jsonSerialize();
}

View File

@@ -0,0 +1,25 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Token;
interface ResourceOwnerAccessTokenInterface extends AccessTokenInterface
{
/**
* Returns the resource owner identifier, if defined.
*
* @return string|null
*/
public function getResourceOwnerId();
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
/**
* Provides generic array navigation tools.
*/
trait ArrayAccessorTrait
{
/**
* Returns a value by key using dot notation.
*
* @param array $data
* @param string $key
* @param mixed|null $default
* @return mixed
*/
private function getValueByKey(array $data, $key, $default = null)
{
if (!is_string($key) || empty($key) || !count($data)) {
return $default;
}
if (strpos($key, '.') !== false) {
$keys = explode('.', $key);
foreach ($keys as $innerKey) {
if (!is_array($data) || !array_key_exists($innerKey, $data)) {
return $default;
}
$data = $data[$innerKey];
}
return $data;
}
return array_key_exists($key, $data) ? $data[$key] : $default;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
use League\OAuth2\Client\Token\AccessTokenInterface;
/**
* Enables `Bearer` header authorization for providers.
*
* @link http://tools.ietf.org/html/rfc6750 Bearer Token Usage (RFC 6750)
*/
trait BearerAuthorizationTrait
{
/**
* Returns authorization headers for the 'bearer' grant.
*
* @param AccessTokenInterface|string|null $token Either a string or an access token instance
* @return array
*/
protected function getAuthorizationHeaders($token = null)
{
return ['Authorization' => 'Bearer ' . $token];
}
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
/**
* Provides support for blacklisting explicit properties from the
* mass assignment behavior.
*/
trait GuardedPropertyTrait
{
/**
* The properties that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];
/**
* Attempts to mass assign the given options to explicitly defined properties,
* skipping over any properties that are defined in the guarded array.
*
* @param array $options
* @return mixed
*/
protected function fillProperties(array $options = [])
{
if (isset($options['guarded'])) {
unset($options['guarded']);
}
foreach ($options as $option => $value) {
if (property_exists($this, $option) && !$this->isGuarded($option)) {
$this->{$option} = $value;
}
}
}
/**
* Returns current guarded properties.
*
* @return array
*/
public function getGuarded()
{
return $this->guarded;
}
/**
* Determines if the given property is guarded.
*
* @param string $property
* @return bool
*/
public function isGuarded($property)
{
return in_array($property, $this->getGuarded());
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Token\AccessTokenInterface;
/**
* Enables `MAC` header authorization for providers.
*
* @link http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-05 Message Authentication Code (MAC) Tokens
*/
trait MacAuthorizationTrait
{
/**
* Returns the id of this token for MAC generation.
*
* @param AccessToken $token
* @return string
*/
abstract protected function getTokenId(AccessToken $token);
/**
* Returns the MAC signature for the current request.
*
* @param string $id
* @param integer $ts
* @param string $nonce
* @return string
*/
abstract protected function getMacSignature($id, $ts, $nonce);
/**
* Returns a new random string to use as the state parameter in an
* authorization flow.
*
* @param int $length Length of the random string to be generated.
* @return string
*/
abstract protected function getRandomState($length = 32);
/**
* Returns the authorization headers for the 'mac' grant.
*
* @param AccessTokenInterface|string|null $token Either a string or an access token instance
* @return array
* @codeCoverageIgnore
*
* @todo This is currently untested and provided only as an example. If you
* complete the implementation, please create a pull request for
* https://github.com/thephpleague/oauth2-client
*/
protected function getAuthorizationHeaders($token = null)
{
if ($token === null) {
return [];
}
$ts = time();
$id = $this->getTokenId($token);
$nonce = $this->getRandomState(16);
$mac = $this->getMacSignature($id, $ts, $nonce);
$parts = [];
foreach (compact('id', 'ts', 'nonce', 'mac') as $key => $value) {
$parts[] = sprintf('%s="%s"', $key, $value);
}
return ['Authorization' => 'MAC ' . implode(', ', $parts)];
}
}

View File

@@ -0,0 +1,122 @@
<?php
namespace League\OAuth2\Client\Tool;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Uri;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
trait ProviderRedirectTrait
{
/**
* Maximum number of times to follow provider initiated redirects
*
* @var integer
*/
protected $redirectLimit = 2;
/**
* Retrieves a response for a given request and retrieves subsequent
* responses, with authorization headers, if a redirect is detected.
*
* @param RequestInterface $request
* @return ResponseInterface
* @throws BadResponseException
*/
protected function followRequestRedirects(RequestInterface $request)
{
$response = null;
$attempts = 0;
while ($attempts < $this->redirectLimit) {
$attempts++;
$response = $this->getHttpClient()->send($request, [
'allow_redirects' => false
]);
if ($this->isRedirect($response)) {
$redirectUrl = new Uri($response->getHeader('Location')[0]);
$request = $request->withUri($redirectUrl);
} else {
break;
}
}
return $response;
}
/**
* Returns the HTTP client instance.
*
* @return GuzzleHttp\ClientInterface
*/
abstract public function getHttpClient();
/**
* Retrieves current redirect limit.
*
* @return integer
*/
public function getRedirectLimit()
{
return $this->redirectLimit;
}
/**
* Determines if a given response is a redirect.
*
* @param ResponseInterface $response
*
* @return boolean
*/
protected function isRedirect(ResponseInterface $response)
{
$statusCode = $response->getStatusCode();
return $statusCode > 300 && $statusCode < 400 && $response->hasHeader('Location');
}
/**
* Sends a request instance and returns a response instance.
*
* WARNING: This method does not attempt to catch exceptions caused by HTTP
* errors! It is recommended to wrap this method in a try/catch block.
*
* @param RequestInterface $request
* @return ResponseInterface
*/
public function getResponse(RequestInterface $request)
{
try {
$response = $this->followRequestRedirects($request);
} catch (BadResponseException $e) {
$response = $e->getResponse();
}
return $response;
}
/**
* Updates the redirect limit.
*
* @param integer $limit
* @return League\OAuth2\Client\Provider\AbstractProvider
* @throws InvalidArgumentException
*/
public function setRedirectLimit($limit)
{
if (!is_int($limit)) {
throw new InvalidArgumentException('redirectLimit must be an integer.');
}
if ($limit < 1) {
throw new InvalidArgumentException('redirectLimit must be greater than or equal to one.');
}
$this->redirectLimit = $limit;
return $this;
}
}

View File

@@ -0,0 +1,33 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
/**
* Provides a standard way to generate query strings.
*/
trait QueryBuilderTrait
{
/**
* Build a query string from an array.
*
* @param array $params
*
* @return string
*/
protected function buildQueryString(array $params)
{
return http_build_query($params, '', '&', \PHP_QUERY_RFC3986);
}
}

View File

@@ -0,0 +1,87 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
use GuzzleHttp\Psr7\Request;
/**
* Used to produce PSR-7 Request instances.
*
* @link https://github.com/guzzle/guzzle/pull/1101
*/
class RequestFactory
{
/**
* Creates a PSR-7 Request instance.
*
* @param null|string $method HTTP method for the request.
* @param null|string $uri URI for the request.
* @param array $headers Headers for the message.
* @param string|resource|StreamInterface $body Message body.
* @param string $version HTTP protocol version.
*
* @return Request
*/
public function getRequest(
$method,
$uri,
array $headers = [],
$body = null,
$version = '1.1'
) {
return new Request($method, $uri, $headers, $body, $version);
}
/**
* Parses simplified options.
*
* @param array $options Simplified options.
*
* @return array Extended options for use with getRequest.
*/
protected function parseOptions(array $options)
{
// Should match default values for getRequest
$defaults = [
'headers' => [],
'body' => null,
'version' => '1.1',
];
return array_merge($defaults, $options);
}
/**
* Creates a request using a simplified array of options.
*
* @param null|string $method
* @param null|string $uri
* @param array $options
*
* @return Request
*/
public function getRequestWithOptions($method, $uri, array $options = [])
{
$options = $this->parseOptions($options);
return $this->getRequest(
$method,
$uri,
$options['headers'],
$options['body'],
$options['version']
);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
use BadMethodCallException;
/**
* Provides functionality to check for required parameters.
*/
trait RequiredParameterTrait
{
/**
* Checks for a required parameter in a hash.
*
* @throws BadMethodCallException
* @param string $name
* @param array $params
* @return void
*/
private function checkRequiredParameter($name, array $params)
{
if (!isset($params[$name])) {
throw new BadMethodCallException(sprintf(
'Required parameter not passed: "%s"',
$name
));
}
}
/**
* Checks for multiple required parameters in a hash.
*
* @throws InvalidArgumentException
* @param array $names
* @param array $params
* @return void
*/
private function checkRequiredParameters(array $names, array $params)
{
foreach ($names as $name) {
$this->checkRequiredParameter($name, $params);
}
}
}