Comhon!PHP 框架

v5.0.0 2020-12-01 02:51 UTC

This package is auto-updated.

Last update: 2024-09-09 07:17:14 UTC


README

概要

Comhon!(通用对象管理器,哈希表或无!)是一个基于 gess what ?... 哈希表的对象管理器!

它将允许您在不编写任何代码的情况下导入、导出、序列化(在 SQL 数据库、XML 文件、JSON 文件)对象。您不需要创建任何类(实际上您可以,如果您不习惯使用它们),您只需要在清单(XML 文件或 JSON 文件)中描述您的模型。例如,一个清单可以链接到一个 SQL 表,您将能够执行选择/插入/更新,而不必编写 SQL 查询(实际上就像 ORM)

其他功能

清单示例

清单允许通过列出其属性来描述一个概念。清单可以定义在 XML 或 JSON 格式

描述人的简单 XML 清单

<manifest version="3.0" name="Example\Person">
    <properties>
        <property name="identifier" is_id="1" __inheritance__="Comhon\Manifest\Property\String"/>
        <property name="firstName" __inheritance__="Comhon\Manifest\Property\String"/>
        <property name="lastName" __inheritance__="Comhon\Manifest\Property\String"/>
        <property name="age" __inheritance__="Comhon\Manifest\Property\Integer"/>
    </properties>
</manifest>

描述人的简单 JSON 清单

{
    "version": "3.0",
    "name": "Example\\Person",
    "properties": [
        {
            "name": "identifier",
            "is_id": true,
            "__inheritance__": "Comhon\\Manifest\\Property\\String"
        },
        {
            "name": "firstName",
            "__inheritance__": "Comhon\\Manifest\\Property\\String"
        },
        {
            "name": "lastName",
            "__inheritance__": "Comhon\\Manifest\\Property\\String"
        },
        {
            "name": "age",
            "__inheritance__": "Comhon\\Manifest\\Property\\Integer"
        }
    ]
}

有关构建复杂清单的更多信息,请参阅 清单维基页面

代码示例

// first way to instanciate a comhon object
$personModel = InstanceModel::getInstance()->getInstanceModel('Example\Person');
$person = $personModel->getObjectInstance();

// second way to instanciate a comhon object
$person = new ComhonObject('Example\Person');

// third way to instanciate a comhon object only if you have defined a class
$person = new Person();

// set comhon object values
$person->setValue('age', 21);
$person->setValue('foo', 'bar'); // will not work because person doesn't have property 'foo'

// get a comhon object value
$age = $person->getValue('age');

// instanciate a comhon object by importing json
$interfacer = new StdObjectInterfacer();
$person = $interfacer->import(json_decode('{"first_name":"john","last_name":"john","age":21}'), $personModel);

// fill an existing comhon object by importing json
$interfacer = new StdObjectInterfacer();
$person->fill(json_decode('{"first_name":"john","last_name":"john","age":21}'), $interfacer);

// export a comhon object to xml and print it as string
$interfacer = new XMLInterfacer();
$nodeXML = $interfacer->export($person);
echo $interfacer->toString($nodeXML);
// output : <root first_name="john" last_name="john" age=21 />

// load, update and save an object (from/to sql database or xml file or json file)
$loadedPerson = $personModel->loadObject('id_of_a_person');
$loadedPerson->setValue('age', 25);
$loadedPerson->save();

动机

给您的项目一个尝试 Comhon 对象管理的机会,并避免在代码源中出现的许多特定情况,这些情况会使您的项目难以维护和改进

安装

有关更多信息,请参阅 安装维基页面

文档

有关更多信息,请参阅 维基页面