-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from PhpGt/7-file
Improve API design while moving towards FileUpload support
- Loading branch information
Showing
27 changed files
with
504 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
|
||
use Gt\Http\Stream; | ||
|
||
class Body extends Stream {} | ||
class BodyStream extends Stream {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
namespace Gt\Input\InputData; | ||
|
||
use ArrayAccess; | ||
use Countable; | ||
use Iterator; | ||
|
||
abstract class AbstractInputData implements ArrayAccess, Countable, Iterator { | ||
use KeyValueArrayAccess; | ||
use KeyValueCountable; | ||
use KeyValueIterator; | ||
|
||
/** @var InputDatum[] */ | ||
protected $parameters = []; | ||
|
||
public function get(string $key):?InputDatum { | ||
return $this->parameters[$key] ?? null; | ||
} | ||
|
||
protected function set(string $key, InputDatum $value):void { | ||
$this->parameters[$key] = $value; | ||
} | ||
|
||
public function withKeyValue(string $key, InputDatum $value):self { | ||
$clone = clone($this); | ||
$clone->parameters[$key] = $value; | ||
return $clone; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
namespace Gt\Input\InputData; | ||
|
||
class BodyInputData extends InputData {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
namespace Gt\Input\InputData; | ||
|
||
class CombinedInputData extends InputData {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace Gt\Input\InputData; | ||
|
||
class FileUploadInputData extends InputData { | ||
|
||
public function __construct(array $files) { | ||
$files = $this->normalizeArray($files); | ||
|
||
// TODO: Set $this->data with kvp of files ($files[filename] => FileUpload(data)) | ||
} | ||
|
||
/** | ||
* The files array is an associative array where the key is the name of the request | ||
* parameter, and the value is another associative array with keys: | ||
* + name | ||
* + type | ||
* + tmp_name | ||
* + error | ||
* + size | ||
* Each key's value is string, unless the request parameter name ends with [], in which case | ||
* each value is another array. This function normalises the array to the latter. | ||
*/ | ||
protected function normalizeArray($files):array { | ||
foreach($files as $parameterName => $fileDetailArray) { | ||
foreach($fileDetailArray as $key => $value) { | ||
if(!is_array($value)) { | ||
$files[$parameterName][$key] = [$value]; | ||
} | ||
} | ||
} | ||
|
||
return $files; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace Gt\Input\InputData; | ||
|
||
class InputData extends AbstractInputData { | ||
public function __construct(iterable...$sources) { | ||
$this->parameters = []; | ||
|
||
foreach($sources as $source) { | ||
foreach($source as $key => $value) { | ||
if(!$value instanceof InputDatum) { | ||
$value = new InputDatum($value); | ||
} | ||
$this->add($key, $value); | ||
} | ||
} | ||
} | ||
|
||
public function add(string $key, InputDatum $datum):self { | ||
$this->parameters[$key] = $datum; | ||
return $this; | ||
} | ||
|
||
public function addKeyValue(string $key, string $value):self { | ||
$datum = new InputDatum($value); | ||
return $this->add($key, $datum); | ||
} | ||
|
||
public function remove(string...$keys):self { | ||
foreach($keys as $key) { | ||
if(isset($this->parameters[$key])) { | ||
unset($this->parameters[$key]); | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function removeExcept(string...$keys):self { | ||
foreach($this->parameters as $key => $value) { | ||
if(!in_array($key, $keys)) { | ||
unset($this->parameters[$key]); | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function getKeys():array { | ||
return array_keys($this->parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.