From b1f5336014b197cc1ef473f143762ca94c5c3d6d Mon Sep 17 00:00:00 2001 From: Joel Shepherd Date: Fri, 28 Jul 2017 17:14:15 +1000 Subject: [PATCH 1/7] Updating changelog --- CHANGELOG.md | 6 ++++-- composer.json | 2 +- src/WithIpAddress.php | 5 ++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 953c890..6b0a4fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ # Changelog -## [Unreleased] +## [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 +19,6 @@ - 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.3.0...HEAD +[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/composer.json b/composer.json index 5b8acea..ad94125 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "php": ">=7.0", "ramsey/uuid": "^3.6", "illuminate/support": "^5.0", - "illuminate/http": "^5.4" + "illuminate/http": "^5.0" }, "require-dev": { "illuminate/database": "^5.0", diff --git a/src/WithIpAddress.php b/src/WithIpAddress.php index 89bbccf..06b9966 100644 --- a/src/WithIpAddress.php +++ b/src/WithIpAddress.php @@ -15,7 +15,10 @@ trait WithIpAddress */ public function getIpAddress(): string { - return Request::capture()->ip() ?: gethostbyname(gethostname()); + // Check for an existing request object + $request = function_exists('app') ? app('request') : Request::capture(); + + return $request->ip() ?: gethostbyname(gethostname()); } /** From 3c65f381fdd0d99bb9c218e3a18f4a625a803f58 Mon Sep 17 00:00:00 2001 From: Joel Shepherd Date: Fri, 28 Jul 2017 17:19:58 +1000 Subject: [PATCH 2/7] Adding WithIpAddress to the README --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 80e966b..0cd04b0 100644 --- a/README.md +++ b/README.md @@ -81,5 +81,28 @@ $example2 = Example::create([ $example2->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. From f5ae40016c64daacbb0611de2c5c060bf0e72929 Mon Sep 17 00:00:00 2001 From: Joel Shepherd Date: Tue, 1 Aug 2017 09:24:14 +1000 Subject: [PATCH 3/7] Housekeeping --- tests/WithIpAddressTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/WithIpAddressTest.php b/tests/WithIpAddressTest.php index aa3770c..c85d4b4 100644 --- a/tests/WithIpAddressTest.php +++ b/tests/WithIpAddressTest.php @@ -1,6 +1,5 @@ assertTrue(false !== filter_var($model->getIpAddress(), FILTER_VALIDATE_IP)); + $this->assertTrue(false !== filter_var($model->getIpAddress(), FILTER_VALIDATE_IP)); } } From c1922e9402a75e15c2f18a8b9f72be56b983782a Mon Sep 17 00:00:00 2001 From: Joel Shepherd Date: Tue, 15 Aug 2017 10:27:55 +1000 Subject: [PATCH 4/7] Adding support attempts test --- src/Support.php | 2 +- tests/SupportTest.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/SupportTest.php 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/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); + } + } +} From df6c8e7ce4bafbd6be344996893519fd01285457 Mon Sep 17 00:00:00 2001 From: Joel Shepherd Date: Wed, 13 Sep 2017 16:31:41 +1000 Subject: [PATCH 5/7] Adding base model classes and renaming traits --- CHANGELOG.md | 7 +++++ README.md | 21 +++++++------ composer.json | 6 ++-- phpunit.xml | 5 ++++ src/{WithIpAddress.php => IpAddress.php} | 12 +++++--- src/{WithSlug.php => Slug.php} | 8 +++-- src/SlugModel.php | 30 +++++++++++++++++++ src/{WithUuid.php => Uuid.php} | 10 +++---- src/UuidModel.php | 30 +++++++++++++++++++ ...ithIpAddressTest.php => IpAddressTest.php} | 8 ++--- tests/{WithSlugTest.php => SlugTest.php} | 8 ++--- tests/{WithUuidTest.php => UuidTest.php} | 6 ++-- 12 files changed, 116 insertions(+), 35 deletions(-) rename src/{WithIpAddress.php => IpAddress.php} (70%) rename src/{WithSlug.php => Slug.php} (92%) create mode 100644 src/SlugModel.php rename src/{WithUuid.php => Uuid.php} (81%) create mode 100644 src/UuidModel.php rename tests/{WithIpAddressTest.php => IpAddressTest.php} (69%) rename tests/{WithSlugTest.php => SlugTest.php} (79%) rename tests/{WithUuidTest.php => UuidTest.php} (81%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b0a4fc..ef1c178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [Unreleased] +### 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)) diff --git a/README.md b/README.md index 0cd04b0..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 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 ad94125..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.0" + "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 70% rename from src/WithIpAddress.php rename to src/IpAddress.php index 06b9966..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 @@ -16,7 +16,9 @@ trait WithIpAddress public function getIpAddress(): string { // Check for an existing request object - $request = function_exists('app') ? app('request') : Request::capture(); + $request = function_exists('app') + ? app(Request::class) + : Request::capture(); return $request->ip() ?: gethostbyname(gethostname()); } @@ -26,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 92% rename from src/WithSlug.php rename to src/Slug.php index dab24ed..aa19ccc 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. @@ -39,14 +39,16 @@ public function convertTextToSlug(string $text): string * * @return void */ - public static function bootWithSlug() + public static function bootSlug() { static::creating(function ($model) { $base = $model->getSlugBaseText() ? $this->convertTextToSlug($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/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 69% rename from tests/WithIpAddressTest.php rename to tests/IpAddressTest.php index c85d4b4..1c05ba7 100644 --- a/tests/WithIpAddressTest.php +++ b/tests/IpAddressTest.php @@ -1,14 +1,14 @@ assertTrue( - method_exists(new TestIpAddressModel(), 'bootWithIpAddress') + method_exists(new TestIpAddressModel(), 'bootIpAddress') ); } @@ -22,5 +22,5 @@ public function testGetIpAddress() 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/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; } From bfb343ff07a05a6bbe9584d14950043e5ae14028 Mon Sep 17 00:00:00 2001 From: Joel Shepherd Date: Wed, 13 Sep 2017 16:34:55 +1000 Subject: [PATCH 6/7] Updating changelog --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef1c178..26ff374 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## [Unreleased] +## [0.4.0] - 2017-09-13 ### Added - New helper `SlugModel` and `UuidModel` base models that override the primary identifier. @@ -26,6 +26,7 @@ - New `WithUuid` trait that provides UUIDs. - New `WithSlug` trait that provides slug generation. -[Unreleased]: https://github.com/joelshepherd/create-with/compare/0.3.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 From 918c075e6f5cc463ae8eb6c33930bf94ce1d5644 Mon Sep 17 00:00:00 2001 From: Victor Lim Date: Wed, 28 Feb 2018 23:10:20 +1000 Subject: [PATCH 7/7] Fixed FatalThrowableError exception. Using $this when not in object context. --- src/Slug.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/Slug.php b/src/Slug.php index aa19ccc..062034a 100644 --- a/src/Slug.php +++ b/src/Slug.php @@ -23,17 +23,6 @@ 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. * @@ -43,7 +32,7 @@ public static function bootSlug() { static::creating(function ($model) { $base = $model->getSlugBaseText() - ? $this->convertTextToSlug($model->getSlugBaseText()) + ? Str::slug($model->getSlugBaseText()) : ''; $model->forceFill([