-
Notifications
You must be signed in to change notification settings - Fork 16
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
1 parent
303183b
commit 8bb76eb
Showing
11 changed files
with
111 additions
and
54 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
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 LireinCore\YMLParser; | ||
|
||
trait TYML | ||
{ | ||
/** | ||
* @param string $name | ||
* @param mixed $value | ||
* @return $this | ||
*/ | ||
public function setField($name, $value) | ||
{ | ||
$setter = 'set' . str_replace(['-', '_'], '', $name); | ||
if (method_exists($this, $setter)) { | ||
$this->$setter($value); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getData() | ||
{ | ||
$array = []; | ||
|
||
foreach ($this as $key => $value) { | ||
if (!is_null($value)) { | ||
if (is_object($value) && method_exists($value, 'getData')) { | ||
$array[$key] = $value->getData(); | ||
} | ||
elseif (is_array($value)) { | ||
if (!empty($value)) { | ||
$array[$key] = $this->getArray($value); | ||
} | ||
} | ||
else { | ||
$array[$key] = $value; | ||
} | ||
} | ||
} | ||
|
||
return $array; | ||
} | ||
|
||
/** | ||
* @param array $value | ||
* @return array | ||
*/ | ||
private function getArray($value) | ||
{ | ||
$subarray = []; | ||
|
||
foreach ($value as $subkey => $subvalue) { | ||
if (is_object($subvalue) && method_exists($subvalue, 'getData')) { | ||
$subarray[$subkey] = $subvalue->getData(); | ||
} | ||
elseif (is_array($subvalue)) { | ||
$subarray[$subkey] = $this->getArray($subvalue); | ||
} | ||
else { | ||
$subarray[$subkey] = $subvalue; | ||
} | ||
} | ||
|
||
return $subarray; | ||
} | ||
} |
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