diff --git a/CHANGELOG.md b/CHANGELOG.md index 953c890..26ff374 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,15 @@ # Changelog -## [Unreleased] +## [0.4.0] - 2017-09-13 ### Added +- New helper `SlugModel` and `UuidModel` base models that override the primary identifier. + +### Changed +- Traits no longer have the `With` prefix. + +## [0.3.0] - 2017-07-28 +### Added +- New `WithIpAddress` trait that adds requester's IP address. ([#1](https://github.com/joelshepherd/create-with/pull/1)) - New method that can override the text to slug conversion function. ## [0.2.0] - 2017-07-06 @@ -18,5 +26,7 @@ - New `WithUuid` trait that provides UUIDs. - New `WithSlug` trait that provides slug generation. -[Unreleased]: https://github.com/joelshepherd/create-with/compare/0.2.0...HEAD +[Unreleased]: https://github.com/joelshepherd/create-with/compare/0.4.0...HEAD +[0.4.0]: https://github.com/joelshepherd/create-with/compare/0.3.0...0.4.0 +[0.3.0]: https://github.com/joelshepherd/create-with/compare/0.2.0...0.3.0 [0.2.0]: https://github.com/joelshepherd/create-with/compare/0.1.0...0.2.0 diff --git a/README.md b/README.md index 80e966b..3f332c3 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,16 @@ -# Create with X - Laravel Models +# Create With - Laravel Models A simple package that provides traits to add common indentity fields to Laravel models when they are created. This package is designed to work out of the box with just the traits. No other configuration is needed. -## Install -Install the package via composer. Minimum PHP version is 7.0. +## Installation +**Dependencies** +- PHP 7 +- Laravel 5.* +**Composer** ``` composer require joelshepherd/create-with ``` @@ -24,11 +27,11 @@ Adds an unique UUID to the model. ```php slug; // this-is-a-title-7iw90lj ``` +### Create with IP address +Adds the requester's IP address to the model. + +**Default options** +- `getIpAddressField()` returns `ip_address` + +```php +ip_address; // 127.0.0.1 +``` + ## Contributing -Bug and feature pull requests are welcome. If you have any feature requests, feel free to create an issue with your proposal. +Submitting issues and pull requests for bugs, features and feature requests are welcome. diff --git a/composer.json b/composer.json index 5b8acea..40f2f69 100644 --- a/composer.json +++ b/composer.json @@ -23,11 +23,11 @@ "require": { "php": ">=7.0", "ramsey/uuid": "^3.6", - "illuminate/support": "^5.0", - "illuminate/http": "^5.4" + "illuminate/database": "^5.0", + "illuminate/http": "^5.0", + "illuminate/support": "^5.0" }, "require-dev": { - "illuminate/database": "^5.0", "phpunit/phpunit": "^6.2" } } diff --git a/phpunit.xml b/phpunit.xml index 9e5ae99..8cda42a 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,6 +9,11 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false"> + + + ./src + + ./tests diff --git a/src/WithIpAddress.php b/src/IpAddress.php similarity index 59% rename from src/WithIpAddress.php rename to src/IpAddress.php index 89bbccf..ac25b75 100644 --- a/src/WithIpAddress.php +++ b/src/IpAddress.php @@ -6,7 +6,7 @@ /** * Adds IP Address (v4) to the model. */ -trait WithIpAddress +trait IpAddress { /** * Get the IP Address @@ -15,7 +15,12 @@ trait WithIpAddress */ public function getIpAddress(): string { - return Request::capture()->ip() ?: gethostbyname(gethostname()); + // Check for an existing request object + $request = function_exists('app') + ? app(Request::class) + : Request::capture(); + + return $request->ip() ?: gethostbyname(gethostname()); } /** @@ -23,10 +28,12 @@ public function getIpAddress(): string * * @return void */ - public static function bootWithIpAddress() + public static function bootIpAddress() { static::creating(function ($model) { - $model->forceFill([$model->getIpAddressField() => $model->getIpAddress()]); + $model->forceFill([ + $model->getIpAddressField() => $model->getIpAddress() + ]); }); } diff --git a/src/WithSlug.php b/src/Slug.php similarity index 79% rename from src/WithSlug.php rename to src/Slug.php index dab24ed..062034a 100644 --- a/src/WithSlug.php +++ b/src/Slug.php @@ -6,7 +6,7 @@ /** * Adds an unique slug to the model. */ -trait WithSlug +trait Slug { /** * Generate a candidate slug that will be tested for uniqueness. @@ -23,30 +23,21 @@ public function generateCandidateSlug(string $base): string return trim("$base-$random", '-'); } - /** - * Method used to convert text into a slug. - * - * @param string $text - * @return string - */ - public function convertTextToSlug(string $text): string - { - return Str::slug($text); - } - /** * Bind generation logic to the creating event. * * @return void */ - public static function bootWithSlug() + public static function bootSlug() { static::creating(function ($model) { $base = $model->getSlugBaseText() - ? $this->convertTextToSlug($model->getSlugBaseText()) + ? Str::slug($model->getSlugBaseText()) : ''; - $model->forceFill([$model->getSlugField() => $base]); + $model->forceFill([ + $model->getSlugField() => $base + ]); $attributes = Support::generate( $model, $model->getSlugField(), diff --git a/src/SlugModel.php b/src/SlugModel.php new file mode 100644 index 0000000..fdefdc8 --- /dev/null +++ b/src/SlugModel.php @@ -0,0 +1,30 @@ +getKeyName(); + } +} diff --git a/src/Support.php b/src/Support.php index 7762ed9..b30d491 100644 --- a/src/Support.php +++ b/src/Support.php @@ -21,7 +21,7 @@ class Support */ public static function attempt(int &$attempts) { - if ($attempts > static::$maxAttempts) { + if ($attempts >= static::$maxAttempts) { throw new EntropyException('Unable to find a unique value.'); } diff --git a/src/WithUuid.php b/src/Uuid.php similarity index 81% rename from src/WithUuid.php rename to src/Uuid.php index c224b88..2ee7bab 100644 --- a/src/WithUuid.php +++ b/src/Uuid.php @@ -1,12 +1,12 @@ getKeyName(); + } +} diff --git a/tests/WithIpAddressTest.php b/tests/IpAddressTest.php similarity index 51% rename from tests/WithIpAddressTest.php rename to tests/IpAddressTest.php index aa3770c..1c05ba7 100644 --- a/tests/WithIpAddressTest.php +++ b/tests/IpAddressTest.php @@ -1,15 +1,14 @@ assertTrue( - method_exists(new TestIpAddressModel(), 'bootWithIpAddress') + method_exists(new TestIpAddressModel(), 'bootIpAddress') ); } @@ -17,11 +16,11 @@ public function testGetIpAddress() { $model = new TestIpAddressModel(); - $this->assertTrue(false !== filter_var($model->getIpAddress(), FILTER_VALIDATE_IP)); + $this->assertTrue(false !== filter_var($model->getIpAddress(), FILTER_VALIDATE_IP)); } } class TestIpAddressModel extends Model { - use WithIpAddress; + use IpAddress; } diff --git a/tests/WithSlugTest.php b/tests/SlugTest.php similarity index 79% rename from tests/WithSlugTest.php rename to tests/SlugTest.php index 176a242..d217fb5 100644 --- a/tests/WithSlugTest.php +++ b/tests/SlugTest.php @@ -1,14 +1,14 @@ assertTrue( - method_exists(new TestSlugModel(), 'bootWithSlug') + method_exists(new TestSlugModel(), 'bootSlug') ); } @@ -30,5 +30,5 @@ public function testSlugGenerateMethod() class TestSlugModel extends Model { - use WithSlug; + use Slug; } diff --git a/tests/SupportTest.php b/tests/SupportTest.php new file mode 100644 index 0000000..02652f8 --- /dev/null +++ b/tests/SupportTest.php @@ -0,0 +1,17 @@ +expectException(EntropyException::class); + + $attempts = 0; + while ($attempts < Support::$maxAttempts + 1) { + Support::attempt($attempts); + } + } +} diff --git a/tests/WithUuidTest.php b/tests/UuidTest.php similarity index 81% rename from tests/WithUuidTest.php rename to tests/UuidTest.php index 71cedee..d020312 100644 --- a/tests/WithUuidTest.php +++ b/tests/UuidTest.php @@ -1,6 +1,6 @@ assertTrue( - method_exists(new TestUuidModel(), 'bootWithUuid') + method_exists(new TestUuidModel(), 'bootUuid') ); } @@ -25,5 +25,5 @@ public function testUuid4GenerateMethod() class TestUuidModel extends Model { - use WithUuid; + use Uuid; }