jyxon/data-tools

一个简单的数据操作库。

1.2.0 2018-07-05 19:08 UTC

This package is auto-updated.

Last update: 2024-09-09 14:14:25 UTC


README

Data Tools 是一个简单的库,用于对数据进行基本操作。这个库简化了开发过程中有时可能需要执行的一些简单操作。通过使用对象,它还增加了一些额外的功能。

安装

可以通过 composer 安装 Data Tools。这可以通过以下命令完成

composer require jyxon/data-tools

用法

该包分为2个命名空间(目前)。

本地

在本地命名空间中,我们定义了处理原生数据类型和 PHP 对象的简单操作包。

ArrayTool

ArrayTool 是一个工具,它允许在数组中设置路径。它还允许以这种方式展开数组并扩展扁平化数组。对于 ArrayTool,一个 pathflat path 是用 "/"(斜杠)分隔的路径(foo/bar)。一个 array path 实际上是路径爆炸的结果(explode('/', $path) 的结果)。

ArrayTool 可以按照以下方式进行初始化

use Jyxon\DataTools\Native\ArrayTool;

$associative_array = [
    'foo' => [
        'bar' => 'baz'
    ]
];

$array = new ArrayTool($associative_array);

以下函数可用于 ArrayTool

/**
 * The array is expanded and multidimensional.
 */
ArrayTool::STATUS_EXPANDED;

/**
 * The array is flat and one dimensional.
 */
ArrayTool::STATUS_FLAT;

/**
 * Constructor
 *
 * @param array $array The array that needs to be loaded into the tool.
 * @param int $type The type of the array. Can either be ArrayTool::STATUS_EXPANDED or ArrayTool::STATUS_FLAT
 */
public function __construct(array $array, int $type = self::STATUS_EXPANDED);

/**
 * Returns the array in it's current state.
 *
 * @return array
 */
public function getArray(): array;

/**
 * Loads a new array into the tool.
 *
 * @return ArrayTool
 */
public function setArray(array $array, int $type = self::STATUS_EXPANDED): ArrayTool;

/**
 * Expands the array from the flattened form.
 *
 * @return ArrayTool
 */
public function expandArray(): ArrayTool;

/**
 * Flattens the array.
 *
 * @return ArrayTool
 */
public function flattenArray(): ArrayTool;

/**
 * Fetches a value in an array through a "/" separated path.
 *
 * @param string $path
 *
 * @return mixed
 */
public function getByPath(string $path);

/**
 * Sets an entry by a path.
 *
 * @param string $path
 * @param mixed $value
 *
 * @return ArrayTool
 */
public function setByPath(string $path, $value): ArrayTool;

/**
 * Strips all empty strings from an array (array_filter, but explicitly with strings).
 *
 * @return ArrayTool
 */
public function stripEmptyString(): ArrayTool;

/**
 * Fetches the value within an array by the given array path.
 *
 * @return mixed
 */
public function getArrayValueByArrayPath(array $path);

/**
 * Sets a value in the array by an array path.
 *
 * @param array $path
 * @param mixed $value
 *
 * @return ArrayTool
 */
public function setArrayValueByArrayPath(array $path, $value): ArrayTool;

/**
 * Creates a subarray wether the mixed variable is an array or not.
 * If the mixed variable is an array it creates an entry.
 * If it is not an array it will create an array with the key set as a variable.
 *
 * @param mixed $mixed
 * @param string $key
 *
 * @return array
 */
public function forceSubArrayKey($mixed, string $key): array;

DataObject

DataObject 更多的是一个对象的标准功能的基类,而不是一个工具。你可以用它作为基类,也可以作为独立的对象使用,作为一个数据容器。你可以选择让现有的类扩展 DataObject 来“添加”功能到该类。或者你也可以选择单独初始化它

use Jyxon\DataTools\Native\DataObjet;

$container = new DataObject();

此对象添加的功能包括

/**
 * Sets data on the object.
 *
 * @param string $key
 * @param mixed $value
 */
public function setData(string $key, $value);

/**
 * Retrieves the value of the object.
 *
 * @param string $key
 *
 * @return mixed
 */
public function getData(string $key = '');

/**
 * Check wether the value is set or not.
 *
 * @param string $key
 *
 * @return boolean
 */
public function hasData(string $key): bool;

ReflectorTool

ReflectorTool 向 PHP 的初始 ReflectionClass 添加了一些额外的函数。它可以按照以下方式初始化

use Jyxon\DataTools\Native\ReflectorTool;

$reflection = new ReflectorTool('\\Foo\\Bar\\Baz');

ReflectorTool 添加了以下函数

/**
 * Returns the type of the object as defined in the constants.
 *
 * @return int This can be ReflectorTool::TYPE_UNKNOWN, ReflectorTool::TYPE_INTERFACE, ReflectorTool::TYPE_CLASS or ReflectorTool::TYPE_TRAIT.
 */
public function getType(): int;

/**
 * Fetches the constructor arguments.
 *
 * @return bool|ReflectionParameter[]
 */
public function getConstructorArgs();

/**
 * Fetches the arguments of a method.
 *
 * @param string $methodName
 *
 * @return bool|ReflectionParameter[]
 */
public function getMethodArguments(string $methodName);

操作

在操作命名空间中,我们定义了不是 PHP 原生的包,但增加了功能或简化了标准操作。

CalculationTool

CalculationTool 提供了一些可以避免繁琐实现的计算。

它可以按照以下方式进行初始化

use Jyxon\DataTools\Operation\CalculationTool;

$calculator = new CalculationTool();

它公开以下函数

/**
 * Gets the percentage of a factor based on a base number.
 *
 * @param float $baseNumber
 * @param float $factorNumber
 * @param float $basePercentage
 *
 * @return float
 */
public function getPercentageOfFactor(float $baseNumber, float $factorNumber, float $basePercentage = 1): float;

/**
 * Gets the factor of a percentage based on a base number.
 *
 * @param float $baseNumber
 * @param float $percentage
 * @param float $basePercentage
 *
 * @return float
 */
public function getFactorOfPercentage(float $baseNumber, float $percentage, float $basePercentage = 1): float;

/**
 * Adds a percentage to a base number.
 *
 * @param float $baseNumber
 * @param float $percentage
 * @param float $basePercentage
 *
 * @return float
 */
public function addPercentage(float $baseNumber, float $percentage, float $basePercentage = 1): float;

/**
 * Subtracts a percentage of a base number.
 *
 * @param float $baseNumber
 * @param float $percentage
 * @param float $basePercentage
 *
 * @return float
 */
public function subtractPercentage(float $baseNumber, float $percentage, float $basePercentage = 1): float;

/**
 * Returns the average of an array.
 *
 * @param array $numbers
 *
 * @return float
 */
public function average(array $numbers): float;

/**
 * Returns the median of an array of numbers.
 *
 * @param array $numbers
 *
 * @return float
 */
public function median(array $numbers): float;

/**
 * Returns horizon distance between 2 points on earth.
 * Using the Haversine formula.
 *
 * @param float $lat1
 * @param float $lon1
 * @param float $lat2
 * @param float $lon2
 *
 * @return float Distance in KM
 */
public function distance(float $lat1, float $lon1, float $lat2, float $lon2);

/**
 * Calculates the correlation between 2 sets of data.
 *
 * @param float[] $x
 * @param float[] $y
 *
 * @return float
 */
public function correlation(array $x, array $y): float;

/**
 * Calculates the offset between 2 arrays.
 *
 * @param float[] $x
 * @param float[] $y
 *
 * @return float
 */
public function offset(array $x, array $y): float;

/**
 * Checks wether the provided array are similar or not.
 *
 * @param float[] $x
 * @param float[] $y
 *
 * @return float
 */
public function similarity(array $x, array $y): float;

/**
 * Runs the similarity function with sorted arrays.
 *
 * @param float[] $x
 * @param float[] $y
 *
 * @return float
 */
public function sortedSimilarity(array $x, array $y): float;

PathTool

PathTool 是路径解析器。它将可读路径转换为应用程序可用的路径。

use Jyxon\DataTools\Operation\PathTool;

$path = new PathTool('foo/bar/baz?foo=bar', true);

它公开以下函数

/**
 * Constructor
 *
 * @param string $path
 * @param bool $parseQuery
 */
public function __construct(string $path, bool $parseQuery = false);

/**
 * Returns the path that is set.
 *
 * @return string
 */
public function getPath(): string;

/**
 * Sets the path that needs to be parsed.
 *
 * @param string $path
 *
 * @return void
 */
public function setPath(string $path);

/**
 * Returns the previous path.
 *
 * @return PathTool
 */
public function getPreviousPath(): PathTool;

/**
 * Reverts the current object to the last.
 *
 * @return void
 */
public function revertToPreviousPath();

/**
 * Returns the expanded path.
 *
 * @return array
 */
public function getExpanded(): array;

/**
 * Merges multiple paths into the current path.
 *
 * @param string ...$param
 *
 * @return void
 */
public function mergePaths();

/**
 * Returns an associative array of the parameters from the path.
 *
 * @return array
 */
public function getParameters(): array;

VersionTool

分析应用程序版本的工具。

它可以按照以下方式进行初始化

use Jyxon\DataTools\Operation\VersionTool;

$path = new VersionTool('1.2.3');

它公开以下函数

/**
 * Constructor
 *
 * @param string $version
 */
public function __construct(string $version);

/**
 * Compares one version to another.
 *
 * @param VersionTool $version
 *
 * @return bool
 */
public function versionCompare(VersionTool $version, string $operator): bool;

/**
 * Resets the object with a new version.
 *
 * @param string $version
 *
 * @return void
 */
public function setVersion(string $version);

/**
 * Returns the version that is set within the VersionTool.
 *
 * @return string
 */
public function getVersion(): string;

/**
 * __toString interpreter hook.
 *
 * @return string
 */
public function __toString(): string;

/**
 * Returns the version depth.
 *
 * @return int
 */
public function getVersionDepth(): int;

/**
 * Returns the expanded version.
 *
 * @return array
 */
public function getExpandedVersion(): array;

/**
 * Returns the version number at a certain depth.
 *
 * @param int $depth
 *
 * @return int
 */
public function getVersionAt(int $depth): int

反馈

我们希望得到一些关于此包的反馈。您可以在 GitHub 上创建问题。同时请注意,由于我们自己的开发过程需要此包进行更改,这个包可能会在不久的将来收到很多更新。