Skip to content

Commit

Permalink
feat: add deepGet function
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Sep 6, 2022
1 parent 3ea5468 commit 4590934
Showing 1 changed file with 72 additions and 47 deletions.
119 changes: 72 additions & 47 deletions src/Anchor.php
Original file line number Diff line number Diff line change
@@ -1,75 +1,100 @@
<?php

declare(strict_types=1);
declare (strict_types = 1);

namespace Leaf;

/**
* Leaf Security Module
* ---------------------------------
* Simple to use security based utility methods
*
*
* @author Michael Darko <[email protected]>
* @since v2.2
* @version 1.0
*/
class Anchor
{
protected static $config = [
'SECRET_KEY' => '_token',
'SECRET' => '@nkor_leaf$0Secret!',
'EXCEPT' => [],
'METHODS' => ['POST', 'PUT', 'PATCH', 'DELETE'],
];
protected static $config = [
'SECRET_KEY' => '_token',
'SECRET' => '@nkor_leaf$0Secret!',
'EXCEPT' => [],
'METHODS' => ['POST', 'PUT', 'PATCH', 'DELETE'],
];

protected static $errors = [];
protected static $errors = [];

/**
* Manage config for leaf anchor
*
* @param array|null $config The config to set
*/
public static function config($config = null)
{
if ($config === null) {
return static::$config;
}
/**
* Manage config for leaf anchor
*
* @param array|null $config The config to set
*/
public static function config($config = null)
{
if ($config === null) {
return static::$config;
}

static::$config = array_merge(static::$config, $config);
}

/**
* Escape malicious characters
*
* @param mixed $data The data to sanitize.
*/
public static function sanitize($data)
{
if (is_array($data)) {
foreach ($data as $key => $value) {
$data[is_string($key) ? self::sanitize($key) : $key] = self::sanitize($value);
}
}

static::$config = array_merge(static::$config, $config);
}
if (is_string($data)) {
$data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
}

return $data;
}

/**
* Escape malicious characters
* Get an item or items from an array of data.
*
* @param mixed $data The data to sanitize.
* @param array $dataSource An array of data to search through
* @param string|array $item The items to return
*/
public static function sanitize($data)
{
if (is_array($data)) {
foreach ($data as $key => $value) {
$data[is_string($key) ? self::sanitize($key) : $key] = self::sanitize($value);
}
public static function deepGet($dataSource, $item = null)
{
if (!$item) {
return $dataSource;
}

if (is_string($data)) {
$data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
}
$output = [];

return $data;
}
if (is_array($item)) {
foreach ($item as $dataItem) {
$output[$dataItem] = $dataSource[$dataItem] ?? null;
}
} else {
$output = $dataSource[$item] ?? null;
}

/**
* Generate a token for identifying your application
*
* @param int $strength Number of random characters to attach to token
*/
public static function generateToken(int $strength = 16): string
{
return bin2hex(static::$config['SECRET'] . '.' . random_bytes($strength));;
}
return $output;
}

/**
* Generate a token for identifying your application
*
* @param int $strength Number of random characters to attach to token
*/
public static function generateToken(int $strength = 16): string
{
return bin2hex(static::$config['SECRET'] . '.' . random_bytes($strength));
}

public static function errors(): array
{
return static::$errors;
}
public static function errors(): array
{
return static::$errors;
}
}

0 comments on commit 4590934

Please sign in to comment.