Download the PHP package monamoxie/vocab-mapper without Composer
On this page you can find all versions of the php package monamoxie/vocab-mapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download monamoxie/vocab-mapper
More information about monamoxie/vocab-mapper
Files in monamoxie/vocab-mapper
Package vocab-mapper
Short Description Supercharge your application by providing a seamless and personalized experience. Vocab Mapper ensures that users across different regions or domains can easily interact with and navigate your application, even if they use different vocabulary for the same concepts
License MIT
Homepage https://github.com/Monamoxie/vocab-mapper
Informations about the package vocab-mapper
VOCAB MAPPER
A Laravel package for both multi-tenant and single tenant architectures.
Vocab Mapper provides your tenants or users a personalized experience, by defining mappings between standard terminology (The Default Vocab) and user-preferred terminology (The Custom Vocab), especially when both terminologies are referring to the same core concept.
INSTALLATION
VENDOR PUBLISHING
Vocab Mapper includes some default configurations out of the box, but it's recommended you publish and customize these settings to suit your needs.
PUBLISHING THE CONFIG
EXPLORING THE VOCAB CONFIG
MIGRATION SUB DIR
This is the subdirectory where Vocab Mapper's migration files will be published. The default value is null, suitable for most single-tenant applications.
For multi-tenant systems needing a different subdirectory for tenant-specific migrations, you can simply set the subdirectory value here. For example, the stancl/tenancy multi-tenant package requires tenant migrations be kept in database/migrations/tenant
.
If that is the case, then you should set the value of migration_sub_dir => '/tenant'
ENTITY MODEL
An entity in your application is a unique tenant or user who whiches to use a custom vocabulary for a core concept, different from the standard term.
Single-tenant systems
In single-tenant systems, this is typically the model class representing your users. For example, \App\Models\User::class
, or whichever model namespace manages and identifies your users.
key | type | Default Value |
---|---|---|
entity_model |
string,array |
\App\Models\User::class |
Multi-tenant systems
In multi-tenant systems, this is typically the model class representing the tenant. For example, \App\Models\Tenant::class
, or whichever model namespace manages and identifies your tenants.
key | type | Recommended |
---|---|---|
user_entity |
string,array |
\App\Models\Tenant::class |
Please refer to the section on BREAK DOWN OF TERMS for a more information on this.
ENTITY HAS UUID
Enable this option if the entity model utilizes a UUID as its primary key.
key | type | Default Value |
---|---|---|
user_entity_has_uuid |
boolean |
false |
PUBLISHING THE MIGRATION
This action will publish the migration files based on the provided configuration in migration_sub_dir
. If no specific path is set, the files will be published to Laravel's default migration directory.
MIGRATION
Run the artisan command responsible for handling your Database migrations.
Single-tenant systems
Multi-tenant systems
The command to execute depends on your setup or package configuration. For example, projects utilizing the stancl/tenancy package would execute:
MAKING YOUR ELOQUENT MODELS VOCABLE
A vocab is a name, identity or a domain driven terminology used by default across your application. In an educational project, it could be the word SUBJECTS
for storing all the subjects taught in a school. In an automotive project, it could be the word VEHICLES
for identifying all vehicles available in your system.
To make any of your models VOCABLE
, (which is the ability to maintain and be represented in a different term across multiple tenants or users), you should include the HasVocab
trait to the model
That's all.
With this, this model, MyModel
, should now be ready to accept and maintain different identites across each of your tenants or users.
CREATING A DEFAULT VOCAB
From any part of your application, you can call
The name
argument is optional. If not provided, the default vocab (term, identity) for this model would the table name.
MAPPING A VOCAB TO AN ENTITY
From any part of your application, you can call
where:
This entity could be the currently logged in tenant or currently logged in user. It doesn't matter.
You must ensure the instance of this entity matches what you defined in the config, entity_model
.
If there is a mis-match between the $entity instance and whatever was set in the config, entity_model
, this will thrown an exception of type:
THE DUALITY OF mapVocabTo
If you attempt to map a vocab to an entity and Vocab Mapper detects that the vocab doesn't exist, it will silently create a new vocab and proceed with the mapping.
GETTING THE VOCAB MAPPED TO AN ENTITY
From any part of your application, you can call
where:
THE VOCAB COLLECTOR
Alternatively, you could also take advantage of the Vocab Collector for getting the vocab mapped to a user:
where:
THE DIFFERENCE BETWEEN getVocabFor and VocabCollector::getFor
(new MyModel)->getVocabFor($entity)
will return an eloquent model, which is an instance of Monamoxie\VocabMapper\VocabMapper
You may want to use this to inspect the model or for any eloquent operations.
Alternatively, VocabCollector::getFor
returns an instance of Monamoxie\VocabMapper\Data\VocabResponse
, which is ideal for displaying this data in the client-facing part of your application.
The VocabResponse
provides the custom vocabulary defined by the entity, including its singular and plural forms.
For instance, you could have a setup like this in your controller:
And if it's an API driven setup, you could also have something like this. It doesn't matter.
You are also not limited to working with just the instance of Monamoxie\VocabMapper\Data\VocabResponse
.
You can convert the response into an array by calling the toArray()
method on it.
BREAK DOWN OF TERMS
Entity
An entity in your application is an authenticated and unique tenant or user who uses a custom vocabulary for a core concept, different from the standard term.
Vocab
The name of the concept you provide, as known or defined by you. By default, the vocab is the model's table name.
Vocab Mapper
A mapping of custom names to existing vocabs. It is advisable to store custom names in their plural form. For instance, use "subjects" instead of "subject." The VocabResponse will handle the conversion between singular and plural forms in its response.
Handler
The class namespace responsible for managing this vocab, typically the model class for that vocab, but it can also be a non-model class.
TESTING
For console-based coverage tests:
OR
For html-based coverage tests:
CONTRIBUTING
LICENSE
The MIT License (MIT). Please see License File for more information.