-
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.
Merge pull request #7 from Hi-Folks/feat/6-yaml
Handling YAML
- Loading branch information
Showing
10 changed files
with
202 additions
and
110 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
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,69 @@ | ||
<?php | ||
|
||
namespace HiFolks\DataType\Traits; | ||
|
||
trait IteratableBlock | ||
{ | ||
public function current(): mixed | ||
{ | ||
if ($this->iteratorReturnsBlock) { | ||
$current = current($this->data); | ||
if (is_array($current)) { | ||
return self::make($current); | ||
} | ||
return $current; | ||
} | ||
return current($this->data); | ||
} | ||
|
||
public function next(): void | ||
{ | ||
next($this->data); | ||
} | ||
|
||
/** | ||
* Return the key of the current element | ||
* | ||
* @link https://php.net/manual/en/iterator.key.php | ||
* | ||
* @return string|int|null scalar on success, or null on failure. | ||
*/ | ||
public function key(): string|int|null | ||
{ | ||
return key($this->data); | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
return !is_null($this->key()); | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
reset($this->data); | ||
} | ||
|
||
public function offsetExists(mixed $offset): bool | ||
{ | ||
return array_key_exists($offset, $this->data); | ||
} | ||
|
||
public function offsetGet(mixed $offset): mixed | ||
{ | ||
return $this->get($offset); | ||
} | ||
|
||
public function offsetSet(mixed $offset, mixed $value): void | ||
{ | ||
if (is_null($offset)) { | ||
$this->data[] = $value; | ||
} else { | ||
$this->data[$offset] = $value; | ||
} | ||
} | ||
|
||
public function offsetUnset(mixed $offset): void | ||
{ | ||
unset($this->data[$offset]); | ||
} | ||
} |
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 HiFolks\DataType\Traits; | ||
|
||
use Symfony\Component\Yaml\Yaml; | ||
|
||
trait LoadableBlock | ||
{ | ||
public static function fromJsonString(string $jsonString = "[]"): self | ||
{ | ||
/** @var array<int|string, mixed> $json */ | ||
$json = json_decode($jsonString, associative: true); | ||
return self::make($json); | ||
} | ||
|
||
public static function fromJsonFile(string $jsonFile): self | ||
{ | ||
if (file_exists($jsonFile)) { | ||
$content = file_get_contents($jsonFile); | ||
if ($content === false) { | ||
return self::make([]); | ||
} | ||
return self::fromJsonString($content); | ||
} | ||
return self::make([]); | ||
} | ||
|
||
public static function fromYamlFile(string $yamlFile): self | ||
{ | ||
if (file_exists($yamlFile)) { | ||
$content = file_get_contents($yamlFile); | ||
if ($content === false) { | ||
return self::make([]); | ||
} | ||
return self::fromYamlString($content); | ||
} | ||
return self::make([]); | ||
} | ||
|
||
public static function fromYamlString(string $yamlString = ""): self | ||
{ | ||
/** @var array<int|string, mixed> $yaml */ | ||
$yaml = Yaml::parse($yamlString); | ||
return self::make($yaml); | ||
} | ||
} |
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,5 @@ | ||
<?php | ||
|
||
arch('globals') | ||
->expect(['dd', 'dump']) | ||
->not->toBeUsed(); |
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.