From 4a11e91e3bf32b39eb94a95ac1bfbb35d22c8e6d Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Sun, 22 Nov 2015 17:27:54 -0800 Subject: [PATCH 01/13] Added documentation. --- README.md | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..a89c067 --- /dev/null +++ b/README.md @@ -0,0 +1,89 @@ +## Laravel Value Objects + +Cast your Eloquent model attributes to value objects with ease! + +### Install + +Require this package with composer using the following command: + +```bash +composer require redcrystal/cast +``` + +### Set Up + +This package lets you easily cast your model attributes to Value Objects that implement our `RedCrystal\Cast\ValueObject` interface. A simple example is provided below. + +```php +value = $value; + } + + public function toScalar() + { + return $this->value; + } + + public function __toString() { + return $this->toScalar(); + } +} +``` + +Set up your model by using the included `Trait` and adding a tiny bit of configuration. + +```php + name of the value object class + 'email' => Email::class + ]; + + // ... +} +``` + +### Usage + +When accessing attributes of your model normally, any attribute you've set up for casting will be returned as an instance of the Value Object. + +```php +$user = User::find($id); + +$user->email; // returns instance of App\ValueObjects\Email +$user->email->toScalar(); // "someone@example.com" +(string) $user->email; // "someone@example.com" +``` + +You can set an attribute set up for casting with either a scalar (native) value, or an instance of the Value Object. + +```php +$user = new User(); + +$user->email = "someone@example.com"; +$user->email = new Email("someone@example.com"); +``` + +### License +This package is open-source software licensed under the [MIT license](http://opensource.org/licenses/MIT). From 5ff7b2b7327c61106165fa339ff25d5c5ab2eeaa Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Sun, 22 Nov 2015 17:29:17 -0800 Subject: [PATCH 02/13] Added tests to confirm json and array serialization of model works as intended. --- tests/CastsValueObjectTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/CastsValueObjectTest.php b/tests/CastsValueObjectTest.php index eba2ff6..8ef4272 100644 --- a/tests/CastsValueObjectTest.php +++ b/tests/CastsValueObjectTest.php @@ -98,4 +98,29 @@ public function testValueObjectCacheIsInvalidatedWhenSettingScalar() $this->assertInstanceOf(EmailValueObject::class, $user->email); $this->assertEquals($email, $user->email->toScalar()); } + + public function testModelToArrayWithValueObjects() + { + $user = new UserModel(); + + $user->email = $email = 'user@example.com'; + + $array = $user->toArray(); + + $this->assertArrayHasKey('email', $array); + $this->assertTrue(is_string($array['email'])); + $this->assertFalse($array['email'] instanceof EmailValueObject); + } + + public function testModelToJsonWithValueObjects() + { + $user = new UserModel(); + + $user->email = $email = 'user@example.com'; + + $json = $user->toJson(); + + $this->assertJson($json); + $this->assertJsonStringEqualsJsonString(json_encode(['email' => $email]), $json); + } } From 984d2287dd13b88d6c3bbed1ce158e6f09834e54 Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Sun, 22 Nov 2015 17:37:41 -0800 Subject: [PATCH 03/13] Added packagist info to README. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a89c067..2d3298c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ ## Laravel Value Objects +[![Latest Stable Version](https://poser.pugx.org/redcrystal/cast/version.png)](https://packagist.org/packages/redcrystal/cast) [![Total Downloads](https://poser.pugx.org/redcrystal/cast/d/total.png)](https://packagist.org/packages/redcrystal/cast) + Cast your Eloquent model attributes to value objects with ease! From 4245336d66501e5b2ad5e67dadd2d1f14e128899 Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Sun, 22 Nov 2015 17:42:18 -0800 Subject: [PATCH 04/13] Added Travis-CI config. --- .travis.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6aa6f78 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: php + +php: + - 5.4 + - 5.5 + - 5.6 + - nightly + - hhvm + +before_script: + - composer self-update + - composer install --prefer-source --no-interaction --dev From 3602f617c7c2e496e146564d734898b87951f335 Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Sun, 22 Nov 2015 17:46:22 -0800 Subject: [PATCH 05/13] Fix travis build config and add travis build status to readme. --- .travis.yml | 2 -- README.md | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6aa6f78..e5aa5ef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ language: php php: - - 5.4 - - 5.5 - 5.6 - nightly - hhvm diff --git a/README.md b/README.md index 2d3298c..b26afcf 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ ## Laravel Value Objects -[![Latest Stable Version](https://poser.pugx.org/redcrystal/cast/version.png)](https://packagist.org/packages/redcrystal/cast) [![Total Downloads](https://poser.pugx.org/redcrystal/cast/d/total.png)](https://packagist.org/packages/redcrystal/cast) +[![Build Status](https://travis-ci.org/redcrystalcode/laravel-value-objects.svg?branch=master)](https://travis-ci.org/redcrystal/cast) +[![Latest Stable Version](https://poser.pugx.org/redcrystal/cast/version.png)](https://packagist.org/packages/redcrystal/cast) +[![Total Downloads](https://poser.pugx.org/redcrystal/cast/d/total.png)](https://packagist.org/packages/redcrystal/cast) Cast your Eloquent model attributes to value objects with ease! From 828766e8b933b60e7bc0845f765ac2c73db60d4d Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Mon, 23 Nov 2015 10:05:39 -0800 Subject: [PATCH 06/13] Don't cast empty values (null or '') to value objects. Instead, return null. --- src/CastsValueObjects.php | 5 +++++ tests/CastsValueObjectTest.php | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/CastsValueObjects.php b/src/CastsValueObjects.php index 44547cc..31c7339 100644 --- a/src/CastsValueObjects.php +++ b/src/CastsValueObjects.php @@ -23,6 +23,11 @@ public function getAttribute($key) // Allow other mutators and such to do their work first. $value = parent::getAttribute($key); + // Don't cast empty $value. + if ($value === null || $value === '') { + return null; + } + // Cache the instantiated value for future access. // This allows tests such as ($model->casted === $model->casted) to be true. if (!$this->isValueObjectCached($key)) { diff --git a/tests/CastsValueObjectTest.php b/tests/CastsValueObjectTest.php index 8ef4272..2c519f9 100644 --- a/tests/CastsValueObjectTest.php +++ b/tests/CastsValueObjectTest.php @@ -123,4 +123,20 @@ public function testModelToJsonWithValueObjects() $this->assertJson($json); $this->assertJsonStringEqualsJsonString(json_encode(['email' => $email]), $json); } + + public function testNullValuesDontGetCast() + { + $user = new UserModel(); + + $this->assertNull($user->email); + } + + public function testEmptyStringValuesDontGetCast() + { + $user = new UserModel(); + + $user->setInternalAttributes(['email' => '']); + + $this->assertNull($user->email); + } } From 0f2e063944c24b38d80b44ca5b70a83d922ebdc1 Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Mon, 23 Nov 2015 10:09:08 -0800 Subject: [PATCH 07/13] Updated package description for better visibility. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 743604b..0657090 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "redcrystal/cast", - "description": "Cast your model attributes to value objects!", + "description": "Laravel Value Objects: Cast your Eloquent model attributes to value objects with ease!", "license": "MIT", "authors": [ { From 4a187f6a0b8ae017d312c328c02838aefe55511a Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Wed, 25 Nov 2015 17:37:21 -0800 Subject: [PATCH 08/13] Update PHP version requirement in Composer to reflect actual minimum version. Update README to add version info. --- README.md | 4 ++++ composer.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b26afcf..397b48d 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ Cast your Eloquent model attributes to value objects with ease! +### Requirements + +This package requires PHP >= 5.6. Using the latest version of PHP is highly recommended. Laravel 4.x and 5.x are supported. + ### Install Require this package with composer using the following command: diff --git a/composer.json b/composer.json index 0657090..a141da1 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ] }, "require": { - "php": ">=5.4", + "php": ">=5.6", "laravel/framework": ">=4.0" }, "require-dev": { From a76c5b444f08787732719bc28758aea81ea4a7f3 Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Sat, 5 Dec 2015 15:24:14 -0800 Subject: [PATCH 09/13] Remove dependency on PHP 5.6 and allow use of this package on PHP >=5.4 --- README.md | 2 +- composer.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 397b48d..2e88562 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Cast your Eloquent model attributes to value objects with ease! ### Requirements -This package requires PHP >= 5.6. Using the latest version of PHP is highly recommended. Laravel 4.x and 5.x are supported. +This package requires PHP >= 5.4. Using the latest version of PHP is highly recommended. Laravel 4.x and 5.x are supported. ### Install diff --git a/composer.json b/composer.json index a141da1..44afdc1 100644 --- a/composer.json +++ b/composer.json @@ -17,10 +17,10 @@ ] }, "require": { - "php": ">=5.6", + "php": ">=5.4", "laravel/framework": ">=4.0" }, "require-dev": { - "phpunit/phpunit": "^5.0" + "phpunit/phpunit": "^4.0" } } From 48cef754d07d9a625b6c0e3ee0b7d02bbeb95b62 Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Sat, 5 Dec 2015 15:24:48 -0800 Subject: [PATCH 10/13] Update docblocks to provide type information for method arguments. --- src/CastsValueObjects.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/CastsValueObjects.php b/src/CastsValueObjects.php index 31c7339..56a2252 100644 --- a/src/CastsValueObjects.php +++ b/src/CastsValueObjects.php @@ -12,7 +12,7 @@ trait CastsValueObjects protected $cachedObjects = []; /** - * @param $key + * @param string $key * * @return mixed */ @@ -44,8 +44,8 @@ public function getAttribute($key) } /** - * @param $key - * @param $value + * @param string $key + * @param mixed $value */ public function setAttribute($key, $value) { @@ -78,7 +78,7 @@ public function setAttribute($key, $value) } /** - * @param $key + * @param string $key * * @return mixed */ @@ -90,7 +90,7 @@ private function createValueObject($key, $value) } /** - * @param $key + * @param string $key * @param ValueObject $object */ private function cacheValueObject($key, ValueObject $object) @@ -99,7 +99,7 @@ private function cacheValueObject($key, ValueObject $object) } /** - * @param $key + * @param string $key */ private function invalidateValueObjectCache($key) { @@ -107,7 +107,7 @@ private function invalidateValueObjectCache($key) } /** - * @param $key + * @param string $key * * @return bool */ @@ -117,7 +117,7 @@ private function isValueObjectCached($key) } /** - * @param $key + * @param string $key * * @return ValueObject */ From 9e11117b1fcc25e5bd22ee2895817f1269b52543 Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Sat, 5 Dec 2015 15:27:08 -0800 Subject: [PATCH 11/13] Update travis config to test on PHP >=5.4 --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index e5aa5ef..6aa6f78 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ language: php php: + - 5.4 + - 5.5 - 5.6 - nightly - hhvm From 3778ec30de388bb59beba59b9ecc39ba6956638c Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Sat, 5 Dec 2015 15:42:47 -0800 Subject: [PATCH 12/13] Revert "Update travis config to test on PHP >=5.4" This reverts commit 9e11117b1fcc25e5bd22ee2895817f1269b52543. --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6aa6f78..e5aa5ef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ language: php php: - - 5.4 - - 5.5 - 5.6 - nightly - hhvm From 87e3ed8edc42bb2088a04bbde76cd9cf29815d52 Mon Sep 17 00:00:00 2001 From: Zain Mehdi Date: Sat, 5 Dec 2015 15:44:34 -0800 Subject: [PATCH 13/13] Re-upgrade to phpunit 5.x. Added note in README to explain dependency on PHP 5.6 for tests. --- README.md | 2 ++ composer.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2e88562..0ccf340 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ Cast your Eloquent model attributes to value objects with ease! This package requires PHP >= 5.4. Using the latest version of PHP is highly recommended. Laravel 4.x and 5.x are supported. +> **Note:** Running tests for this package requires PHP >=5.6. + ### Install Require this package with composer using the following command: diff --git a/composer.json b/composer.json index 44afdc1..0657090 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,6 @@ "laravel/framework": ">=4.0" }, "require-dev": { - "phpunit/phpunit": "^4.0" + "phpunit/phpunit": "^5.0" } }