-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added resource value support Moved SingleValueObjectTrait validation / normalization logic to factory method (to avoid unnecessary instantiation) Improved documentation Fixed money example
- Loading branch information
1 parent
e36a73c
commit 118c927
Showing
12 changed files
with
366 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ Read the full documentation [here](docs/index.md). | |
Usage | ||
----- | ||
|
||
Install the package using composer. | ||
|
||
``` | ||
composer require lhellemons/php-value-objects | ||
``` | ||
|
@@ -36,9 +38,11 @@ final class Weekday | |
return self::define('TUESDAY'); | ||
} | ||
|
||
... | ||
// ... | ||
} | ||
... | ||
|
||
// ... | ||
|
||
$monday = Weekday::MONDAY(); | ||
$tuesday = Weekday::TUESDAY(); | ||
$deliveryDay = WeekDay::MONDAY(); | ||
|
@@ -74,7 +78,9 @@ final class EmailAddress | |
return $this->emailAddressString; | ||
} | ||
} | ||
... | ||
|
||
// ... | ||
|
||
$emailAddress = EmailAddress::of("[email protected]"); | ||
$sameEmailAddress = EmailAddress::of(" [email protected]"); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace SolidPhp\ValueObjects\Value\Ref; | ||
|
||
define('__SOLIDPHP_VALUEOBJECTS_WEAKREF_AVAILABLE', class_exists('\WeakRef')); | ||
|
||
if (__SOLIDPHP_VALUEOBJECTS_WEAKREF_AVAILABLE) { | ||
function createRef(): Ref { | ||
return new WeakRef(); | ||
} | ||
} else { | ||
function createRef(): Ref { | ||
return new StrongRef(); | ||
} | ||
} | ||
|
||
/** | ||
* Class Ref | ||
* | ||
* Provides a way of storing a reference to an object. There are two implementations: | ||
* StrongRef, which stores a reference to the object directly; and | ||
* WeakRef, which stores a PHP WeakRef to the object. | ||
* | ||
* WeakRef can only be used if the PECL extension has been installed. | ||
* | ||
* @internal | ||
*/ | ||
abstract class Ref | ||
{ | ||
public static function create(): Ref | ||
{ | ||
return createRef(); | ||
} | ||
|
||
/** | ||
* @return object|null | ||
*/ | ||
abstract public function get(); | ||
|
||
/** | ||
* @param object|null $object | ||
*/ | ||
abstract public function set($object): void; | ||
|
||
abstract public function has(): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace SolidPhp\ValueObjects\Value\Ref; | ||
|
||
/** | ||
* Class StrongRef | ||
* @internal | ||
*/ | ||
class StrongRef extends Ref | ||
{ | ||
/** @var object|null */ | ||
private $object; | ||
|
||
/** | ||
* @return object|null | ||
*/ | ||
public function get() | ||
{ | ||
return $this->object; | ||
} | ||
|
||
/** | ||
* @param object|null $object | ||
*/ | ||
public function set($object): void | ||
{ | ||
$this->object = $object; | ||
} | ||
|
||
public function has(): bool | ||
{ | ||
return $this->object !== null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace SolidPhp\ValueObjects\Value\Ref; | ||
|
||
use \WeakRef as PhpWeakRef; | ||
|
||
/** | ||
* Class WeakRef | ||
* @internal | ||
*/ | ||
class WeakRef extends Ref | ||
{ | ||
/** @var PhpWeakRef|null */ | ||
private $weakRef; | ||
|
||
/** | ||
* @return object|null | ||
*/ | ||
public function get() | ||
{ | ||
return $this->weakRef ? $this->weakRef->get() : null; | ||
} | ||
|
||
/** | ||
* @param object|null $object | ||
*/ | ||
public function set($object): void | ||
{ | ||
$this->weakRef = new PhpWeakRef($object); | ||
} | ||
|
||
public function has(): bool | ||
{ | ||
return $this->weakRef && $this->weakRef->valid(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.