Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipient api #196

Open
wants to merge 16 commits into
base: develop-split
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Kernel/Abstractions/AbstractDatabaseDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ abstract class AbstractDatabaseDecorator
const TABLE_RECURRENCE_CHARGE = 12;
const TABLE_RECURRENCE_SUBSCRIPTION = 13;
const TABLE_RECURRENCE_SUBSCRIPTION_ITEM = 14;
const TABLE_SPLIT_RECIPIENT = 15;
const TABLE_SPLIT_RECIPIENT_BANK_ACCOUNT = 16;
const TABLE_SPLIT_TRANSFER_SETTING = 17;

protected $db;
protected $tablePrefix;
Expand Down
132 changes: 132 additions & 0 deletions src/Kernel/Helper/Hydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace Mundipagg\Core\Kernel\Helper;

use ReflectionClass;
use ReflectionException;

class Hydrator
{
/**
* @param $array
* @param $obj
* @return mixed
* @throws ReflectionException
*/
public static function hidrator($array, $obj)
{
if (!is_array($array)) {
return $obj;
}

$reflection = new ReflectionClass($obj);

foreach ($array as $key => $value) {
$propertyFind = self::findProperty($reflection, $key);

if ($propertyFind === null) {
continue;
}

$reflectionProperty = $reflection->getProperty($propertyFind);

if ($reflectionProperty->isPrivate() || $reflectionProperty->isProtected()) {
$reflectionProperty->setAccessible(true);
}

$reflectionProperty->setValue($obj, $value);

if ($reflectionProperty->isPrivate() || $reflectionProperty->isProtected()) {
$reflectionProperty->setAccessible(false);
}
}
return $obj;
}

public static function findProperty(ReflectionClass $reflection, $propertyName)
{
if ($reflection->hasProperty($propertyName)) {
return $propertyName;
}

if ($reflection->hasProperty(self::dashesToCamelCase($propertyName))) {
return self::dashesToCamelCase($propertyName);
}

if ($reflection->hasProperty(self::underToCamelCase($propertyName))) {
return self::underToCamelCase($propertyName);
}

return null;
}

private static function underToCamelCase($string, $capitalizeFirstCharacter = false)
{
$str = str_replace('_', '', ucwords($string, '_'));

if (!$capitalizeFirstCharacter) {
$str = lcfirst($str);
}

return $str;
}

private static function dashesToCamelCase($string, $capitalizeFirstCharacter = false)
{
$str = str_replace('-', '', ucwords($string, '-'));

if (!$capitalizeFirstCharacter) {
$str = lcfirst($str);
}

return $str;
}

/**
* @param object $obj
* @return array
* @throws ReflectionException
*/
public static function extract($obj)
{
$reflection = new ReflectionClass($obj);

$array = array();

foreach ($reflection->getProperties() as $prop) {
$accessible = !$prop->isPrivate();
$prop->setAccessible(true);
$array[$prop->getName()] = $prop->getValue($obj);
$prop->setAccessible($accessible);
}

return $array;
}

/**
* @param object $obj
* @return array
* @throws ReflectionException
*/
public static function extractRecursive($obj)
{
$reflection = new ReflectionClass($obj);

$array = [];
foreach ($reflection->getProperties() as $prop) {
$accessible = !$prop->isPrivate();
$prop->setAccessible(true);

$value = $prop->getValue($obj);

if (is_object($value)) {
$value = self::extractRecursive($value);
}

$array[$prop->getName()] = $value;
$prop->setAccessible($accessible);
}

return $array;
}
}
71 changes: 71 additions & 0 deletions src/Kernel/ValueObjects/DocumentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Mundipagg\Core\Kernel\ValueObjects;

use Mundipagg\Core\Kernel\Abstractions\AbstractValueObject;

class DocumentType extends AbstractValueObject
{
const INDIVIDUAL = 'individual';
const COMPANY = 'company';

/** @var string */
private $type;

/**
* Type constructor.
* @param string $type
*/
public function __construct($type)
{
$this->type = $type;
}

/**
* @return DocumentType
*/
public static function individual()
{
return new self(self::INDIVIDUAL);
}

/**
* @return DocumentType
*/
public static function company()
{
return new self(self::COMPANY);
}

/**
* @return string
*/
public function getValue()
{
return $this->type;
}

/**
* To check the structural equality of value objects,
* this method should be implemented in this class children.
*
* @param $object
* @return bool
*/
protected function isEqual($object)
{
return $this->type === $object->getType();
}

/**
* Specify data which should be serialized to JSON
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
{
return $this->type;
}
}
Loading