Source for file document.php

Documentation is available at document.php

  1. <?php
  2. /**
  3.  * @package     FrameworkOnFramework
  4.  * @subpackage  hal
  5.  * @copyright   Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
  6.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8. defined('_JEXEC'or die;
  9.  
  10. /**
  11.  * Implementation of the Hypertext Application Language document in PHP. It can
  12.  * be used to provide hypermedia in a web service context.
  13.  *
  14.  * @package  FrameworkOnFramework
  15.  * @since    2.1
  16.  */
  17. {
  18.     /**
  19.      * The collection of links of this document
  20.      *
  21.      * @var   FOFHalLinks 
  22.      */
  23.     private $_links null;
  24.  
  25.     /**
  26.      * The data (resource state or collection of resource state objects) of the
  27.      * document.
  28.      *
  29.      * @var   array 
  30.      */
  31.     private $_data null;
  32.  
  33.     /**
  34.      * Embedded documents. This is an array of FOFHalDocument instances.
  35.      *
  36.      * @var   array 
  37.      */
  38.     private $_embedded array();
  39.  
  40.     /**
  41.      * When $_data is an array we'll output the list of data under this key
  42.      * (JSON) or tag (XML)
  43.      *
  44.      * @var   string 
  45.      */
  46.     private $_dataKey '_list';
  47.  
  48.     /**
  49.      * Public constructor
  50.      *
  51.      * @param   mixed  $data  The data of the document (usually, the resource state)
  52.      */
  53.     public function __construct($data null)
  54.     {
  55.         $this->_data $data;
  56.         $this->_links new FOFHalLinks;
  57.     }
  58.  
  59.     /**
  60.      * Add a link to the document
  61.      *
  62.      * @param   string      $rel        The relation of the link to the document.
  63.      *                                   See RFC 5988 http://tools.ietf.org/html/rfc5988#section-6.2.2 A document MUST always have
  64.      *                                   a "self" link.
  65.      * @param   FOFHalLink  $link       The actual link object
  66.      * @param   boolean     $overwrite  When false and a link of $rel relation exists, an array of links is created. Otherwise the
  67.      *                                   existing link is overwriten with the new one
  68.      *
  69.      * @see FOFHalLinks::addLink
  70.      *
  71.      * @return  boolean  True if the link was added to the collection
  72.      */
  73.     public function addLink($relFOFHalLink $link$overwrite true)
  74.     {
  75.         return $this->_links->addLink($rel$link$overwrite);
  76.     }
  77.  
  78.     /**
  79.      * Add links to the document
  80.      *
  81.      * @param   string   $rel        The relation of the link to the document. See RFC 5988
  82.      * @param   array    $links      An array of FOFHalLink objects
  83.      * @param   boolean  $overwrite  When false and a link of $rel relation exists, an array of
  84.      *                                links is created. Otherwise the existing link is overwriten
  85.      *                                with the new one
  86.      *
  87.      * @see FOFHalLinks::addLinks
  88.      *
  89.      * @return  boolean 
  90.      */
  91.     public function addLinks($relarray $links$overwrite true)
  92.     {
  93.         return $this->_links->addLinks($rel$links$overwrite);
  94.     }
  95.  
  96.     /**
  97.      * Add data to the document
  98.      *
  99.      * @param   stdClass  $data       The data to add
  100.      * @param   boolean   $overwrite  Should I overwrite existing data?
  101.      *
  102.      * @return  void 
  103.      */
  104.     public function addData($data$overwrite true)
  105.     {
  106.         if (is_array($data))
  107.         {
  108.             $data = (object) $data;
  109.         }
  110.  
  111.         if ($overwrite)
  112.         {
  113.             $this->_data $data;
  114.         }
  115.         else
  116.         {
  117.             if (!is_array($this->_data))
  118.             {
  119.                 $this->_data array($this->_data);
  120.             }
  121.  
  122.             $this->_data[$data;
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Add an embedded document
  128.      *
  129.      * @param   string          $rel        The relation of the embedded document to its container document
  130.      * @param   FOFHalDocument  $document   The document to add
  131.      * @param   boolean         $overwrite  Should I overwrite existing data with the same relation?
  132.      *
  133.      * @return  boolean 
  134.      */
  135.     public function addEmbedded($relFOFHalDocument $document$overwrite true)
  136.     {
  137.         if (!array_key_exists($rel$this->_embedded|| !$overwrite)
  138.         {
  139.             $this->_embedded[$rel$document;
  140.         }
  141.         elseif (array_key_exists($rel$this->_embedded&& !$overwrite)
  142.         {
  143.             if (!is_array($this->_embedded[$rel]))
  144.             {
  145.                 $this->_embedded[$relarray($this->_embedded[$rel]);
  146.             }
  147.  
  148.             $this->_embedded[$rel][$document;
  149.         }
  150.         else
  151.         {
  152.             return false;
  153.         }
  154.     }
  155.  
  156.     /**
  157.      * Returns the collection of links of this document
  158.      *
  159.      * @param   string  $rel  The relation of the links to fetch. Skip to get all links.
  160.      *
  161.      * @return  array 
  162.      */
  163.     public function getLinks($rel null)
  164.     {
  165.         return $this->_links->getLinks($rel);
  166.     }
  167.  
  168.     /**
  169.      * Returns the collection of embedded documents
  170.      *
  171.      * @param   string  $rel  Optional; the relation to return the embedded documents for
  172.      *
  173.      * @return  array|FOFHalDocument
  174.      */
  175.     public function getEmbedded($rel null)
  176.     {
  177.         if (empty($rel))
  178.         {
  179.             return $this->_embedded;
  180.         }
  181.         elseif (isset($this->_embedded[$rel]))
  182.         {
  183.             return $this->_embedded[$rel];
  184.         }
  185.         else
  186.         {
  187.             return array();
  188.         }
  189.     }
  190.  
  191.     /**
  192.      * Return the data attached to this document
  193.      *
  194.      * @return   array|stdClass
  195.      */
  196.     public function getData()
  197.     {
  198.         return $this->_data;
  199.     }
  200.  
  201.     /**
  202.      * Instantiate and call a suitable renderer class to render this document
  203.      * into the specified format.
  204.      *
  205.      * @param   string  $format  The format to render the document into, e.g. 'json'
  206.      *
  207.      * @return  string  The rendered document
  208.      *
  209.      * @throws  RuntimeException  If the format is unknown, i.e. there is no suitable renderer
  210.      */
  211.     public function render($format 'json')
  212.     {
  213.         $class_name 'FOFHalRender' ucfirst($format);
  214.  
  215.         if (!class_exists($class_nametrue))
  216.         {
  217.             throw new RuntimeException("Unsupported HAL Document format '$format'. Render aborted.");
  218.         }
  219.  
  220.         $renderer new $class_name($this);
  221.  
  222.         return $renderer->render(
  223.             array(
  224.                 'data_key'        => $this->_dataKey
  225.             )
  226.         );
  227.     }
  228. }

Documentation generated on Tue, 19 Nov 2013 15:01:37 +0100 by phpDocumentor 1.4.3