-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
510 additions
and
37 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace gapple\StructuredFields; | ||
|
||
class InnerList implements TupleInterface, \IteratorAggregate | ||
{ | ||
use TupleTrait; | ||
|
||
public function __construct($value, ?object $parameters = null) | ||
{ | ||
$this->value = $value; | ||
|
||
if (is_null($parameters)) { | ||
$this->parameters = new \stdClass(); | ||
} else { | ||
$this->parameters = $parameters; | ||
} | ||
} | ||
|
||
public function getIterator() | ||
{ | ||
return new \ArrayIterator($this->value); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace gapple\StructuredFields; | ||
|
||
class Item implements TupleInterface | ||
{ | ||
use TupleTrait; | ||
|
||
public function __construct($value, ?object $parameters = null) | ||
{ | ||
$this->value = $value; | ||
|
||
if (is_null($parameters)) { | ||
$this->parameters = new \stdClass(); | ||
} else { | ||
$this->parameters = $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace gapple\StructuredFields; | ||
|
||
class OuterList implements \IteratorAggregate, \ArrayAccess | ||
{ | ||
/** | ||
* The array of values. | ||
* | ||
* @var array | ||
*/ | ||
public $value; | ||
|
||
public function __construct($value = []) | ||
{ | ||
array_walk($value, [$this, 'validateItemType']); | ||
|
||
$this->value = $value; | ||
} | ||
|
||
private static function validateItemType($value): void | ||
{ | ||
if (is_object($value)) { | ||
if (!($value instanceof TupleInterface)) { | ||
throw new \InvalidArgumentException(); | ||
} | ||
} elseif (is_array($value)) { | ||
if (count($value) != 2) { | ||
throw new \InvalidArgumentException(); | ||
} | ||
} else { | ||
throw new \InvalidArgumentException(); | ||
} | ||
} | ||
|
||
public function getIterator() | ||
{ | ||
return new \ArrayIterator($this->value); | ||
} | ||
|
||
public function offsetExists($offset) | ||
{ | ||
return isset($this->value[$offset]); | ||
} | ||
|
||
public function offsetGet($offset) | ||
{ | ||
return $this->value[$offset] ?? null; | ||
} | ||
|
||
public function offsetSet($offset, $value) | ||
{ | ||
static::validateItemType($value); | ||
|
||
if (is_null($offset)) { | ||
$this->value[] = $value; | ||
} else { | ||
$this->value[$offset] = $value; | ||
} | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
unset($this->value[$offset]); | ||
} | ||
} |
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 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 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,7 @@ | ||
<?php | ||
|
||
namespace gapple\StructuredFields; | ||
|
||
interface TupleInterface extends \ArrayAccess | ||
{ | ||
} |
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,53 @@ | ||
<?php | ||
|
||
namespace gapple\StructuredFields; | ||
|
||
trait TupleTrait | ||
{ | ||
/** | ||
* The tuple's value. | ||
* | ||
* @var mixed | ||
*/ | ||
public $value; | ||
|
||
/** | ||
* The tuple's parameters | ||
* | ||
* @var object | ||
*/ | ||
public $parameters; | ||
|
||
public function offsetExists($offset): bool | ||
{ | ||
return $offset == 0 || $offset == 1; | ||
} | ||
|
||
public function offsetGet($offset) | ||
{ | ||
if ($offset == 0) { | ||
return $this->value; | ||
} elseif ($offset == 1) { | ||
return $this->parameters; | ||
} | ||
return null; | ||
} | ||
|
||
public function offsetSet($offset, $value) | ||
{ | ||
if ($offset == 0) { | ||
$this->value = $value; | ||
} elseif ($offset == 1) { | ||
$this->parameters = $value; | ||
} | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
if ($offset == 0) { | ||
$this->value = null; | ||
} elseif ($offset == 1) { | ||
$this->parameters = new \stdClass(); | ||
} | ||
} | ||
} |
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 gapple\Tests\StructuredFields; | ||
|
||
use gapple\StructuredFields\InnerList; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class InnerListTest extends TestCase | ||
{ | ||
public function testDefaultParameters() | ||
{ | ||
$item = new InnerList(true); | ||
|
||
$this->assertInstanceOf(\stdClass::class, $item->parameters); | ||
$this->assertEmpty(get_object_vars($item->parameters)); | ||
} | ||
|
||
public function testArrayAccess() | ||
{ | ||
$item = new InnerList( | ||
[ | ||
'Test Value One', | ||
'Test Value Two', | ||
], | ||
(object) ['paramKey' => 'param value'] | ||
); | ||
|
||
$this->assertEquals('Test Value One', $item->value[0]); | ||
$this->assertEquals('Test Value One', $item[0][0]); | ||
$this->assertEquals('param value', $item->parameters->paramKey); | ||
$this->assertEquals('param value', $item[1]->paramKey); | ||
} | ||
|
||
public function testIteration() | ||
{ | ||
$list = new InnerList( | ||
[ | ||
'Test Value One', | ||
'Test Value Two', | ||
], | ||
(object) ['paramKey' => 'param value'] | ||
); | ||
|
||
$this->assertIsIterable($list); | ||
|
||
$this->assertEquals( | ||
['Test Value One', 'Test Value Two'], | ||
iterator_to_array($list) | ||
); | ||
} | ||
} |
Oops, something went wrong.