PHP code example of skrz / meta
1. Go to this page and download the library: Download skrz/meta library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
skrz / meta example snippets
namespace Skrz\API;
class Category
{
/** @var string */
public $name;
/** @var string */
public $slug;
/** @var Category */
public $parentCategory;
}
public function toJson()
{
return json_encode(array(
"name" => $this->name,
"slug" => $this->slug,
"parentCategory" => $this->parentCategory ? $this->parentCategory->toJson() : null
));
}
namespace Skrz;
use Skrz\Meta\AbstractMetaSpec;
use Skrz\Meta\JSON\JsonModule;
use Skrz\Meta\PHP\PhpModule;
class ApiMetaSpec extends AbstractMetaSpec
{
protected function configure()
{
$this->match("Skrz\\API\\*")
->addModule(new PhpModule())
->addModule(new JsonModule());
}
}
use Symfony\Component\Finder\Finder;
$files = array_map(function (\SplFileInfo $file) {
return $file->getPathname();
}, iterator_to_array(
(new Finder())
->in(__DIR__ . "/API")
->name("*.php")
->notName("*Meta*")
->files()
));
$spec = new ApiMetaSpec();
$spec->processFiles($files);
use Skrz\API\Category;
use Skrz\API\Meta\CategoryMeta;
$parentCategory = new Category();
$parentCategory->name = "The parent category";
$parentCategory->slug = "parent-category";
$childCategory = new Category();
$childCategory->name = "The child category";
$childCategory->slug = "child-category";
$childCategory->parentCategory = $parentCategory;
var_export(CategoryMeta::toArray($childCategory));
// array(
// "name" => "The child category",
// "slug" => "child-category",
// "parentCategory" => array(
// "name" => "The parent category",
// "slug" => "parent-category",
// "parentCategory" => null,
// ),
// )
echo CategoryMeta::toJson($childCategory);
// {"name":"The child category","slug":"child-category","parentCategory":{"name":"The parent category","slug":"parent-category","parentCategory":null}}
$someCategory = CategoryMeta::fromJson(array(
"name" => "Some category",
"ufo" => 42, // unknown fields are ignored
));
var_export($someCategory instanceof Category);
// TRUE
var_export($someCategory->name === "Some category");
// TRUE
use Skrz\API\Category;
use Skrz\API\Meta\CategoryMeta;
use Skrz\Meta\Fields\Fields;
$parentCategory = new Category();
$parentCategory->name = "The parent category";
$parentCategory->slug = "parent-category";
$childCategory = new Category();
$childCategory->name = "The child category";
$childCategory->slug = "child-category";
$childCategory->parentCategory = $parentCategory;
var_export(CategoryMeta::toArray($childCategory, null, Fields::fromString("name,parentCategory{name}")));
// array(
// "name" => "The child category",
// "parentCategory" => array(
// "name" => "The parent category",
// ),
// )
namespace Skrz\API;
use Skrz\Meta\PHP\PhpArrayOffset;
class Category
{
/**
* @var string
*
* @PhpArrayOffset("THE_NAME")
* @PhpArrayOffset("name", group="javascript")
*/
protected $name;
/**
* @var string
*
* @PhpArrayOffset("THE_SLUG")
* @PhpArrayOffset("slug", group="javascript")
*/
protected $slug;
public function getName() { return $this->name; }
public function getSlug() { return $this->slug; }
}
// ...
use Skrz\API\Meta\CategoryMeta;
$category = CategoryMeta::fromArray(array(
"THE_NAME" => "My category name",
"THE_SLUG" => "category",
"name" => "Different name" // name is not an unknown field, so it is ignored
));
var_export($category->getName());
// "My category name"
var_export($category->getSlug());
// "category"
var_export(CategoryMeta::toArray($category, "javascript"));
// array(
// "name" => "My category name",
// "slug" => "category",
// )
namespace Skrz\API;
use Skrz\Meta\PHP\PhpArrayOffset;
use Skrz\Meta\JSON\JsonProperty;
class Category
{
/**
* @var string
*
* @PhpArrayOffset("THE_NAME")
* @JsonProperty("NAME")
*/
protected $name;
/**
* @var string
*
* @PhpArrayOffset("THE_SLUG")
* @JsonProperty("sLuG")
*/
protected $slug;
public function getName() { return $this->name; }
public function getSlug() { return $this->slug; }
}
// ...
use Skrz\API\Meta\CategoryMeta;
$category = CategoryMeta::fromArray(array(
"THE_NAME" => "My category name",
"THE_SLUG" => "category",
));
var_export(CategoryMeta::toJson($category));
// {"NAME":"My category name","sLuG":"category"}
// example: serialize object to XMLWriter
/**
* @XmlElement(name="SHOPITEM")
*/
class Product
{
/**
* @var string
*
* @XmlElement(name="ITEM_ID")
*/
public $itemId;
/**
* @var string[]
*
* @XmlElement(name="CATEGORYTEXT")
*/
public $categoryTexts;
}
$product = new Product();
$product->itemId = "SKU123";
$product->categoryTexts = array("Home Appliances", "Dishwashers");
$xml = new \XMLWriter();
$xml->openMemory();
$xml->setIndent(true);
$xml->startDocument();
$meta->toXml($product, null, $xml);
$xml->endDocument();
echo $xml->outputMemory();
// <?xml version="1.0"
namespace Animals;
use Skrz\Meta\PHP\PhpArrayOffset;
/**
* @PhpDiscriminatorMap({
* "cat" => "Animals\Cat", // specify subclass
* "dog" => "Animals\Dog"
* })
*/
class Animal
{
/**
* @var string
*/
protected $name;
}
class Cat extends Animal
{
public function meow() { echo "{$this->name}: meow"; }
}
class Dog extends Animal
{
public function bark() { echo "{$this->name}: woof"; }
}
// ...
use Animals\Meta\AnimalMeta;
$cat = AnimalMeta::fromArray(["cat" => ["name" => "Oreo"]]);
$cat->meow();
// prints "Oreo: meow"
$dog = AnimalMeta::fromArray(["dog" => ["name" => "Mutt"]]);
$dog->bark();
// prints "Mutt: woof"
namespace Animals;
use Skrz\Meta\PHP\PhpArrayOffset;
/**
* @PhpDiscriminatorOffset("type")
* @PhpDiscriminatorMap({
* "cat" => "Animals\Cat", // specify subclass
* "dog" => "Animals\Dog"
* })
*/
class Animal
{
/**
* @var string
*/
protected $type;
/**
* @var string
*/
protected $name;
}
class Cat extends Animal
{
public function meow() { echo "{$this->name}: meow"; }
}
class Dog extends Animal
{
public function bark() { echo "{$this->name}: woof"; }
}
// ...
use Animals\Meta\AnimalMeta;
$cat = AnimalMeta::fromArray(["type" => "cat", "name" => "Oreo"]);
$cat->meow();
// prints "Oreo: meow"
$dog = AnimalMeta::fromArray(["type" => "dog", "name" => "Mutt"]);
$dog->bark();
// prints "Mutt: woof"