Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Improve elements to be arrayble and define after
Browse files Browse the repository at this point in the history
  • Loading branch information
NastuzziSamy committed Aug 8, 2020
1 parent e5511a8 commit 9ad690c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
4 changes: 1 addition & 3 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<?xml version="1.1"?>
<ruleset name="NastuzziSamy">
<description>NastuzziSamy's standards</description>

Expand Down Expand Up @@ -43,10 +43,8 @@

<!-- Comment rules -->
<rule ref="Generic.Commenting.Todo"/>
<rule ref="Squiz.Commenting.BlockComment" />
<rule ref="Squiz.Commenting.DocCommentAlignment" />
<rule ref="Squiz.Commenting.EmptyCatchComment" />
<rule ref="Squiz.Commenting.InlineComment" />
<rule ref="Squiz.Commenting.PostStatementComment" />
<rule ref="Squiz.Commenting.FileComment">
<type>warning</type>
Expand Down
13 changes: 12 additions & 1 deletion src/Elements/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

namespace Laramore\Elements;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Str;
use Laramore\Exceptions\LockException;
use Laramore\Contracts\Locked;
use Laramore\Traits\IsLocked;

class Element implements Locked
class Element implements Locked, Arrayable
{
use IsLocked;

Expand Down Expand Up @@ -221,4 +222,14 @@ public function __toString()
{
return $this->native;
}

/**
* Return values.
*
* @return array
*/
public function toArray(): array
{
return $this->values;
}
}
18 changes: 9 additions & 9 deletions src/Elements/ElementManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,7 @@ public function create(string $name, $native=null): Element
public function set($element): self
{
if ($element instanceof $this->elementClass) {
$this->elements[$name = $element->getName()] = $element;

foreach ($this->definitions as $keyName => $valueName) {
if (!$element->has($keyName)) {
$element->set($keyName, ($valueName ?? $name));
}
}
$this->elements[$element->getName()] = $element;
} else if (\is_array($element)) {
if (Arr::isAssoc($element)) {
foreach ($element as $key => $value) {
Expand Down Expand Up @@ -228,13 +222,19 @@ public function definitions(): array
}

/**
* Lock every element.
* Lock every element after defining all definitions.
*
* @return void
*/
protected function locking()
{
foreach ($this->all() as $element) {
foreach ($this->all() as $name => $element) {
foreach ($this->definitions as $keyName => $valueName) {
if (!$element->has($keyName)) {
$element->set($keyName, ($valueName ?? $name));
}
}

$element->lock();
}
}
Expand Down

0 comments on commit 9ad690c

Please sign in to comment.