Initial commit
This commit is contained in:
@@ -0,0 +1,293 @@
|
||||
<?php
|
||||
|
||||
namespace FluentCrm\Includes\Helpers;
|
||||
|
||||
use Closure;
|
||||
use ArrayAccess;
|
||||
|
||||
/**
|
||||
* @deprecated No longer used by internal code and not recommended. Please use FluentCrm\Framework\Support\Arr instead
|
||||
*/
|
||||
class Arr
|
||||
{
|
||||
/**
|
||||
* Taken from Illuminate\Support\Arr (Laravel Framework)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check if an item or items exist in an array using "dot" notation.
|
||||
*
|
||||
* @param \ArrayAccess|array $array
|
||||
* @param string|array $keys
|
||||
* @return bool
|
||||
*/
|
||||
public static function has($array, $keys)
|
||||
{
|
||||
if (is_null($keys)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$keys = (array)$keys;
|
||||
|
||||
if (!$array) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($keys === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$subKeyArray = $array;
|
||||
|
||||
if (static::exists($array, $key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (explode('.', $key) as $segment) {
|
||||
if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
|
||||
$subKeyArray = $subKeyArray[$segment];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from an array using "dot" notation.
|
||||
*
|
||||
* @param \ArrayAccess|array $array
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($array, $key, $default = null)
|
||||
{
|
||||
if (!static::accessible($array)) {
|
||||
return static::value($default);
|
||||
}
|
||||
|
||||
if (is_null($key)) {
|
||||
return $array;
|
||||
}
|
||||
|
||||
if (static::exists($array, $key)) {
|
||||
return $array[$key];
|
||||
}
|
||||
|
||||
foreach (explode('.', $key) as $segment) {
|
||||
if (static::accessible($array) && static::exists($array, $segment)) {
|
||||
$array = $array[$segment];
|
||||
} else {
|
||||
return static::value($default);
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an array item to a given value using "dot" notation.
|
||||
*
|
||||
* If no key is given to the method, the entire array will be replaced.
|
||||
*
|
||||
* @param array $array
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return array
|
||||
*/
|
||||
public static function set(&$array, $key, $value)
|
||||
{
|
||||
if (is_null($key)) {
|
||||
return $array = $value;
|
||||
}
|
||||
|
||||
$keys = explode('.', $key);
|
||||
|
||||
while (count($keys) > 1) {
|
||||
$key = array_shift($keys);
|
||||
|
||||
// If the key doesn't exist at this depth, we will just create an empty array
|
||||
// to hold the next value, allowing us to create the arrays to hold final
|
||||
// values at the correct depth. Then we'll keep digging into the array.
|
||||
if (!isset($array[$key]) || !is_array($array[$key])) {
|
||||
$array[$key] = [];
|
||||
}
|
||||
|
||||
$array = &$array[$key];
|
||||
}
|
||||
|
||||
$array[array_shift($keys)] = $value;
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a subset of the items from the given array.
|
||||
*
|
||||
* @param array $array
|
||||
* @param array|string $keys
|
||||
* @return array
|
||||
*/
|
||||
public static function only($array, $keys)
|
||||
{
|
||||
$keys = (array)$keys;
|
||||
|
||||
$results = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
static::set($results, $key, static::get($array, $key));
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the given array except for a specified array of items.
|
||||
*
|
||||
* @param array $array
|
||||
* @param array|string $keys
|
||||
* @return array
|
||||
*/
|
||||
public static function except($array, $keys)
|
||||
{
|
||||
$keys = (array)$keys;
|
||||
|
||||
static::forget($array, $keys);
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove one or many array items from a given array using "dot" notation.
|
||||
*
|
||||
* @param array $array
|
||||
* @param array|string $keys
|
||||
* @return void
|
||||
*/
|
||||
public static function forget(&$array, $keys)
|
||||
{
|
||||
$original = &$array;
|
||||
|
||||
$keys = (array)$keys;
|
||||
|
||||
if (count($keys) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($keys as $key) {
|
||||
// if the exact key exists in the top-level, remove it
|
||||
if (static::exists($array, $key)) {
|
||||
unset($array[$key]);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$parts = explode('.', $key);
|
||||
|
||||
// clean up before each pass
|
||||
$array = &$original;
|
||||
|
||||
while (count($parts) > 1) {
|
||||
$part = array_shift($parts);
|
||||
|
||||
if (isset($array[$part]) && is_array($array[$part])) {
|
||||
$array = &$array[$part];
|
||||
} else {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
|
||||
unset($array[array_shift($parts)]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first element in an array passing a given truth test.
|
||||
*
|
||||
* @param array $array
|
||||
* @param \Closure $callback
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public static function first($array, $callback, $default = null)
|
||||
{
|
||||
foreach ($array as $key => $value) {
|
||||
if (call_user_func($callback, $key, $value)) return $value;
|
||||
}
|
||||
|
||||
return value($default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given value is array accessible.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public static function accessible($value)
|
||||
{
|
||||
return is_array($value) || $value instanceof ArrayAccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given key exists in the provided array.
|
||||
*
|
||||
* @param \ArrayAccess|array $array
|
||||
* @param string|int $key
|
||||
* @return bool
|
||||
*/
|
||||
public static function exists($array, $key)
|
||||
{
|
||||
if ($array instanceof ArrayAccess) {
|
||||
return $array->offsetExists($key);
|
||||
}
|
||||
|
||||
return array_key_exists($key, $array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the default value of the given value.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
public static function value($value)
|
||||
{
|
||||
return $value instanceof Closure ? $value() : $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten a multi-dimensional associative array with dots.
|
||||
*
|
||||
* @param array $array
|
||||
* @param string $prepend
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function dot($array, $prepend = '')
|
||||
{
|
||||
$results = [];
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_array($value) && !empty($value)) {
|
||||
$results = array_merge($results, static::dot($value, $prepend . $key . '.'));
|
||||
} else {
|
||||
$results[$prepend . $key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public static function isTrue($array, $key)
|
||||
{
|
||||
$value = self::get($array, $key);
|
||||
$isFalse = !$value || $value =='false' || $value =='0';
|
||||
|
||||
return !$isFalse;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace FluentCrm\Includes\Helpers;
|
||||
|
||||
use FluentCrm\App\Services\Libs\ConditionAssessor;
|
||||
|
||||
/**
|
||||
* @deprecated No longer used by internal code and not recommended. Please use FluentCrm\App\Services\Libs\ConditionAssessor instead
|
||||
*/
|
||||
|
||||
class ConditionAssesor
|
||||
{
|
||||
public static function matchAllGroups($groups, $inputs, $matchType = 'match_any')
|
||||
{
|
||||
_doing_it_wrong(__FUNCTION__, 'Use FluentCrm\App\Services\Libs\ConditionAssessor::matchAllGroups() instead', '2.5.0');
|
||||
|
||||
$hasConditionMet = true;
|
||||
foreach ($groups as $group) {
|
||||
$hasConditionMet = self::evaluate($group, $inputs);
|
||||
if($hasConditionMet && $matchType == 'match_any') {
|
||||
return true;
|
||||
}
|
||||
if ($matchType === 'match_all' && !$hasConditionMet) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return $hasConditionMet;
|
||||
}
|
||||
|
||||
public static function evaluate($conditionGroup, $inputs)
|
||||
{
|
||||
_doing_it_wrong(__FUNCTION__, 'Use FluentCrm\App\Services\Libs\ConditionAssessor::evaluate() instead', '2.5.0');
|
||||
|
||||
$hasConditionMet = true;
|
||||
$conditionals = Arr::get($conditionGroup, 'conditions');
|
||||
|
||||
if ($conditionals) {
|
||||
$toMatch = Arr::get($conditionGroup, 'match_type');
|
||||
foreach ($conditionals as $conditional) {
|
||||
$hasConditionMet = static::assess($conditional, $inputs);
|
||||
|
||||
if($hasConditionMet && $toMatch == 'match_any') {
|
||||
return true;
|
||||
}
|
||||
if ($toMatch === 'match_all' && !$hasConditionMet) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $hasConditionMet;
|
||||
}
|
||||
|
||||
public static function assess($conditional, $inputs)
|
||||
{
|
||||
_doing_it_wrong(__FUNCTION__, 'Use FluentCrm\App\Services\Libs\ConditionAssessor::assess() instead', '2.5.0');
|
||||
|
||||
if ($conditional['data_key']) {
|
||||
$sourceValue = Arr::get($inputs, $conditional['data_key']);
|
||||
$dataValue = $conditional['data_value'];
|
||||
|
||||
switch ($conditional['operator']) {
|
||||
case '=':
|
||||
if(is_array($sourceValue)) {
|
||||
return in_array($dataValue, $sourceValue);
|
||||
}
|
||||
return $sourceValue == $dataValue;
|
||||
break;
|
||||
case '!=':
|
||||
if(is_array($sourceValue)) {
|
||||
return !in_array($dataValue, $sourceValue);
|
||||
}
|
||||
return $sourceValue != $dataValue;
|
||||
break;
|
||||
case '>':
|
||||
return $sourceValue > $dataValue;
|
||||
break;
|
||||
case '<':
|
||||
return $sourceValue < $dataValue;
|
||||
break;
|
||||
case '>=':
|
||||
return $sourceValue >= $dataValue;
|
||||
break;
|
||||
case '<=':
|
||||
return $sourceValue <= $dataValue;
|
||||
break;
|
||||
case 'startsWith':
|
||||
return Str::startsWith($sourceValue, $dataValue);
|
||||
break;
|
||||
case 'endsWith':
|
||||
return Str::endsWith($sourceValue, $dataValue);
|
||||
break;
|
||||
case 'contains':
|
||||
return Str::contains($sourceValue, $dataValue);
|
||||
break;
|
||||
case 'doNotContains':
|
||||
return !Str::contains($sourceValue, $dataValue);
|
||||
break;
|
||||
case 'length_equal':
|
||||
if(is_array($sourceValue)) {
|
||||
return count($sourceValue) == $dataValue;
|
||||
}
|
||||
$sourceValue = strval($sourceValue);
|
||||
return strlen($sourceValue) == $dataValue;
|
||||
break;
|
||||
case 'length_less_than':
|
||||
if(is_array($sourceValue)) {
|
||||
return count($sourceValue) < $dataValue;
|
||||
}
|
||||
$sourceValue = strval($sourceValue);
|
||||
return strlen($sourceValue) < $dataValue;
|
||||
break;
|
||||
case 'length_greater_than':
|
||||
if(is_array($sourceValue)) {
|
||||
return count($sourceValue) > $dataValue;
|
||||
}
|
||||
$sourceValue = strval($sourceValue);
|
||||
return strlen($sourceValue) > $dataValue;
|
||||
break;
|
||||
case 'match_all':
|
||||
$sourceValue = (array) $sourceValue;
|
||||
$dataValue = (array) $dataValue;
|
||||
sort($sourceValue);
|
||||
sort($dataValue);
|
||||
return $sourceValue == $dataValue;
|
||||
break;
|
||||
case 'match_none_of':
|
||||
$sourceValue = (array) $sourceValue;
|
||||
$dataValue = (array) $dataValue;
|
||||
return !(array_intersect($sourceValue, $dataValue));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace FluentCrm\Includes\Helpers;
|
||||
|
||||
/**
|
||||
* @deprecated No longer used by internal code and not recommended. Please use FluentCrm\Framework\Support\Str instead
|
||||
*/
|
||||
class Str
|
||||
{
|
||||
/**
|
||||
* Determine if a given string starts with a given substring.
|
||||
*
|
||||
* @param string $haystack
|
||||
* @param string|array $needles
|
||||
* @return bool
|
||||
*/
|
||||
public static function startsWith($haystack, $needles)
|
||||
{
|
||||
if (is_array($haystack)) {
|
||||
$haystack = implode(' ', $haystack);
|
||||
}
|
||||
|
||||
foreach ((array)$needles as $needle) {
|
||||
if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string)$needle) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a given string ends with a given substring.
|
||||
*
|
||||
* @param string $haystack
|
||||
* @param string|array $needles
|
||||
* @return bool
|
||||
*/
|
||||
public static function endsWith($haystack, $needles)
|
||||
{
|
||||
if (is_array($haystack)) {
|
||||
$haystack = implode(' ', $haystack);
|
||||
}
|
||||
foreach ((array)$needles as $needle) {
|
||||
if (substr($haystack, -strlen($needle)) === (string)$needle) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a given string contains a given substring.
|
||||
*
|
||||
* @param string $haystack
|
||||
* @param string|array $needles
|
||||
* @return bool
|
||||
*/
|
||||
public static function contains($haystack, $needles)
|
||||
{
|
||||
if (is_array($haystack)) {
|
||||
$haystack = implode(' ', $haystack);
|
||||
}
|
||||
|
||||
foreach ((array)$needles as $needle) {
|
||||
if ($needle != '' && self::crm_mb_strpos($haystack, $needle) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function crm_mb_strpos($haystack, $needle)
|
||||
{
|
||||
if (function_exists('mb_strpos')) {
|
||||
return mb_strpos($haystack, $needle);
|
||||
}
|
||||
return strpos($haystack, $needle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a given string does not contain a given substring.
|
||||
*
|
||||
* @param string $haystack
|
||||
* @param string|array $needles
|
||||
* @return bool
|
||||
*/
|
||||
public static function doNotContains($haystack, $needles)
|
||||
{
|
||||
return !self::contains($haystack, $needles);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
// Silence is golden
|
||||
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
// Silence is golden.
|
||||
Reference in New Issue
Block a user