skrz / meta Goto Github PK
View Code? Open in Web Editor NEWMap from PHP arrays, JSON, XML and Protocol Buffers (protobuf) to objects and back
License: MIT License
Map from PHP arrays, JSON, XML and Protocol Buffers (protobuf) to objects and back
License: MIT License
Beside properties, it should be possible to annotate also getters and setters with annotations like @PhpArrayOffset
, @JsonProperty
etc. to support non-trivial cases.
Use cases:
class User
{
/** @var string */
public $firstName;
/** @var string */
public $lastName;
/**
* @return string
*
* @PhpArrayOffset("name")
*/
public function getName()
{
return sprintf("%s %s", $this->firstName, $this->lastName);
}
}
$user = new User();
$user->firstName = "Jakub";
$user->lastName = "Kulhan";
UserMeta::toArray($user);
// ["firstName" => "Jakub", "lastName" => "Kulhan", "name" => "Jakub Kulhan"]
class Product
{
/** @var string */
public $description;
/**
* @return string
*
* @PhpArrayOffset("descr")
*/
public function getDescr()
{
return $this->description;
}
}
deal->variants
, https://muj.skrz.cz/doc/xml-feed-rival) vs. Sdovolena XML feed (hierarchy tour->term_groups->terms
, https://muj.skrz.cz/doc/xml-feed-sdovolena)class Product
{
/** @var Variant */
public $variants;
/**
* @return TermGroup[]
*
* @PhpArrayOffset("term_groups")
*/
public function getTermGroups()
{
// converts Variants to TermGroups
}
/**
* @var TermGroup[]
* @return void
*
* @PhpArrayOffset("term_groups")
*/
public function setTermGroups($termGroups)
{
// converts TermGroups to Variants
}
}
Add stack to check for loops.
To generate fast object checksums.
namespace Skrz\Meta;
interface MetaInterface
{
// ...
public static function hash($object, $algoOrCtx = "md5", $raw = false);
// ...
}
@Hash
.hash_init()
, hash_update()
& hash_final()
for incremenetal updates.hash_init()
and then returns hash. If passed hash context, updates it, does not return anything.Useful if certain group can be deterministically infered from field name - e.g. db
group has names in underscore notation of field names. Should be able to add callback to module in *MetaSpec
.
When using Doctrine entities there's a problem with transforming them to arrays.
Example:
/** @Doctrine\ORM\Mapping\Entity */
class Cart
{
/** @var CartItem[] */
public $cartItems;
public function __construct()
{
$this->cartItems = new ArrayCollection();
}
}
ArrayCollection serves as a helper when modifiying the array.
Problems occurs with this:
/** @var Cart $cart */
$arr = CartMeta::toArray($cart);
Because skrz/meta (PhpModule) applies casting to child array entities like this:
foreach ((array)$input['cartItems'] as $k0 => $v0) {
...
}
Quickfix is easy - remove the casting. Better solution would be to support ArrayCollections :)
Meta currently wraps PHP's type system (https://github.com/skrz/meta/tree/master/src/Skrz/Meta/Reflection) and uses standard PHP reflection. It has several downsides:
protoc-gen-php
)Consider replacing standard PHP reflection with Roave/BetterReflection and type system wrapping with phpDocumentor/TypeResolver.
If I add the repo as a dependency and then try and use protoc-gen-php
it breaks saying it can't load autoload.php
.
failed to open stream: No such file or directory in ..../vendor/skrz/meta/bin/protoc-gen-php on line 10
require_once(): Failed opening required '....meta/bin/../vendor/autoload.php'
Could protoc-gen-php
be added to the bin
list in composer.json?
Edit: I realise now that won't even fix it. I found this stack chain which seems promising though:
There is no way to determine what meta object represents given object. Convention <namespace>\Meta\<classname>Meta
cannot be used because of proxy objects.
Self-describing objects would implement Skrz\Meta\ObjectInterface
:
namespace Skrz\Meta;
interface ObjectInterface
{
/**
* @return MetaInterface
*/
public static function meta();
}
@ProtobufField(number=X, wireType=T, unsigned=U, packed=P)
where X
is a tag number and T
is a wire type string (see https://developers.google.com/protocol-buffers/docs/encoding)protoc-gen-php
executable.Useful for many kinds of API - REST, GraphQL, Falcor Model etc. to filter fields that will be sent to client.
Filter can be specified as array where keys are property names that should be exported. Only mentioned properties will be exported. Cannot include whole objects (only atomic properties can be filtered). Properties of sub-objects can be specified using sub-array filters.
class Product
{
/** @var string */
public $title;
/** @var string */
public $slug;
/** @var Category[] */
public $categories;
}
class Category
{
/** @var string */
public $title;
/** @var string */
public $slug;
}
$product = new Product();
$product->title = "iPhone 6";
$product->slug = "iphone-6";
$electronics = new Category();
$electronics->title = "Electronics";
$electronics->slug = "electronics";
$mobilePhones = new Category();
$mobilePhones->title = "Mobile Phones";
$mobilePhones->slug = "mobile-phones";
$product->categories = [$electronics, $mobilePhones];
ProductMeta::toArray($product, null, ["title" => true]);
// ["title" => "iPhone 6"]
ProductMeta::toArray($product, null, ["slug" => true]);
// ["slug" => "iphone-6"]
ProductMeta::toArray($product, null, ["categories" => true]);
// throw InvalidArgumentException("An atomic property has to be specified.")
ProductMeta::toArray($product, null, ["categories" => ["title" => true]]);
// ["categories" => [["title" => "Electronics"], ["title" => "Mobile Phones"]]]
Is it possible to generate php from proto files with Meta? Would be great to see an example workflow.
class AnEntity
{
/**garbage
* @var int
*/
protected $id;
}
Currently the message says "Property AnEntity::$id of type mixed. Either add @var, or @transient annotation." The user is confused, because there is @var
annotation. The problem is caused by PHP not recognizing comment as doc comment. Error message should warn that there is no doc comment.
fromXml
to be passed XMLReader
or DOMElement
(use XMLReader::expand()
or create separate implementations?)toXml
can be passed XMLWriter
or creates DOMElement
(needs separate implementations)@XmlElement
+ @XmlElementWrapper
annotations, e.g.class Product
{
/**
* @var string[]
*
* @XmlElement("IMAGE")
* @XmlElementWrapper("IMAGES")
*/
public $images;
}
All following <PRODUCT>
s should result in same object. Should be serialized as 1st option.
<PRODUCT>
<IMAGES>
<IMAGE>http://skrz.cz/storage/img/20150904/abcd.jpg</IMAGE>
<IMAGE>http://skrz.cz/storage/img/20150904/efgh.jpg</IMAGE>
</IMAGES>
</PRODUCT>
<PRODUCT>
<IMAGE>http://skrz.cz/storage/img/20150904/abcd.jpg</IMAGE>
<IMAGE>http://skrz.cz/storage/img/20150904/efgh.jpg</IMAGE>
</PRODUCT>
<PRODUCT>
<IMAGES>
<IMAGE>http://skrz.cz/storage/img/20150904/abcd.jpg</IMAGE>
</IMAGES>
<IMAGE>http://skrz.cz/storage/img/20150904/efgh.jpg</IMAGE>
</PRODUCT>
@XmlValue
to map content of a node to object property:class Product
{
/**
* @var Image[]
*
* @XmlElement("IMAGE")
* @XmlElementWrapper("IMAGES")
*/
public $images;
}
class Image
{
/**
* @var string
*
* @XmlValue
*/
public $url;
}
@XmlAttribute
to map attribute of a node to object property:class Product
{
/**
* @var Param[]
*
* @XmlElement("param")
*/
public $params;
}
class Param
{
/**
* @var string
*
* @XmlAttribute("name")
*/
public $name;
/**
* @var string
*
* @XmlValue
*/
public $value;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.