This is still in active development. Use with certain pre-caution.
Requires an intermediate knowledge in Laravel and Software Design Patterns
Collection of Laravel utilities
composer require exylon/fuse:dev-master
Creates a new Subscriber class based on Laravel Subscriber
Parameters | Description | Example |
---|---|---|
name |
Subscriber class name | AuthEventsSubscriber |
Options | Description | Example |
---|---|---|
--event= |
The event class/es being listened for. | Illuminate\\Auth\\Events\\Login |
Basic Usage:
$ php artisan make:subscriber AuthEventsSubscriber --event="Illuminate\\Auth\\Events\\Login" --event="Illuminate\\Auth\\Events\\Logout"
Creates a new repository class
Parameters | Description | Example |
---|---|---|
model |
Target model | User |
Options | Description | Example |
---|---|---|
--no-interface |
By default, a repository interface and an Eloquent implementation will be created. If you wish to use Eloquent implementation solely, add this as option |
Basic Usage:
$ php artisan make:repository User --no-interface
Creates a new service class.
Parameters | Description | Example |
---|---|---|
name |
Service class name | UserService |
Options | Description | Example |
---|---|---|
--repository= |
Provides the repository to be used by the service | UserRepository |
--crud |
Adds create, update and delete methods |
Basic Usage:
$ php artisan make:service UserService -r=UserRepository --crud
String replace using key-value pair (assoc array).
Shorthand for \Validator::validate($data, $rules)
Generates a random hexadecimal string with fixed length.
Example: $var = str_random_hex(10) //ffeb09ed56
Generates a random numeric string. If the generated numeric string is shorter than the $length
, it will be padded by the $pad
.
Example: $var = str_random_int(5) //01467
Converts a string to proper cased string
Examples:
$var = proper_case('lorem_ipsum_dolor') // Lorem Ipsum Dolor
$var = proper_case('lorem_ipsum-dolor') // Lorem Ipsum-Dolor
$var = proper_case('lorem_ipsum-dolor',['_','-']) // Lorem Ipsum Dolor
Associative array on steroids. Converts regular associative array to standard objects
$arr = new Attributes([
'red' => 'apple',
'orange' => 'orange',
'yellow' => [
'mangoes' => 'foo',
'pear' => 'bar'
]
]);
echo $arr['red']; // "apple"
echo $arr->red; // "apple"
echo $arr['yellow']['mangoes']; // "foo"
echo $arr->yellow->mangoes; // "foo"
Working with aliases. Note: Currently, aliases supports the first level keys only
$arr = new Attributes([
'red' => 'apple',
'orange' => 'orange',
'yellow' => [
'mangoes' => 'foo',
'pear' => 'bar'
]
],[ // 'pula' as an alias for 'red'
'pula' => 'red'
]);
echo $arr['red']; // "apple"
echo $arr->red; // "apple"
echo $arr['pula']; // "apple"
echo $arr->pula; // "apple"
Converts the attributes to json
Enables cascade delete on the PHP side. Useful for polymorphic relationships.
/*
* 'Team' model can also have payment methods. That's the reason
* PaymentMethod is poloymorphic.
*/
class Customer extends Model {
use CascadeDelete;
protected $cascade = [
'paymentMethods'
];
public function paymentMethods(){
return $this->morphMany(PaymentMethod::class,'owner');
}
}
...
$customer->delete(); // this will delete all related payment methods
Using torann/geoip
,
\Exylon\Fuse\Support\Attributes {
#attributes: array [
"ip" => "127.0.0.0"
"iso_code" => "PH"
"country" => "Philippines"
"city" => "Paranaque"
"state" => "MNL"
"state_name" => "Manila"
"postal_code" => "06510"
"lat" => 14.471016
"lon" => 121.01476
"timezone" => "Asia/Manila"
"continent" => "NA"
"currency" => "USD"
"default" => true
"cached" => false
]
#aliases: array [
"country_code" => "iso_code"
"latitude" => "lat"
"longitude" => "lon"
"zip_code" => "postal_code"
]
}
Using jenssegers/agent
\Exylon\Fuse\Support\Attributes {
#attributes: array [
"agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
"is_mobile" => false
"is_phone" => false
"is_tablet" => false
"device" => "Macintosh"
"is_desktop" => true
"platform" => "OS X"
"is_robot" => false
"robot" => false
"browser" => "Chrome"
"languages" => array:2 [▶]
]
}
Coincides with Eloquent Models' forceCreate
, this method creates an instance of the model without persisting and avoiding MassAssignmentException.
NOTE: Make sure that you pre-validated the attributes.
$user = User::forceMake(['name'=>'John Doe']);
Cleans up your data based on rules.
$data = [
'email' => ' [email protected] '
];
$data = FuseSanitizer::sanitize($data,[
'*' => 'trim::string', // Wildcard
'email' => 'strtolower' // Applicable only fields named 'email'
]); // ['email' => '[email protected]']
The 'email' rule will be only applicable to fields with name 'email'. Int this case, strtolower
will be called if there exists an 'email' field from the data.
The *
signifies a wildcard which makes it applicable to "any" field, but take note that the included rule is trim::string
. The rule is divided into three parts - function, parameters, data type -, which is separated by colon (':
'); parameters are further divided by comma (',
'). By default, data type will be assumed as string. In our case, for the function trim
we don't need any parameters but we need to supply a data type to make it applicable only to string
-typed data. Available data types are string
,array
, int
, float
, and double
.
Sample:
FuseSanitizer::setGlobalRules([
'email' => 'trim'
]);
$data = [
'email' => ' [email protected] '
];
$data = FuseSanitizer::sanitize($data,[
'email' => 'strtolower'
]); // ['email' => '[email protected]']
The rules are prioritized in this manner:
- Global Wildcard
- Global Rule
- Inline Wildcard
- Inline Rule
If you set the global rules, the inline rules are optional. Sample:
FuseSanitizer::setGlobalRules([
'email' => ['trim','strtolower']
]);
$data = [
'email' => ' [email protected] '
];
$data = FuseSanitizer::sanitize($data); // ['email' => '[email protected]']
Sample:
$email = FuseSanitizer::sanitizeValue(' [email protected] ',['trim','strtolower']); // '[email protected]'
$email = FuseSanitizer::sanitizeValue(' [email protected] ','trim|strtolower'); // '[email protected]'
$email = FuseSanitizer::sanitizeValue(' [email protected] ', ['trim','strtolower']); // '[email protected]'
$email = FuseSanitizer::sanitizeValue(' [email protected] ', ['trim',function($value){
return strtolower($value);
}]); // '[email protected]'
$email = FuseSanitizer::sanitizeValue(' [email protected] ', ['trim','App\\Support\\SanitizerHelper@toLower']); // '[email protected]'
FuseSanitizer::register('tolower',function($value){
return strtolower($value);
});
$email = FuseSanitizer::sanitizeValue(' [email protected] ', ['trim','tolower']); // '[email protected]'
FuseSanitizer::register('tolower','App\\Support\\SanitizerHelper@toLower');
$email = FuseSanitizer::sanitizeValue(' [email protected] ', ['trim','tolower']); // '[email protected]'