generated from chevere/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
735 additions
and
27 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
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
Empty file.
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,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Chevere. | ||
* | ||
* (c) Rodolfo Berrios <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chevere\VarSupport\Exceptions; | ||
|
||
use LogicException; | ||
|
||
/** | ||
* Exception thrown when failing to provide a clonable object. | ||
*/ | ||
final class ObjectNotClonableException extends LogicException | ||
{ | ||
} |
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,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Chevere. | ||
* | ||
* (c) Rodolfo Berrios <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chevere\VarSupport\Exceptions; | ||
|
||
use LogicException; | ||
|
||
/** | ||
* Exception thrown when a variable can't be stored. | ||
*/ | ||
final class UnableToStoreException extends LogicException | ||
{ | ||
} |
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,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Chevere. | ||
* | ||
* (c) Rodolfo Berrios <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chevere\VarSupport\Interfaces; | ||
|
||
/** | ||
* Describes the component in charge of defining an object variable. | ||
*/ | ||
interface ObjectVariableInterface | ||
{ | ||
public function variable(): object; | ||
|
||
public function assertClonable(): void; | ||
} |
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,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Chevere. | ||
* | ||
* (c) Rodolfo Berrios <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chevere\VarSupport\Interfaces; | ||
|
||
/** | ||
* Describes the component in charge of handling storable variable. | ||
*/ | ||
interface StorableVariableInterface | ||
{ | ||
public function __construct(mixed $variable); | ||
|
||
/** | ||
* Provides access to passed `$variable`. | ||
*/ | ||
public function variable(): mixed; | ||
|
||
/** | ||
* Shorthand for `\var_export($variable)`. | ||
*/ | ||
public function toExport(): string; | ||
|
||
/** | ||
* Shorthand for `\serialize($variable)`. | ||
*/ | ||
public function toSerialize(): string; | ||
} |
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,97 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Chevere. | ||
* | ||
* (c) Rodolfo Berrios <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chevere\VarSupport; | ||
|
||
use Chevere\Iterator\Breadcrumb; | ||
use Chevere\Iterator\Interfaces\BreadcrumbInterface; | ||
use Chevere\VarSupport\Exceptions\ObjectNotClonableException; | ||
use Chevere\VarSupport\Interfaces\ObjectVariableInterface; | ||
use Chevere\VarSupport\Traits\BreadcrumbIterableTrait; | ||
use ReflectionNamedType; | ||
use ReflectionObject; | ||
use function Chevere\Message\message; | ||
|
||
final class ObjectVariable implements ObjectVariableInterface | ||
{ | ||
use BreadcrumbIterableTrait; | ||
|
||
private BreadcrumbInterface $breadcrumb; | ||
|
||
public function __construct( | ||
private object $variable | ||
) { | ||
$this->breadcrumb = new Breadcrumb(); | ||
} | ||
|
||
public function variable(): object | ||
{ | ||
return $this->variable; | ||
} | ||
|
||
/** | ||
* @throws ObjectNotClonableException | ||
*/ | ||
public function assertClonable(): void | ||
{ | ||
$this->assert($this->variable); | ||
} | ||
|
||
private function assert(mixed $variable): void | ||
{ | ||
if (is_object($variable)) { | ||
$this->breadcrumbObject($variable); | ||
} elseif (is_iterable($variable)) { | ||
$this->breadcrumbIterable($variable); | ||
} | ||
} | ||
|
||
private function breadcrumbObject(object $variable): void | ||
{ | ||
$this->breadcrumb = $this->breadcrumb | ||
->withAdded('object: ' . $variable::class); | ||
$objectKey = $this->breadcrumb->pos(); | ||
$reflection = new ReflectionObject($variable); | ||
if (! $reflection->isCloneable()) { | ||
throw new ObjectNotClonableException( | ||
(string) message( | ||
'Object is not clonable at `%at%`', | ||
at: $this->breadcrumb->__toString() | ||
) | ||
); | ||
} | ||
$properties = $reflection->getProperties(); | ||
foreach ($properties as $property) { | ||
/** @var ?ReflectionNamedType $namedType */ | ||
$namedType = $property->getType(); | ||
$propertyType = $namedType !== null | ||
? $namedType->getName() . ' ' | ||
: ''; | ||
$this->breadcrumb = $this->breadcrumb | ||
->withAdded( | ||
'property: ' | ||
. $propertyType | ||
. '$' . $property->getName() | ||
); | ||
$propertyKey = $this->breadcrumb->pos(); | ||
// @infection-ignore-all | ||
if ($property->isInitialized($variable)) { | ||
$this->assert($property->getValue($variable)); | ||
} | ||
$this->breadcrumb = $this->breadcrumb | ||
->withRemoved($propertyKey); | ||
} | ||
$this->breadcrumb = $this->breadcrumb | ||
->withRemoved($objectKey); | ||
} | ||
} |
Oops, something went wrong.