mover-io/belt

工具腰带

v2.0.1 2016-07-27 18:59 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:46:40 UTC


README

一个充满彩虹和酷炫输出的PHP开发者工具腰带!

@jacobstr 用爱创建,并在 @mover 上经过测试和验证。

追踪

通过智能和彩色的堆栈、源和结果格式化输出提供错误、日志和CLI调试工具。比直接使用 error_log 更好,因为

  1. 您总是知道日志语句发生的行号,因此更容易稍后删除。
  2. 使用更大的 traceDepth,您可以识别代码的调用位置。
  3. 着色选项有助于在控制台调试会话期间,当您需要大量数据并需要一种方法来视觉扫描特定消息时。

示例

Belt\Trace::debug($your_result);
// with Stack Trace
Belt\Trace::traceDepth(7)->debug($your_result);

文本

提供常用字符串辅助器的字符串格式辅助器。

示例

$route = Belt\Text::ensureNoPrefix($route, "/v2/");
$route = Belt\Text::ensureNoSuffix($route, "/");

数组

提供常见操作的数组辅助器。

示例

// Safe getter that returns null if no value for key
$value = Belt\Array::get($array, 'key', null)

性能分析

提供函数调用时的内存和执行时间性能分析。

SchemaObject

Facebook的React有一个类似的概念,称为 shapeSchemaObject 在您想要将普通数组包装为类型时很有用。这反过来又很有用,因为您想要为函数添加类型提示。与其声明 $user_info 参数,您可以指定 UserInfo $user_info。交叉引用 UserInfo 立即告诉您对象应该具有哪些键/值对。此外,SchemaObject 可帮助进行验证,以确保与给定键对应的值具有正确的类型。

<?php namespace Mover\Connectors\Sharing;

use Belt\SchemaObject;
use Belt\Arrays;

class UserInfo extends SchemaObject
{
    protected static $attribute_spec = array(
        'first_name'  => array('type' => 'string'),
        'last_name'   => array('type' => 'string'),
        'email'       => array('type' => 'string'),
        // E.g. this could be an id from the migration source system.
        'external_id' => array('type' => 'string'),
        // The users id' in the active connector.
        'id'          => array('type' => 'string'),
        // The raw data from the third party if available. This is optional.
        // Code should never rely on a raw field. We may use it for debugging
        // and introspection.
        'raw'         => array('type' => 'array')
    );

    public function fullname()
    {
        return implode(
            ' ',
            array_filter(
                array($this->first_name, $this->last_name)
            )
        );
    }

    /**
     * Splits a full name (first + last name) into an array of two parts, the
     * first and last name. Note that this assumes only a single space separates
     * the first and last name.
     */
    public static function splitName($fullname)
    {
          $name_parts = explode(" ", $fullname, 2);
          $first_name = Arrays::get($name_parts, 0, "");
          $last_name  = Arrays::get($name_parts, 1, "");
          return array($first_name, $last_name);
    }
}