| @@ 22-75 (lines=54) @@ | ||
| 19 | * |
|
| 20 | * @see AbstractDelegateBuilder |
|
| 21 | */ |
|
| 22 | abstract class AbstractDelegateContextualBuilder extends AbstractDelegateBuilder implements ContextualBuilder |
|
| 23 | { |
|
| 24 | ||
| 25 | /** |
|
| 26 | * Properties |
|
| 27 | */ |
|
| 28 | ||
| 29 | /** |
|
| 30 | * Whether or not to provide a fallback empty context, when a `null` context |
|
| 31 | * is otherwise provided, to make processes simpler by not having to rely on |
|
| 32 | * null checks of the actual parameter before usage. |
|
| 33 | * |
|
| 34 | * @var bool |
|
| 35 | */ |
|
| 36 | private $provide_fallback_context = false; |
|
| 37 | ||
| 38 | ||
| 39 | /** |
|
| 40 | * Methods |
|
| 41 | */ |
|
| 42 | ||
| 43 | /** |
|
| 44 | * Constructor |
|
| 45 | * |
|
| 46 | * @param bool $provide_fallback_context Whether or not to provide a |
|
| 47 | * fallback empty context, when a `null` context is otherwise provided, to |
|
| 48 | * make processes simpler by not having to rely on null checks of the |
|
| 49 | * actual parameter before usage. |
|
| 50 | */ |
|
| 51 | protected function __construct(bool $provide_fallback_context = false) |
|
| 52 | { |
|
| 53 | $this->provide_fallback_context = $provide_fallback_context; |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * {@inheritdoc} |
|
| 58 | * |
|
| 59 | * @param mixed $incoming The input data. |
|
| 60 | * @param Map|null $context An optional generic key-value map, for providing |
|
| 61 | * contextual values during the build process. |
|
| 62 | * @return mixed The built model. |
|
| 63 | */ |
|
| 64 | public function build($incoming, Map $context = null) |
|
| 65 | { |
|
| 66 | $callable = $this->getDelegate(); |
|
| 67 | ||
| 68 | if (null === $context && $this->provide_fallback_context) { |
|
| 69 | // Provide a non-null context so null checks aren't later required |
|
| 70 | $context = new Map(); |
|
| 71 | } |
|
| 72 | ||
| 73 | return $callable($incoming, $context); |
|
| 74 | } |
|
| 75 | } |
|
| 76 | ||
| @@ 22-76 (lines=55) @@ | ||
| 19 | * |
|
| 20 | * @see AbstractDelegateHydrator |
|
| 21 | */ |
|
| 22 | abstract class AbstractDelegateContextualHydrator extends AbstractDelegateHydrator implements ContextualHydrator |
|
| 23 | { |
|
| 24 | ||
| 25 | /** |
|
| 26 | * Properties |
|
| 27 | */ |
|
| 28 | ||
| 29 | /** |
|
| 30 | * Whether or not to provide a fallback empty context, when a `null` context |
|
| 31 | * is otherwise provided, to make processes simpler by not having to rely on |
|
| 32 | * null checks of the actual parameter before usage. |
|
| 33 | * |
|
| 34 | * @var bool |
|
| 35 | */ |
|
| 36 | private $provide_fallback_context = false; |
|
| 37 | ||
| 38 | ||
| 39 | /** |
|
| 40 | * Methods |
|
| 41 | */ |
|
| 42 | ||
| 43 | /** |
|
| 44 | * Constructor |
|
| 45 | * |
|
| 46 | * @param bool $provide_fallback_context Whether or not to provide a |
|
| 47 | * fallback empty context, when a `null` context is otherwise provided, to |
|
| 48 | * make processes simpler by not having to rely on null checks of the |
|
| 49 | * actual parameter before usage. |
|
| 50 | */ |
|
| 51 | protected function __construct(bool $provide_fallback_context = false) |
|
| 52 | { |
|
| 53 | $this->provide_fallback_context = $provide_fallback_context; |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * {@inheritdoc} |
|
| 58 | * |
|
| 59 | * @param mixed $incoming The input data. |
|
| 60 | * @param mixed $model The model to hydrate. |
|
| 61 | * @param Map|null $context An optional generic key-value map, for providing |
|
| 62 | * contextual values during the hydrate process. |
|
| 63 | * @return mixed The hydrated model. |
|
| 64 | */ |
|
| 65 | public function hydrate($incoming, $model, Map $context = null) |
|
| 66 | { |
|
| 67 | $callable = $this->getDelegate(); |
|
| 68 | ||
| 69 | if (null === $context && $this->provide_fallback_context) { |
|
| 70 | // Provide a non-null context so null checks aren't later required |
|
| 71 | $context = new Map(); |
|
| 72 | } |
|
| 73 | ||
| 74 | return $callable($incoming, $model, $context); |
|
| 75 | } |
|
| 76 | } |
|
| 77 | ||