sntools/core

SN Toolbox的核心工具

此包的规范存储库似乎已不存在,因此该包已被冻结。

1.0.0 2016-01-09 14:54 UTC

This package is not auto-updated.

Last update: 2022-03-19 04:03:25 UTC


README

SN Toolbox旨在提供PHP开发中经常需要的经典工具,无需重复实现。

这个特定的包提供了核心工具:这些工具在大多数项目中都会很有用,也是Toolbox中其他工具所需的组件。

下载

通过Composer下载核心工具是最简单的方式。只需将以下内容添加到您的composer要求中,其中"~1.0"可以被您需要的任何版本所替换

"sntools/core": "~1.1"

NotImplementedException特殊异常类型

一种相当独特的异常类型,SNTools\NotImplementedException类旨在指出当前功能尚未实现。因此,它不应该出现在最终发布的代码中。

当您知道方法的签名,但尚未知道如何实现它时,此异常非常有用。这样,您可以开始处理需要该方法的其他组件,或者让您的队友处理它。

Object通用超类

所有完整的对象语言,如Java和C#,都有一个Object类,它是所有其他类的超类。SNTools\Object类旨在以相同的方式工作。

此类为各种魔法方法提供了默认行为,尤其是__toString()方法用于字符串转换,以及用于属性处理的各个方法。

属性处理依赖于SNTools\PropertyException异常进行错误处理(例如尝试写入只读属性)

Object类还提供了一个默认的"静态构造函数"。这个特殊的非公共方法在第一次创建类的对象时自动调用。这对于需要在创建新实例之前初始化的类很有用。

Object : Person类和属性处理器的使用示例

<?php
namespace SNTools\examples;
use SNTools\Object;
use SNTools\PropertyException;

/**
 * @property string $name
 * @property \DateTime $birthday
 * @property-read int $age
 * @author Samy Naamani <samy@namani.net>
 * @license https://github.com/sntools/core/blob/master/LICENSE MIT
 */
class Person extends Object {
    /** @var string */
    private $_name;
    /** @var \DateTime */
    private $_birthday;

    public function __construct($name, \DateTime $birthday = null) {
        parent::__construct();
        $this->name = $name;
        $this->birthday = is_null($birthday) ? new \DateTime() : $birthday;
    }

    public function __get($name) {
        switch($name) {
            case 'name':
            case 'birthday':
                $prop = "_$name";
                return $this->$prop;
            case 'age':
                return $this->birthday->diff(new \DateTime, true)->y;
            default:
                return parent::__get($name);
        }
    }

    public function __set($name, $value) {
        switch($name) {
            case 'name':
                if(!is_string($value)) throw new PropertyException('A person\'s name must be a string');
                if(empty($value)) throw new PropertyException('A person\'s name cannot be empty');
                $this->_name = $value;
                break;
            case 'birthday':
                if(interface_exists('\\DateTimeInterface') and $value instanceof \DateTimeInterface or $value instanceof \DateTime) {
                    if($value->diff(new \DateTime)->invert) throw new PropertyException('Nobody can be born in the future');
                    $this->_birthday = $value;
                } else throw new PropertyException('A person\'s birthday must be a date-time value');
                break;
            default:
                parent::__set($name, $value);
        }
    }
}

/// Usage
$paul = new Person('Paul', new \DateTime("5 years ago"));
printf("This line should display 'Paul' : %s\n", $paul->name);
$paul->name = 'Paul Edward';
printf("This line should display 'Paul Edward' : %s\n", $paul->name);
printf("This line should display a date from 5 years ago as MONTH-DAY-YEAR : %s\n", $paul->birthday->format('m-d-Y'));
printf("This line should display '5' : %s\n", $paul->age);
try {
    $paul->age = 6; // this will throw an exception
} catch (PropertyException $ex) {
    printf("Trying to write on age (not writable) caused exception : %s\n", $ex->getMessage());
}

使用示例:DAO抽象类和静态构造函数

<?php
namespace SNTools\examples;
use SNTools\Object;

/**
 * Subclasses will have access to the property as a unique PDO connexion
 * shared among them. The connexion cannot be overriden once it's been
 * called once.
 * @property-read \PDO $pdo
 * @author Samy Naamani <samy@namani.net>
 * @license https://github.com/sntools/core/blob/master/LICENSE MIT
 */
abstract class DAO extends Object{
    private static $_pdo;
    protected static function __constructStatic() {
        $parent = parent::__constructStatic();
        if(!$parent) {
            self::$_pdo = new \PDO('mysql:host=localhost;dbname=mydb;charset=utf8', 'user', 'pwd', array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
        }
        return $parent;
    }
    public function __get($name) {
        switch($name) {
            case 'pdo':
                return self::$_pdo;
            default:
                return parent::__get($name);
        }
    }
}

API参考

要生成文档,请使用apigen.neon文件在"docs"文件夹中生成文档

> apigen generate

测试

在tests/子文件夹中提供了单元测试,使用PHPUnit。

贡献者

Samy Naamani samy@namani.net

许可证

MIT