| @@ 13-91 (lines=79) @@ | ||
| 10 | * |
|
| 11 | * @author Jacob Bare <[email protected]> |
|
| 12 | */ |
|
| 13 | trait EmbedsTrait |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * All embed fields assigned to this metadata object. |
|
| 17 | * An embed is a " complex object/attribute" field that supports defining sub-attributes. |
|
| 18 | * |
|
| 19 | * @var EmbeddedPropMetadata[] |
|
| 20 | */ |
|
| 21 | public $embeds = []; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * Adds an embed field. |
|
| 25 | * |
|
| 26 | * @param EmbeddedPropMetadata $embed |
|
| 27 | * @return self |
|
| 28 | */ |
|
| 29 | public function addEmbed(EmbeddedPropMetadata $embed) |
|
| 30 | { |
|
| 31 | $this->validateEmbed($embed); |
|
| 32 | $this->embeds[$embed->getKey()] = $embed; |
|
| 33 | ksort($this->embeds); |
|
| 34 | return $this; |
|
| 35 | } |
|
| 36 | ||
| 37 | /** |
|
| 38 | * Determines if an embed field exists. |
|
| 39 | * |
|
| 40 | * @param string $key |
|
| 41 | * @return bool |
|
| 42 | */ |
|
| 43 | public function hasEmbed($key) |
|
| 44 | { |
|
| 45 | return null !== $this->getEmbed($key); |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * Determines any embed fields exist. |
|
| 50 | * |
|
| 51 | * @return bool |
|
| 52 | */ |
|
| 53 | public function hasEmbeds() |
|
| 54 | { |
|
| 55 | return !empty($this->embeds); |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * Gets an embed field. |
|
| 60 | * Returns null if the embed does not exist. |
|
| 61 | * |
|
| 62 | * @param string $key |
|
| 63 | * @return EmbeddedPropMetadata|null |
|
| 64 | */ |
|
| 65 | public function getEmbed($key) |
|
| 66 | { |
|
| 67 | if (!isset($this->embeds[$key])) { |
|
| 68 | return null; |
|
| 69 | } |
|
| 70 | return $this->embeds[$key]; |
|
| 71 | } |
|
| 72 | ||
| 73 | /** |
|
| 74 | * Gets all embed fields. |
|
| 75 | * |
|
| 76 | * @return EmbeddedPropMetadata[] |
|
| 77 | */ |
|
| 78 | public function getEmbeds() |
|
| 79 | { |
|
| 80 | return $this->embeds; |
|
| 81 | } |
|
| 82 | ||
| 83 | /** |
|
| 84 | * Validates that the embed can be added. |
|
| 85 | * |
|
| 86 | * @param EmbeddedPropMetadata $embed |
|
| 87 | * @return self |
|
| 88 | * @throws MetadataException |
|
| 89 | */ |
|
| 90 | abstract protected function validateEmbed(EmbeddedPropMetadata $embed); |
|
| 91 | } |
|
| 92 | ||
| @@ 13-91 (lines=79) @@ | ||
| 10 | * |
|
| 11 | * @author Jacob Bare <[email protected]> |
|
| 12 | */ |
|
| 13 | trait RelationshipsTrait |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * All relationship fields assigned to this metadata object. |
|
| 17 | * A relationship is a field that relates to another entity. |
|
| 18 | * |
|
| 19 | * @var RelationshipMetadata[] |
|
| 20 | */ |
|
| 21 | public $relationships = []; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * Adds a relationship field to this entity. |
|
| 25 | * |
|
| 26 | * @param RelationshipMetadata $relationship |
|
| 27 | * @return self |
|
| 28 | */ |
|
| 29 | public function addRelationship(RelationshipMetadata $relationship) |
|
| 30 | { |
|
| 31 | $this->validateRelationship($relationship); |
|
| 32 | $this->relationships[$relationship->getKey()] = $relationship; |
|
| 33 | ksort($this->relationships); |
|
| 34 | return $this; |
|
| 35 | } |
|
| 36 | ||
| 37 | /** |
|
| 38 | * Gets a relationship field from this entity. |
|
| 39 | * Returns null if the relationship does not exist. |
|
| 40 | * |
|
| 41 | * @param string $key |
|
| 42 | * @return RelationshipMetadata|null |
|
| 43 | */ |
|
| 44 | public function getRelationship($key) |
|
| 45 | { |
|
| 46 | if (!isset($this->relationships[$key])) { |
|
| 47 | return null; |
|
| 48 | } |
|
| 49 | return $this->relationships[$key]; |
|
| 50 | } |
|
| 51 | ||
| 52 | /** |
|
| 53 | * Gets all relationship fields for this entity. |
|
| 54 | * |
|
| 55 | * @return RelationshipMetadata[] |
|
| 56 | */ |
|
| 57 | public function getRelationships() |
|
| 58 | { |
|
| 59 | return $this->relationships; |
|
| 60 | } |
|
| 61 | ||
| 62 | /** |
|
| 63 | * Determines if a relationship field exists on this entity. |
|
| 64 | * |
|
| 65 | * @param string $key |
|
| 66 | * @return bool |
|
| 67 | */ |
|
| 68 | public function hasRelationship($key) |
|
| 69 | { |
|
| 70 | return null !== $this->getRelationship($key); |
|
| 71 | } |
|
| 72 | ||
| 73 | /** |
|
| 74 | * Determines any relationship fields exist. |
|
| 75 | * |
|
| 76 | * @return bool |
|
| 77 | */ |
|
| 78 | public function hasRelationships() |
|
| 79 | { |
|
| 80 | return !empty($this->relationships); |
|
| 81 | } |
|
| 82 | ||
| 83 | /** |
|
| 84 | * Validates that the relationship can be added. |
|
| 85 | * |
|
| 86 | * @param RelationshipMetadata $relationship |
|
| 87 | * @return self |
|
| 88 | * @throws MetadataException |
|
| 89 | */ |
|
| 90 | abstract protected function validateRelationship(RelationshipMetadata $relationship); |
|
| 91 | } |
|
| 92 | ||