joomla/data
Composer 安装命令:
composer require joomla/data
包简介
Joomla Data Package
关键字:
README 文档
README
Data\DataObject
Data\DataObject is a class that is used to store data but allowing you to access the data by mimicking the way PHP handles class properties. Rather than explicitly declaring properties in the class, Data\DataObject stores virtual properties of the class in a private internal array. Concrete properties can still be defined but these are separate from the data.
Construction
The constructor for a new Data\DataObject object can optionally take an array or an object. The keys of the array or the properties of the object will be bound to the properties of the Data\DataObject object.
use Joomla\Data\DataObject; // Create an empty object. $object1 = new DataObject; // Create an object with data. You can use an array or another object. $data = array( 'foo' => 'bar', ); $object2 = new DataObject($data); // The following should echo "bar". echo $object2->foo;
General Usage
Data\DataObject includes magic getters and setters to provide access to the internal property store as if they were explicitly declared properties of the class.
The bind method allows for injecting an existing array or object into the Data\DataObject object.
The dump method gets a plain stdClass version of the Data\DataObject object's properties. It will also support recursion to a specified number of levels where the default is 3 and a depth of 0 would return a stdClass object with all the properties in native form. Note that the dump method will only return virtual properties set binding and magic methods. It will not include any concrete properties defined in the class itself.
The JsonSerializable interface is implemented. This method proxies to the dump method (defaulting to a recursion depth of 3). Note that this interface only takes effect implicitly in PHP 5.4 so any code built for PHP 5.3 needs to explicitly use either the jsonSerialize or the dump method before passing to json_encode.
The Data\DataObject class also implements the IteratorAggregate interface so it can easily be used in a foreach statement.
use Joomla\Data\DataObject; // Create an empty object. $object = new DataObject; // Set a property. $object->foo = 'bar'; // Get a property. $foo = $object->foo; // Binding some new data to the object. $object->bind(array('goo' => 'car'); // Get a plain object version of the data object. $stdClass = $object->dump(); // Get a property with a default value if it is not already set. $foo = $object->foo ?: 'The default'; // Iterate over the properties as if the object were a real array. foreach ($object as $key => $value) { echo "\n$key = $value"; } if (version_compare(PHP_VERSION, '5.4') >= 0) { // PHP 5.4 is aware of the JsonSerializable interface. $json = json_encode($object); } else { // Have to do it the hard way to be compatible with PHP 5.3. $json = json_encode($object->jsonSerialize()); }
Data\DataSet
Data\DataSet is a collection class that allows the developer to operate on a list of Data\DataObject objects as if they were in a typical PHP array (Data\DataSet implements the ArrayAccess, Countable and Iterator interfaces).
Construction
A typical Data\DataSet object will be instantiated by passing an array of Data\DataObject objects in the constructor.
use Joomla\Data\DataObject; use Joomla\Data\DataSet; // Create an empty object. $players = new DataSet( array( new DataObject(array('race' => 'Elf', 'level' => 1)), new DataObject(array('race' => 'Chaos Dwarf', 'level' => 2)), ) );
General Usage
Array elements can be manipulated with the offsetSet and offsetUnset methods, or by using PHP array nomenclature.
The magic __get method in the Data\DataSet class effectively works like a "get column" method. It will return an array of values of the properties for all the objects in the list.
The magic __set method is similar and works like a "set column" method. It will set all a value for a property for all the objects in the list.
The clear method will clear all the objects in the data set.
The keys method will return all of the keys of the objects stored in the set. It works like the array_keys function does on an PHP array.
use Joomla\Data\DataObject; // Add a new element to the end of the list. $players[] => new DataObject(array('race' => 'Skaven', 'level' => 2)); // Add a new element with an associative key. $players['captain'] => new DataObject(array('race' => 'Human', 'level' => 3)); // Get a keyed element from the list. $captain = $players['captain']; // Set the value of a property for all objects. Upgrade all players to level 4. $players->level = 4; // Get the value of a property for all object and also the count (get the average level). $average = $players->level / count($players); // Clear all the objects. $players->clear();
Data\DataSet supports magic methods that operate on all the objects in the list. Calling an arbitrary method will iterate of the list of objects, checking if each object has a callable method of the name of the method that was invoked. In such a case, the return values are assembled in an array forming the return value of the method invoked on the Data\DataSet object. The keys of the original objects are maintained in the result array.
use Joomla\Data\DataObject; use Joomla\Data\DataSet; /** * A custom data object. * * @since 1.0 */ class PlayerObject extends DataObject { /** * Get player damage. * * @return integer The amount of damage the player has received. * * @since 1.0 */ public function hurt() { return (int) $this->maxHealth - $this->actualHealth; } } $players = new DataSet( array( // Add a normal player. new PlayerObject(array('race' => 'Chaos Dwarf', 'level' => 2, 'maxHealth' => 40, 'actualHealth' => '32')), // Add an invincible player. new PlayerObject(array('race' => 'Elf', 'level' => 1)), ) ); // Get an array of the hurt players. $hurt = $players->hurt(); if (!empty($hurt)) { // In this case, $hurt = array(0 => 8); // There is no entry for the second player // because that object does not have a "hurt" method. foreach ($hurt as $playerKey => $player) { // Do something with the hurt players. } };
Data\DumpableInterface
Data\DumpableInterface is an interface that defines a dump method for dumping the properties of an object as a stdClass with or without recursion.
Installation via Composer
Add "joomla/data": "~3.0" to the require block in your composer.json and then run composer install.
{
"require": {
"joomla/data": "~3.0"
}
}
Alternatively, you can simply run the following from the command line:
composer require joomla/data "~3.0"
If you want to include the test sources, use
composer require --prefer-source joomla/data "~3.0"
joomla/data 适用场景与选型建议
joomla/data 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 330.24k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2013 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「data」 「joomla」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 joomla/data 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 joomla/data 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 joomla/data 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Shoot aims to make providing data to your templates more manageable
Adds the EDTF data type to Wikibase
A simple library that allows transform any kind of data to native php data or whatever
Joomla Preload Package
Data provider for yii2
统计信息
- 总下载量: 330.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 14
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2013-02-24