PHP Trait Eloquence\Behaviours\CamelCasing

Show file Open project: kirkbushell/eloquence

Public Properties

Property Type Description
$enforceCamelCase boolean Alter eloquent model behaviour so that model attributes can be accessed via camelCase, but more importantly, attributes also get returned as camelCase fields.

Public Methods

Method Description
__isset ( $key ) : mixed Because we are changing the case of keys and want to use camelCase throughout the application, whenever we do isset checks we need to ensure that we check using snake_case.
__unset ( $key ) : void Because we are changing the case of keys and want to use camelCase throughout the application, whenever we do unset variables we need to ensure that we unset using snake_case.
attributesToArray ( ) : array Return the attributes for the model, converting field casing if necessary.
getAttribute ( string $key ) : mixed Retrieve a given attribute but allow it to be accessed via alternative case methods (such as camelCase).
getAttributes ( $keys = [] ) : array Converts the attributes to a camel-case version, if applicable.
getDates ( ) : array Overloads the eloquent getDates method to ensure that date field declarations can be made in camelCase but mapped to/from DB in snake_case.
getHidden ( ) : array Overloads eloquent's getHidden method to ensure that hidden fields declared in camelCase are actually hidden and not exposed when models are turned into arrays.
getOriginal ( string $key = null, mixed $default = null ) : array Get the model's original attribute values.
getTrueKey ( $key ) : string Retrieves the true key name for a key.
isCamelCase ( ) : boolean Determines whether the model (or its parent) requires camelcasing. This is required for pivot models whereby they actually depend on their parents for this feature.
relationsToArray ( ) : array Get the model's relationships, converting field casing if necessary.
setAttribute ( string $key, mixed $value ) Overloads the eloquent setAttribute method to ensure that fields accessed in any case are converted to snake_case, which is the defacto standard for field names in databases.
toCamelCase ( mixed $attributes ) : array Converts a given array of attribute keys to the casing required by CamelCaseModel.
toSnakeCase ( $attributes ) : array Converts a given array of attribute keys to the casing required by CamelCaseModel.

Protected Methods

Method Description
getSnakeKey ( $key ) : string If the field names need to be converted so that they can be accessed by camelCase, then we can do that here.

Method Details

__isset() public method

Because we are changing the case of keys and want to use camelCase throughout the application, whenever we do isset checks we need to ensure that we check using snake_case.
public __isset ( $key ) : mixed
$key
return mixed

__unset() public method

Because we are changing the case of keys and want to use camelCase throughout the application, whenever we do unset variables we need to ensure that we unset using snake_case.
public __unset ( $key ) : void
$key
return void

attributesToArray() public method

Return the attributes for the model, converting field casing if necessary.
public attributesToArray ( ) : array
return array

getAttribute() public method

Retrieve a given attribute but allow it to be accessed via alternative case methods (such as camelCase).
public getAttribute ( string $key ) : mixed
$key string
return mixed

getAttributes() public method

Converts the attributes to a camel-case version, if applicable.
public getAttributes ( $keys = [] ) : array
return array

getDates() public method

Overloads the eloquent getDates method to ensure that date field declarations can be made in camelCase but mapped to/from DB in snake_case.
public getDates ( ) : array
return array

getHidden() public method

Overloads eloquent's getHidden method to ensure that hidden fields declared in camelCase are actually hidden and not exposed when models are turned into arrays.
public getHidden ( ) : array
return array

getOriginal() public method

Get the model's original attribute values.
public getOriginal ( string $key = null, mixed $default = null ) : array
$key string
$default mixed
return array

getSnakeKey() protected method

If the field names need to be converted so that they can be accessed by camelCase, then we can do that here.
protected getSnakeKey ( $key ) : string
$key
return string

getTrueKey() public method

Retrieves the true key name for a key.
public getTrueKey ( $key ) : string
$key
return string

isCamelCase() public method

Determines whether the model (or its parent) requires camelcasing. This is required for pivot models whereby they actually depend on their parents for this feature.
public isCamelCase ( ) : boolean
return boolean

relationsToArray() public method

Get the model's relationships, converting field casing if necessary.
public relationsToArray ( ) : array
return array

setAttribute() public method

Overloads the eloquent setAttribute method to ensure that fields accessed in any case are converted to snake_case, which is the defacto standard for field names in databases.
public setAttribute ( string $key, mixed $value )
$key string
$value mixed

toCamelCase() public method

Converts a given array of attribute keys to the casing required by CamelCaseModel.
public toCamelCase ( mixed $attributes ) : array
$attributes mixed
return array

toSnakeCase() public method

Converts a given array of attribute keys to the casing required by CamelCaseModel.
public toSnakeCase ( $attributes ) : array
$attributes
return array

Property Details

$enforceCamelCase public property

Alter eloquent model behaviour so that model attributes can be accessed via camelCase, but more importantly, attributes also get returned as camelCase fields.
public bool $enforceCamelCase
return boolean