depotwarehouse / toolbox
A set of tools in PHP
Installs: 12 467
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: >=5.6.0
- illuminate/database: ^5.3
- illuminate/support: ^5.3
- illuminate/validation: ^5.3
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ^5.4
README
This is a standard library of tools that abstracts common work needed to be done across all projects.
Database Management
There are a couple of classes that make working with databases easier in projects. This is designed for use with Laravel, as they expose Eloquent models, however Laravel is not strictly necessary. The repository pattern
allows for greater testability and easier to understand controllers. In your projects, simply create repositories that
extend from ActiveRepositoryAbstract
. For typehints you can use the contract ActiveRepository
.
The active repository requires a Validator
class to validate data processed within - this class is passed in via the constructor.
You should either create your own validator implementing the Validator
contract, or you can typehint the NullValidator
to perform no validation on the model whatsoever.
class MyActiveRepository extends ActiveRepositoryAbstract { public function __construct(\Illuminate\Database\Eloquent\Model $model, \Depotwarehouse\Toolbox\Validation\NullValidator $validator) { $this->model = $model; $this->validator = $validator; } }
Validators are simple to implement. They were designed with Laravel validators in mind. They exceptions they throw, ValidationException
s, can be passed a failing Laravel Validator to construct an instance.
To implement a validator, simply make a class that implements the Validator
interface. The two methods should do nothing if validation passes and throw a ValidationException
if an error occurs.
Strings
Commonly projects have string related needs that aren't easily filled by the PHP standard library.
Strings\generateRandomString($length = 40)
This generates a random (pseudorandom, do not use this for cryptographic or security purposes) string of the given length. The convenient usage for this is generate noninteger keys for a database table.
Strings\starts_with($haystack, $needle)
Strings\ends_with($haystack, $needle)
Laravel Integration
Errors Partial
This project ships with an Error Partial that displays errors, warnings and success messages at the top of the application page, based on a consistent format. To Install the Error Partial Handler in a Laravel application, add
Depotwarehouse\Toolbox\FrameworkIntegration\Laravel\ErrorPartialServiceProvider::class
to your config/app.php
in the providers array. Next, you'll need to publish the view file with:
php artisan vendor:publish
Now in your layout page, where you want the errors to appear (likely at the top), simply add
@include('vendor.toolbox.errors.errorPartial')
Now errors, warnings and success messages will consistently render!
Testing
phpunit