Skip to content

Commit

Permalink
feat: cria base para constants
Browse files Browse the repository at this point in the history
  • Loading branch information
valdeir2000 committed Aug 8, 2020
1 parent 4b82034 commit a52f2e1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
55 changes: 55 additions & 0 deletions upload/system/library/PagSeguro/src/Constants/AbstractEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace ValdeirPsr\PagSeguro\Constants;

abstract class AbstractEnum
{
private static $enums = [];

private function __construct()
{
/** Preventing instance */
}

/**
* Captura a lista de constantes
*
* @return array
*/
private static function getEnums(): array
{
if (self::$enums === null) {
self::$enums = [];
}

$calledClass = get_called_class();

if (!array_key_exists($calledClass, self::$enums)) {
$reflect = new \ReflectionClass($calledClass);
self::$enums[$calledClass] = $reflect->getConstants();
}

return self::$enums[$calledClass];
}

/**
* Verifica se determinada chave existe
*
* @return bool
*/
public static function has($name): bool
{
return array_key_exists($name, self::getEnums());
}

/**
* Verifica se determinado valor existe
*
* @return bool
*/
public static function isValidValue($value): bool
{
$values = array_values(self::getEnums());
return in_array($value, $values);
}
}
12 changes: 12 additions & 0 deletions upload/system/library/PagSeguro/src/Constants/Shipping/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace ValdeirPsr\PagSeguro\Constants\Shipping;

use ValdeirPsr\PagSeguro\Constants\AbstractEnum;

class Type extends AbstractEnum
{
const PAC = 1;
const SEDEX = 2;
const UNKNOWN = 3;
}

0 comments on commit a52f2e1

Please sign in to comment.