Skip to content

Commit a79f5bd

Browse files
committed
Added documentation to readme and accompanying DocumentationTest.
Added documentation to strategy docblocks. Renamed ocurrences of $strategyOrMapping -> $expression. Renamed InvalidMapperTypeException -> InvalidExpressionException.
1 parent 88f258d commit a79f5bd

24 files changed

Lines changed: 935 additions & 73 deletions

README.md

Lines changed: 526 additions & 2 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scriptfusion/mapper",
3-
"description": "Maps data from one format to another using an object composition DSL.",
3+
"description": "Transforms arrays using an object composition DSL.",
44
"authors": [
55
{
66
"name": "Bilge",

src/InvalidExpressionException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace ScriptFUSION\Mapper;
3+
4+
/**
5+
* The exception that is thrown when an invalid expression is specified.
6+
*/
7+
class InvalidExpressionException extends \RuntimeException
8+
{
9+
// Intentionally empty.
10+
}

src/InvalidMapperTypeException.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Mapper.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ class Mapper
1010
{
1111
/**
1212
* @param array $record
13-
* @param Strategy|Mapping|array|mixed $strategyOrMapping
13+
* @param Strategy|Mapping|array|mixed $expression
1414
* @param mixed $context
1515
*
1616
* @return mixed
1717
*
18-
* @throws InvalidMapperTypeException An invalid strategy or mapping object was specified.
18+
* @throws InvalidExpressionException An invalid strategy or mapping object was specified.
1919
*/
20-
public function map(array $record, $strategyOrMapping, $context = null)
20+
public function map(array $record, $expression, $context = null)
2121
{
2222
/* Strategy. */
23-
if ($strategyOrMapping instanceof Strategy) {
24-
return $this->mapStrategy($record, $strategyOrMapping, $context);
23+
if ($expression instanceof Strategy) {
24+
return $this->mapStrategy($record, $expression, $context);
2525
} /* Mapping. */
26-
elseif ($strategyOrMapping instanceof Mapping) {
27-
return $this->mapMapping($record, $strategyOrMapping, $context);
26+
elseif ($expression instanceof Mapping) {
27+
return $this->mapMapping($record, $expression, $context);
2828
} /* Mapping fragment. */
29-
elseif (is_array($strategyOrMapping)) {
30-
return $this->mapFragment($record, $strategyOrMapping, $context);
29+
elseif (is_array($expression)) {
30+
return $this->mapFragment($record, $expression, $context);
3131
} /* Scalar values. */
32-
elseif (is_scalar($strategyOrMapping)) {
33-
return $strategyOrMapping;
32+
elseif (is_scalar($expression)) {
33+
return $expression;
3434
}
3535

36-
throw new InvalidMapperTypeException('Invalid strategy or mapping: "' . get_class($strategyOrMapping) . '".');
36+
throw new InvalidExpressionException('Invalid strategy or mapping: "' . get_class($expression) . '".');
3737
}
3838

3939
/**

src/Strategy/Callback.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
namespace ScriptFUSION\Mapper\Strategy;
33

4+
/**
5+
* Augments data using the return value of the specified callback.
6+
*/
47
class Callback implements Strategy
58
{
69
private $callback;

src/Strategy/Collection.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
use ScriptFUSION\Mapper\Mapping;
55

66
/**
7-
* Decorates a data collection by applying a transformation to each datum.
7+
* Decorates a collection of data by applying a transformation to each datum using a callback.
88
*/
99
class Collection extends Delegate
1010
{
1111
private $transformation;
1212

1313
/**
14-
* Initializes this instance with the specified strategy or mapping that
15-
* yields a data collection and the specified strategy or mapping that
16-
* describes how to transform each datum in the collection.
14+
* Initializes this instance with the specified strategy or mapping that yields a data collection and the specified
15+
* strategy or mapping that describes how to transform each datum in the collection.
1716
*
18-
* @param Strategy|Mapping|array|mixed $collection Data collection.
19-
* @param Strategy|Mapping|array|mixed $transformation Transformation
20-
* strategy or mapping fragment.
17+
* @param Strategy|Mapping|array|mixed $collection Data collection expression that maps to an array
18+
* @param Strategy|Mapping|array|mixed $transformation Transformation expression.
19+
* The current datum is passed as context.
2120
*/
2221
public function __construct($collection, $transformation)
2322
{

src/Strategy/Context.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
<?php
22
namespace ScriptFUSION\Mapper\Strategy;
33

4-
class Context extends Decorator
4+
use ScriptFUSION\Mapper\Mapping;
5+
6+
/**
7+
* Replaces the context for the specified expression.
8+
*/
9+
class Context extends Delegate
510
{
6-
private $strategyOrMapping;
11+
private $expression;
712

8-
public function __construct(Strategy $strategy, $strategyOrMapping)
13+
/**
14+
* @param Strategy|Mapping|array|mixed $expression Expression.
15+
* @param Strategy|Mapping|array|mixed $context New context.
16+
*/
17+
public function __construct($expression, $context)
918
{
10-
parent::__construct($strategy);
19+
parent::__construct($expression);
1120

12-
$this->strategyOrMapping = $strategyOrMapping;
21+
$this->expression = $context;
1322
}
1423

1524
public function __invoke($data, $context = null)
1625
{
17-
return parent::__invoke($data, $this->delegate($this->strategyOrMapping, $data, $context));
26+
return parent::__invoke($data, $this->delegate($this->expression, $data, $context));
1827
}
1928
}

src/Strategy/Copy.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
use ScriptFUSION\ArrayWalker\ArrayWalker;
55

6+
/**
7+
* Copies a portion of input data.
8+
*/
69
class Copy implements Strategy
710
{
811
const PATH_SEPARATOR = '->';
@@ -12,7 +15,7 @@ class Copy implements Strategy
1215
/**
1316
* Initializes this instance with the specified path.
1417
*
15-
* @param array|string $path Path.
18+
* @param array|string $path Array of path components or string of `->`-delimited components.
1619
*/
1720
public function __construct($path)
1821
{
@@ -27,7 +30,7 @@ public function __construct($path)
2730
*/
2831
public function __invoke($data, $context = null)
2932
{
30-
if (is_array($data)) {
33+
if ($this->path && is_array($data)) {
3134
return ArrayWalker::walk($data, $this->path);
3235
}
3336
}

src/Strategy/CopyContext.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<?php
22
namespace ScriptFUSION\Mapper\Strategy;
33

4+
/**
5+
* Copies a portion of context data.
6+
*/
47
class CopyContext extends Copy
58
{
69
private $walk;
710

11+
/**
12+
* {@inheritdoc}
13+
*
14+
* @param array|string $path Array of path components or string of `->`-delimited components.
15+
*/
816
public function __construct($path = null)
917
{
1018
parent::__construct($path);

0 commit comments

Comments
 (0)