-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: bnomei <[email protected]>
- Loading branch information
Showing
14 changed files
with
578 additions
and
175 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Bnomei\Blueprints; | ||
|
||
trait HasFluentSetter | ||
{ | ||
public function __call($name, $arguments): self | ||
{ | ||
// fluent setter | ||
if (count($arguments) === 1 && property_exists($this, $name)) { | ||
$this->{$name} = $arguments[0]; | ||
|
||
return $this; | ||
} | ||
|
||
return parent::__call($name, $arguments); | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
namespace Bnomei\Blueprints; | ||
|
||
use ReflectionClass; | ||
use ReflectionProperty; | ||
|
||
trait HasProperties | ||
{ | ||
public function toArray(): array | ||
{ | ||
$data = []; | ||
|
||
// use reflection to get all public properties of class with their current value in an array | ||
$rc = new ReflectionClass($this); | ||
foreach ($rc->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { | ||
$data[$property->getName()] = $property->getValue($this); | ||
} | ||
|
||
// move all properties to root | ||
if (isset($data['properties'])) { | ||
$data = array_merge($data, $data['properties']); | ||
unset($data['properties']); | ||
} | ||
|
||
ksort($data); | ||
|
||
// empty() would catch 0 and false which is not what we want | ||
$data = array_filter($data, fn ($value) => $value !== null && $value !== '' && $value !== []); | ||
|
||
return $data; | ||
} | ||
|
||
public function property(string $key, mixed $value): self | ||
{ | ||
$this->properties[$key] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
return $this->toArray(); | ||
} | ||
} |
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
Oops, something went wrong.