Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed FatalThrowableError. Using $this when not in object context. #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
Expand All @@ -24,11 +27,11 @@ Adds an unique UUID to the model.

```php
<?php
use JoelShepherd\CreateWith\WithUuid;
use JoelShepherd\CreateWith;

class Example extends Model
{
use WithUuid;
use CreateWith\Uuid;
}
```

Expand All @@ -51,11 +54,11 @@ for uniqueness.

```php
<?php
use JoelShepherd\CreateWith\WithSlug;
use JoelShepherd\CreateWith;

class Example extends Model
{
use WithSlug;
use CreateWith\Slug;

// Optionally set the base string to build the slug from
protected function getSlugBaseText()
Expand All @@ -81,5 +84,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
<?php
use JoelShepherd\CreateWith\WithIpAddress;

class Example extends Model
{
use CreateWith\IpAddress;
}
```

```php
<?php

$example = Example::create();
$example->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.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>./src</directory>
</whitelist>
</filter>
<testsuites>
<testsuite name="Tests">
<directory>./tests</directory>
Expand Down
15 changes: 11 additions & 4 deletions src/WithIpAddress.php → src/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Adds IP Address (v4) to the model.
*/
trait WithIpAddress
trait IpAddress
{
/**
* Get the IP Address
Expand All @@ -15,18 +15,25 @@ 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());
}

/**
* Bind logic to the creating event.
*
* @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()
]);
});
}

Expand Down
21 changes: 6 additions & 15 deletions src/WithSlug.php → src/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(),
Expand Down
30 changes: 30 additions & 0 deletions src/SlugModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace JoelShepherd\CreateWith;

use Illuminate\Database\Eloquent\Model;
use JoelShepherd\CreateWith\Slug;

/**
* Base model that uses a slug as the primary identifier.
*/
class SlugModel extends Model
{
use Slug;

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;

/**
* Get the slug field name.
*
* @return string
*/
protected function getSlugField(): string
{
return $this->getKeyName();
}
}
2 changes: 1 addition & 1 deletion src/Support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down
10 changes: 5 additions & 5 deletions src/WithUuid.php → src/Uuid.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace JoelShepherd\CreateWith;

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Uuid as UuidGenerator;

/**
* Adds an unique UUID to the model.
* Adds an unique UUID (v4) to the model.
*/
trait WithUuid
trait Uuid
{
/**
* Generate a candidate UUID that will be tested for uniqueness.
Expand All @@ -15,15 +15,15 @@ trait WithUuid
*/
public function generateCandidateUuid(): string
{
return Uuid::uuid4();
return UuidGenerator::uuid4();
}

/**
* Bind generation logic to the creating event.
*
* @return void
*/
public static function bootWithUuid()
public static function bootUuid()
{
static::creating(function ($model) {
$attributes = Support::generate(
Expand Down
30 changes: 30 additions & 0 deletions src/UuidModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace JoelShepherd\CreateWith;

use Illuminate\Database\Eloquent\Model;
use JoelShepherd\CreateWith\Uuid;

/**
* Base model that uses a uuid as the primary identifier.
*/
class UuidModel extends Model
{
use Uuid;

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;

/**
* Get the uuid field name.
*
* @return string
*/
protected function getUuidField(): string
{
return $this->getKeyName();
}
}
11 changes: 5 additions & 6 deletions tests/WithIpAddressTest.php → tests/IpAddressTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
<?php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use JoelShepherd\CreateWith\WithIpAddress;
use JoelShepherd\CreateWith\IpAddress;
use PHPUnit\Framework\TestCase;

class WithIpAddressTest extends TestCase
class IpAddressTest extends TestCase
{
public function testTraitAttachesToModel()
{
$this->assertTrue(
method_exists(new TestIpAddressModel(), 'bootWithIpAddress')
method_exists(new TestIpAddressModel(), 'bootIpAddress')
);
}

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;
}
8 changes: 4 additions & 4 deletions tests/WithSlugTest.php → tests/SlugTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
use Illuminate\Database\Eloquent\Model;
use JoelShepherd\CreateWith\WithSlug;
use JoelShepherd\CreateWith\Slug;
use PHPUnit\Framework\TestCase;

class WithSlugTest extends TestCase
class SlugTest extends TestCase
{
public function testTraitAttachesToModel()
{
$this->assertTrue(
method_exists(new TestSlugModel(), 'bootWithSlug')
method_exists(new TestSlugModel(), 'bootSlug')
);
}

Expand All @@ -30,5 +30,5 @@ public function testSlugGenerateMethod()

class TestSlugModel extends Model
{
use WithSlug;
use Slug;
}
17 changes: 17 additions & 0 deletions tests/SupportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
use JoelShepherd\CreateWith\Exceptions\EntropyException;
use JoelShepherd\CreateWith\Support;
use PHPUnit\Framework\TestCase;

class SupportTest extends TestCase
{
public function testAttemptThrowsExceptionAfterMaxAttempts()
{
$this->expectException(EntropyException::class);

$attempts = 0;
while ($attempts < Support::$maxAttempts + 1) {
Support::attempt($attempts);
}
}
}
Loading