-
Notifications
You must be signed in to change notification settings - Fork 1
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 #11 from discoverygarden/feature/early-exit
PER-42: Spec used fields explicitly.
- Loading branch information
Showing
6 changed files
with
142 additions
and
0 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
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 Drupal\iiif_presentation_api; | ||
|
||
use Drupal\Core\DependencyInjection\DependencySerializationTrait; | ||
|
||
/** | ||
* Help passing around some service metadata. | ||
*/ | ||
class FieldMapper implements FieldMapperInterface { | ||
|
||
use DependencySerializationTrait; | ||
|
||
/** | ||
* The mapped fields for which the mapping is being built. | ||
* | ||
* @var \Drupal\iiif_presentation_api\MappedFieldInterface[] | ||
*/ | ||
protected array $mappers = []; | ||
|
||
/** | ||
* Memoized mapping, mapping entity type IDs to arrays of field names. | ||
* | ||
* @var string[][] | ||
*/ | ||
protected array $mapping = []; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function addMapper(MappedFieldInterface $mapping) : FieldMapperInterface { | ||
$this->mappers[] = $mapping; | ||
if (!$this->isInMapping($mapping->getTargetEntityTypeId(), $mapping->getTargetFieldName())) { | ||
$this->mapping[$mapping->getTargetEntityTypeId()][] = $mapping->getTargetFieldName(); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getMapping() : array { | ||
return $this->mapping; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function isInMapping(string $entity_type_id, string $field_name) : bool { | ||
return !empty($this->mapping[$entity_type_id]) && in_array($field_name, $this->mapping[$entity_type_id]); | ||
} | ||
|
||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace Drupal\iiif_presentation_api; | ||
|
||
/** | ||
* Generic service interface in which to build out a mapping. | ||
*/ | ||
interface FieldMapperInterface { | ||
|
||
/** | ||
* Add a type/field pair to the mapping. | ||
* | ||
* @param \Drupal\iiif_presentation_api\MappedFieldInterface $mapping | ||
* The item containing the type/field pair. | ||
* | ||
* @return self | ||
* Fluent interface. | ||
*/ | ||
public function addMapper(MappedFieldInterface $mapping) : self; | ||
|
||
/** | ||
* Get the mapping of types to field names. | ||
* | ||
* @return string[][] | ||
* An associative array mapping entity type IDs to arrays of field names. | ||
*/ | ||
public function getMapping() : array; | ||
|
||
/** | ||
* Test if the given item is present. | ||
* | ||
* @param string $entity_type_id | ||
* The entity type for which to test. | ||
* @param string $field_name | ||
* The field name for which to test. | ||
* | ||
* @return bool | ||
* TRUE if present; otherwise, FALSE. | ||
*/ | ||
public function isInMapping(string $entity_type_id, string $field_name) : bool; | ||
|
||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Drupal\iiif_presentation_api; | ||
|
||
/** | ||
* Field specific normalizer interface. | ||
*/ | ||
interface MappedFieldInterface { | ||
|
||
/** | ||
* Get the target entity type. | ||
* | ||
* @return string | ||
* The target entity type. | ||
*/ | ||
public function getTargetEntityTypeId() : string; | ||
|
||
/** | ||
* Get the target field name. | ||
* | ||
* @return string | ||
* The target field name. | ||
*/ | ||
public function getTargetFieldName() : string; | ||
|
||
} |
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