Skip to content

Commit

Permalink
Upgraded to laravel 10.x
Browse files Browse the repository at this point in the history
  • Loading branch information
impelevin committed Jan 10, 2024
1 parent 18a0aad commit 97ad9f6
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 68 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
## Esensi Model Traits Package

> Version 1
> Version 3
An [Esensi](https://github.com/esensi) package, coded by [SiteRocket Labs®](https://www.siterocket.com).

The `Esensi/Model` package is just one package that makes up [Esensi](https://github.com/esensi), a platform built on [Laravel](https://laravel.com). This package uses [PHP traits](https://culttt.com/2014/06/25/php-traits) to extend Laravel's default Eloquent models and traits. Using traits allows for a high-degree of code reusability and extensibility. While this package provides some reasonable base models, developers are free to mix and match traits in any combination needed, being confident that the code complies to a reliable interface and is properly unit tested. For more details on the inner workings of the traits please consult the generously documented source code.

## Note

This code is specifically designed to be compatible with the [Laravel Framework](https://laravel.com) and may not be compatible as a stand-alone dependency or as part of another framework.
This code is specifically designed to be compatible with the [Laravel Framework ^10](https://laravel.com) and may not be compatible as a stand-alone dependency or as part of another framework.

### Extend the Default Model

Expand Down Expand Up @@ -92,15 +92,15 @@ class Post extends SoftModel {
Add the `esensi/model` package as a dependency to the application. Using [Composer](https://getcomposer.org), this can be done from the command line:

```bash
composer require esensi/model 0.6.*
composer require esensi/model
```

Or manually it can be added to the `composer.json` file:

```json
{
"require": {
"esensi/model": "0.6.*"
"esensi/model": "^3.0"
}
}
```
Expand All @@ -126,7 +126,6 @@ This Esensi package has been featured in various places from university classroo

> **Model validation** is the method of establishing rules to ensure when you’re creating, or updating, an object based on a model, that all of its field values are set appropriately. That all required fields are filled, that all date fields are formatted properly, etc.
- [Read _Laravel 8 Model Validation With the Esensi Model Traits Package_](https://selftaughtcoders.com/from-idea-to-launch/lesson-24/laravel-8-model-validation-esensi-model-traits-package/)
- [Signup for _From Ideas to Launch_](https://selftaughtcoders.com/from-idea-to-launch/)

### Auto-Validating On Save
Expand Down Expand Up @@ -537,7 +536,7 @@ This package includes the [`JugglingModelTrait`](https://github.com/esensi/model
- create custom types to cast to with magic model methods like:
- Example: `fooBar` => `juggleFooBar()`

Like all the traits, it is self-contained and can be used individually. Be aware, however, that using this trait does overload the magic `__get()` and `__set()` methods of the model (see [Esensi\Model\Model](https://github.com/esensi/model/blob/master/src/Model.php) source code for how to deal with overloading conflicts). Special credit goes to the brilliant [Dayle Rees](https://github.com/daylerees), author of [Code Bright book](https://leanpub.com/codebright), who inspired this trait with his [pull request to Laravel](https://github.com/laravel/framework/pull/4948) which eventually arrived in [Laravel 8 as Attribute Casting](http://laravel.com/docs/eloquent-mutators#attribute-casting) which supports basic type casting.
Like all the traits, it is self-contained and can be used individually. Be aware, however, that using this trait does overload the magic `__get()` and `__set()` methods of the model (see [Esensi\Model\Model](https://github.com/esensi/model/blob/master/src/Model.php) source code for how to deal with overloading conflicts). Special credit goes to the brilliant [Dayle Rees](https://github.com/daylerees), author of [Code Bright book](https://leanpub.com/codebright), who inspired this trait with his [pull request to Laravel](https://github.com/laravel/framework/pull/4948) which eventually arrived in [Laravel as Attribute Casting](http://laravel.com/docs/eloquent-mutators#attribute-casting) which supports basic type casting.

### Auto-Juggling on Access

Expand Down Expand Up @@ -791,8 +790,8 @@ This package uses [PHPUnit](http://phpunit.de) to automate the code testing proc
```json
{
"require-dev": {
"phpunit/phpunit": "4.1.*",
"mockery/mockery": "0.9.*"
"phpunit/phpunit": "10.0.7",
"mockery/mockery": "1.5.1"
}
}
```
Expand Down
24 changes: 14 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
],
"license": "MIT",
"require": {
"php": "^7.3|^8.0",
"nesbot/carbon": "^2.53.1",
"illuminate/contracts": "^8.0|^9.0",
"illuminate/database": "^8.0|^9.0",
"illuminate/encryption": "^8.0|^9.0",
"illuminate/events": "^8.0|^9.0",
"illuminate/hashing": "^8.0|^9.0",
"illuminate/support": "^8.0|^9.0",
"illuminate/validation": "^8.0|^9.0",
"php": "^8.1",
"nesbot/carbon": "^2.67",
"illuminate/contracts": "^9.0|^10.0|^11.0",
"illuminate/database": "^10.0",
"illuminate/encryption": "^10.0",
"illuminate/events": "^10.0",
"illuminate/hashing": "^10.0",
"illuminate/support": "^10.0",
"illuminate/validation": "^10.0",
"paragonie/random_compat": "^8.0|^9.0",
"symfony/polyfill-php81": "^1.23",
"watson/validating": "^6.1"
"watson/validating": "^8.1"

},
"require-dev": {
"phpunit/phpunit": "10.0.7",
"mockery/mockery": "1.5.1"
},
"autoload": {
"psr-4": {
"Esensi\\Model\\": "src/"
Expand Down
18 changes: 18 additions & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Esensi\Model\Contracts\PurgingModelInterface;
use Esensi\Model\Contracts\RelatingModelInterface;
use Esensi\Model\Contracts\ValidatingModelInterface;
use Esensi\Model\Traits\BackwardCompatibleDatesTrait;
use Esensi\Model\Traits\DatesToCastsTrait;
use Esensi\Model\Traits\EncryptingModelTrait;
use Esensi\Model\Traits\HashingModelTrait;
use Esensi\Model\Traits\JugglingModelTrait;
Expand Down Expand Up @@ -76,6 +78,22 @@ abstract class Model extends Eloquent implements
*/
use RelatingModelTrait;

/*
* This trait is designed for automatic conversion of the $dates property to the $casts property in Laravel Eloquent models.
* When a model is being saved, updated, or retrieved from the database, the trait checks for the presence of the $dates property
* and adds corresponding values to the $casts array if they are not already defined.
*
* @see Esensi\Model\Traits\DatesToCastsTrait
*/
use DatesToCastsTrait;

/*
* Get the attributes that should be converted to dates.
*
* @see Esensi\Model\Traits\BackwardCompatibleDatesTrait
*/
use BackwardCompatibleDatesTrait;

/**
* The attributes that should be mutated to dates.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Traits/BackwardCompatibleDatesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Esensi\Model\Traits;

trait BackwardCompatibleDatesTrait
{

/**
* Get the attributes that should be converted to dates.
*
* @return array
*/
public function getDates()
{
$defaults = parent::getDates();
return array_unique(array_merge($this->dates, $defaults));
}
}
38 changes: 38 additions & 0 deletions src/Traits/DatesToCastsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Esensi\Model\Traits;

trait DatesToCastsTrait
{
/**
* Boot the trait.
*
* @return void
*/
protected static function bootDatesToCastsTrait()
{
$events = ['saving', 'retrieved', 'deleting'];

foreach ($events as $event) {
static::$event(function ($model) {
$model->convertDatesToCasts();
});
}
}

/**
* Convert $dates to $casts automatically.
*
* @return void
*/
protected function convertDatesToCasts()
{
if (property_exists($this, 'dates') && property_exists($this, 'casts')) {
foreach ($this->dates as $dateColumn) {
if (!array_key_exists($dateColumn, $this->casts)) {
$this->casts[$dateColumn] = 'datetime';
}
}
}
}
}
10 changes: 5 additions & 5 deletions tests/EncryptingModelTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Esensi\Model\Model;
use Illuminate\Encryption\Encrypter;
use PHPUnit_Framework_TestCase as PHPUnit;
use PHPUnit\Framework\TestCase as PHPUnit;

/**
* Tests for the Encrypting Model Trait.
Expand All @@ -13,7 +13,7 @@ class EncryptingModelTraitTest extends PHPUnit
/**
* Set Up and Prepare Tests.
*/
public function setUp()
public function setUp(): void
{
// Mock the Model that uses the custom trait
$this->model = Mockery::mock('ModelEncryptingStub');
Expand All @@ -23,7 +23,7 @@ public function setUp()
/**
* Tear Down and Clean Up Tests.
*/
public function tearDown()
public function tearDown(): void
{
Mockery::close();
}
Expand Down Expand Up @@ -267,7 +267,7 @@ public function testGettingEncrypter()
// Check that its an Encrypter
$model = new ModelEncryptingStub();
$encrypter = $model->getEncrypter();
$this->assertInstanceOf('\Illuminate\Encryption\Encrypter', $encrypter, $encrypter);
$this->assertInstanceOf('\Illuminate\Encryption\Encrypter', $encrypter);
}

/**
Expand Down Expand Up @@ -515,7 +515,7 @@ public function __construct()
parent::__construct();

// Assign a default encrypter for mocking purposes
$this->encrypter = EncrypterStub::newInstance();
$this->encryptor = EncrypterStub::newInstance();
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/HashingModelObserverTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Esensi\Model\Observers\HashingModelObserver;
use PHPUnit_Framework_TestCase as PHPUnit;
use PHPUnit\Framework\TestCase as PHPUnit;

/**
* Tests for Hashing Model Observer.
Expand All @@ -12,7 +12,7 @@ class HashingModelObserverTest extends PHPUnit
/**
* Set Up and Prepare Tests.
*/
public function setUp()
public function setUp(): void
{
// Create a new instance of the HashingModelObserver
$this->observer = new HashingModelObserver();
Expand All @@ -24,7 +24,7 @@ public function setUp()
/**
* Tear Down and Clean Up Tests.
*/
public function tearDown()
public function tearDown(): void
{
Mockery::close();
}
Expand Down
9 changes: 5 additions & 4 deletions tests/HashingModelTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Esensi\Model\Model;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Hashing\BcryptHasher;
use PHPUnit_Framework_TestCase as PHPUnit;
use PHPUnit\Framework\TestCase as PHPUnit;

/**
* Tests for the Hashing Model Trait.
Expand All @@ -14,7 +14,7 @@ class HashingModelTraitTest extends PHPUnit
/**
* Set Up and Prepare Tests.
*/
public function setUp()
public function setUp(): void
{
// Mock the Model that uses the custom trait
$this->model = Mockery::mock('ModelHashingStub');
Expand All @@ -24,7 +24,7 @@ public function setUp()
/**
* Tear Down and Clean Up Tests.
*/
public function tearDown()
public function tearDown(): void
{
Mockery::close();
}
Expand Down Expand Up @@ -267,7 +267,8 @@ public function testGettingHasher()
// Check that its an Hasher
$model = new ModelHashingStub();
$hasher = $model->getHasher();
$this->assertInstanceOf('\Illuminate\Hashing\BcryptHasher', $hasher, $hasher);

$this->assertInstanceOf('\Illuminate\Hashing\BcryptHasher', $hasher);
}

/**
Expand Down
Loading

0 comments on commit 97ad9f6

Please sign in to comment.