private function validateSubEntity(Entity $entity) { $rel = $entity->getRel(); if (empty($rel)) { throw new \Exception("Sub-entities must contain a rel"); } $href = $entity->getHref(); if (empty($href)) { throw new \Exception("Sub-entities must contain an href"); } }
public function encode(Entity $entity) { $response = array(); if ($class = $entity->getClass()) { $response['class'] = $class; } if ($properties = $entity->getProperties()) { $response['properties'] = $properties; } if ($entities = $entity->getEntities()) { $response['entities'] = array(); $subEntityEncoder = $this->factory->subEntity(); foreach ($entities as $subEntity) { $response['entities'][] = $subEntityEncoder->encode($subEntity); } } if ($actions = $entity->getActions()) { $response['actions'] = array(); $actionEncoder = $this->factory->action(); foreach ($actions as $action) { $response['actions'][] = $actionEncoder->encode($action); } } if ($links = $entity->getLinks()) { $response['links'] = array(); $linkEncoder = $this->factory->link(); foreach ($links as $link) { $response['links'][] = $linkEncoder->encode($link); } } return $response; }
<?php require_once "vendor/autoload.php"; use Siren\Components\Entity; use Siren\Components\Link; use Siren\Components\Action; use Siren\Components\Field; use Siren\Encoders\Encoder; $order = new Entity(); $order->addClass("order")->setProperties(array("orderNumber" => 42, "itemCount" => 3, "status" => "pending")); $items = new Entity(); $items->addClass("items")->addClass("collection")->addRel("http://x.io/rels/order-items")->setHref("http://api.x.io/orders/42/items"); $order->addEntity($items); $customerInfo = new Entity(); $customerInfo->addClass("info")->addClass("customer")->addRel("http://x.io/rels/customer")->setHref("http://api.x.io/customer")->setProperties(array("customerId" => "pj123", "name" => "Peter Joseph")); $link = new Link(); $link->addRel("self")->setHref("http://api.x.io/customers/pj123"); $customerInfo->addLink($link); $order->addEntity($customerInfo); $orderNumber = new Field(); $orderNumber->setName("orderNumber")->setType("hidden")->setValue(42); $productCode = new Field(); $productCode->setName("productCode"); $quantity = new Field(); $quantity->setName("quantity")->setType("number"); $action = new Action(); $action->setName("add-item")->setHref("http://api.x.io/orders/42/items")->setMethod("POST")->setTitle("Add Item")->addField($orderNumber)->addField($productCode)->addField($quantity); $order->addAction($action); $self = new Link(); $self->addRel("self")->setHref("http://api.x.io/orders/42"); $prev = new Link();