alevikzs / phmap
该库提供将一些PHP结构(JSON字符串、数组、对象)映射到特定对象的功能。此库基于Phalcon注解。
Requires
- php: >=5.4.0
- ext-phalcon: 2.0.*
Requires (Dev)
- phalcon/devtools: 2.0.*
- phpunit/phpunit: 6.*
This package is not auto-updated.
Last update: 2024-09-14 18:04:22 UTC
README
关于
PhMap是一个PHP包,用于从JSON字符串、关联数组和对象创建对象。PhMap基于Phalcon注解。
要求
安装
如何使用
class Tree { private $height; private $name; private $branch; public function getHeight() { return $this->height; } public function setHeight($height) { $this->height = $height; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getBranch() { return $this->branch; } /** * @mapper(class="\Tests\Dummy\Branch") */ public function setBranch(Branch $branch) { $this->branch = $branch; } }
如您所见,如果您对象的某个属性具有其他类的类型,则必须为此属性的定义方法声明一个@mapper
注解。此注解有两个参数:class
和isArray
。第一个参数是类名的字符串,第二个是布尔值,表示您的属性值是数组还是不是。
class Branch { private $length; private $leaves; public function getLength() { return $this->length; } public function setLength($length) { $this->length = $length; } public function getLeaves() { return $this->leaves; } /** * @mapper(class="\Tests\Dummy\Leaf", isArray=true) */ public function setLeaves(array $leaves) { $this->leaves = $leaves; } } class Leaf { private $height; private $width; public function getHeight() { return $this->height; } public function setHeight($height) { $this->height = $height; } public function getWidth() { return $this->width; } public function setWidth($width) { $this->width = $width; } }
从JSON字符串创建Tree类的对象
$result = (new \PhMap\Wrapper\Json($json, 'Tree'))->map();
从关联数组创建Tree类的对象
$result = (new \PhMap\Mapper\Structure\Associative($array, 'Tree'))->map();
从另一个对象创建Tree类的对象
$result = (new \PhMap\Mapper\Structure\Object($object, 'Tree'))->map();
如果您不知道您的值的类型,可以使用\PhMap\Mapper\Smart
。在这种情况下,映射说明将自动应用。
$result = (new \PhMap\Wrapper\Smart($value, 'Tree'))->map();
默认情况下,映射器使用内存适配器,但您也可以使用文件适配器、APC适配器和XCache适配器
new \PhMap\Wrapper\Json($json, 'Tree', \PhMap\Mapper::MEMORY_ANNOTATION_ADAPTER); new \PhMap\Wrapper\Smart($json, 'Tree', \PhMap\Mapper::FILES_ANNOTATION_ADAPTER); new \PhMap\Mapper\Structure\Associative($array, 'Tree', \PhMap\Mapper::APC_ANNOTATION_ADAPTER); new \PhMap\Mapper\Structure\Object($object, 'Tree', \PhMap\Mapper::X_CACHE_ANNOTATION_ADAPTER);
您还可以将已存在的对象传递给构造函数
$tree = new Tree(); $result = (new \PhMap\Mapper\Smart($json, $tree))->map();
您可以使用映射器对象。只需设置必要的属性,然后调用map()
方法
$mapper = new \PhMap\Mapper\Structure\Object($tree, 'Tree', \PhMap\Mapper::X_CACHE_ANNOTATION_ADAPTER) $result = $mapper->map(); $mapper->setInputObject($branch) ->setOutputObject(new Branch()) ->setAnnotationAdapterType(Mapper::MEMORY_ANNOTATION_ADAPTER); $result = $mapper->map();
映射器对象有setTransforms(Transforms $transforms)
方法。此方法的一个参数是变换对象。该对象用于声明一组规则,其中每个规则指示输入值的哪个字段对应于输出值的哪个字段
$mapper = new \PhMap\Mapper\Structure\Object($tree, 'Tree') $transforms = (new \PhMap\Transforms()) ->add( (new \PhMap\Transform()) ->setInputFieldName('nameIn') ->setOutputFieldName('name') ) ->add( (new \PhMap\Transform()) ->setInputFieldName('branchIn') ->setOutputFieldName('branch') ->setTransforms( (new \PhMap\Transforms())->add( (new \PhMap\Transform()) ->setInputFieldName('leavesIn') ->setOutputFieldName('leaves') ) ) ); $result = $mapper->setTransforms($transforms)->map();
要跳过某些属性并防止它们被映射,您可以使用setSkipAttributes(array $attributes)
$mapper = new \PhMap\Mapper\Structure\Object($tree, 'Tree') $attributes = [ 'name', 'branch.length' ]; $result = $mapper->setSkipAttributes($attributes)->map();
如果您想禁用验证,可以使用disableValidation()
方法
$result = $mapper->disableValidation()->map();
如果您想使您的类可以映射某些值到自身,必须在类声明中使用MapperTrait
class Tree { use \PhMap\MapperTrait; //other class declaration }
然后在您的类中调用mapper()
或staticMapper()
方法
$tree = new Tree(); $result = $tree->mapper($json)->map(); $result = Tree::staticMapper($json)->map();
MIT许可(MIT)
版权所有 (c) 2016 Alexey Novikov alekseeey@gmail.com
在此条件下,任何人获得此软件及其相关文档文件(“软件”)的副本(“软件”),均可免费使用该软件而不受限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向提供软件的人员进行此类行为,前提如下
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“现状”提供,不提供任何形式的质量保证,无论是明示的、暗示的,还是包括但不限于以下保证:适销性、特定用途适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论其性质是合同、侵权或其他,均与软件或其使用或其他方式无关。