-
-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add missing Laravel casts #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
binaryfire
wants to merge
18
commits into
hypervel:main
Choose a base branch
from
binaryfire:feature/add-missing-laravel-casts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add missing Laravel casts #342
binaryfire
wants to merge
18
commits into
hypervel:main
from
binaryfire:feature/add-missing-laravel-casts
Conversation
This file contains hidden or 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
Port Laravel's verifyConfiguration() method and its supporting methods (isUsingCorrectAlgorithm, isUsingValidOptions) to verify that a hash was created with valid configuration parameters.
Port Laravel's verifyConfiguration() method and its supporting methods (isUsingCorrectAlgorithm, isUsingValidOptions) to verify that an Argon2i hash was created with valid configuration parameters (memory, time, threads).
Override isUsingCorrectAlgorithm() to check for 'argon2id' algorithm. This allows the inherited verifyConfiguration() from ArgonHasher to work correctly for Argon2id hashes.
Delegate verifyConfiguration() to the underlying driver to verify that a hash was created with valid configuration parameters.
Add @method annotation for verifyConfiguration() for IDE autocompletion and static analysis support.
Add comprehensive test coverage for the new verifyConfiguration() method across all hashers (Bcrypt, Argon, Argon2id) and HashManager. Tests cover: - Valid hash verification - Lower cost/options verification (should pass) - Higher cost/options verification (should fail) - Wrong algorithm verification (should fail) - HashManager delegation to driver
Add the 10 missing cast types from Laravel: - encrypted, encrypted:array, encrypted:collection, encrypted:json, encrypted:object - hashed - immutable_date, immutable_datetime, immutable_custom_datetime - json:unicode
Add methods to support encrypted casts: - $encrypter static property for custom encrypter - encryptUsing() to set custom encrypter - currentEncrypter() to get the current encrypter - isEncryptedCastable() to detect encrypted cast types - fromEncryptedString() to decrypt values - castAttributeAsEncryptedString() to encrypt values
Add castAttributeAsHashedString() to support the 'hashed' cast type. The method hashes plain text values and verifies configuration for already-hashed values to prevent accepting hashes with invalid config.
Add support for: - encrypted casts (decrypt before processing, strip encrypted: prefix) - json:unicode (same as array/json on GET) - immutable_date (returns CarbonImmutable) - immutable_datetime (returns CarbonImmutable) - immutable_custom_datetime (returns CarbonImmutable with custom format) Also adds isImmutableCustomDateTimeCast() helper method and getCastType() override to properly detect immutable custom datetime casts.
Replace switch statement with PHP 8 match expression for cleaner code. Extract default case handling into castAttributeDefault() helper method.
Add support for: - Encrypted casts on SET (encrypt before storing) - Hashed cast on SET (hash before storing) - json:unicode on SET (use JSON_UNESCAPED_UNICODE flag) Also adds: - getJsonCastFlags() to determine JSON encoding flags - castAttributeAsJson() override to use flags - asJson() override to accept flags parameter
Add json:unicode and encrypted:* types to the list of JSON-castable types to ensure proper JSON encoding on SET for these cast types.
PHP traits cannot override static properties from parent classes/traits. Move the $primitiveCastTypes property to the Model class instead to avoid the composition conflict with Hyperf's base model.
Comprehensive tests for: - immutable_date cast (returns CarbonImmutable) - immutable_datetime cast (returns CarbonImmutable) - immutable_custom_datetime cast (returns CarbonImmutable) - json:unicode cast (preserves unicode characters) - encrypted cast (encrypts/decrypts values) - encrypted:array cast - encrypted:collection cast - encrypted:object cast - hashed cast (hashes plain text, preserves existing hashes)
- Cast decimal precision to int in castAttribute() - Change setAttribute return type annotation to @return static - Add $primitiveCastTypes to Pivot and MorphPivot classes (they use HasAttributes trait but don't extend Hypervel\Model)
- Test null value handling for encrypted casts (SET and GET) - Test null value handling for hashed cast (SET) - Test encryptUsing() with custom encrypter - Test currentEncrypter() returns custom encrypter when set - Test encrypter can be reset and replaced
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds the 10 primitive cast types that Laravel has but Hypervel/Hyperf lacks, bringing Hypervel's model casting capabilities to feature parity with Laravel.
New Cast Types
encryptedencrypted:arrayencrypted:collectionencrypted:jsonencrypted:objecthashedimmutable_dateimmutable_datetimeimmutable_custom_datetimejson:unicodeJSON_UNESCAPED_UNICODEflagChanges
Hash Package
verifyConfiguration()toBcryptHasher,ArgonHasher,Argon2IdHasher, andHashManager@methodannotation toHashfacade for IDE supportCore Package (HasAttributes)
encryptUsing(),currentEncrypter(),isEncryptedCastable(),fromEncryptedString(),castAttributeAsEncryptedString()castAttributeAsHashedString()getJsonCastFlags(),isJsonCastable()overrideisImmutableCustomDateTimeCast()castAttribute()to handle new cast types (refactored to usematchexpression)setAttribute()to encrypt/hash values on set$primitiveCastTypestoModel,Pivot, andMorphPivotclassesUsage Examples
Test Coverage
verifyConfiguration()across all hashersencryptUsing()custom encrypter API (3 tests)