Skip to content
This repository
  • 4 commits
  • 8 files changed
  • 0 comments
  • 1 contributor
80 README.md
Source Rendered
... ... @@ -1,4 +1,82 @@
1 1 createphp
2 2 =========
3 3
4   -PHP adapter for CreateJS
  4 +This is a small standalone library designed to make it easier to integrate CreateJS
  5 +into existing PHP applications/frameworks.
  6 +
  7 +Usage
  8 +-----
  9 +
  10 +To use createphp, you need to implement the rdfMapper class, instantiate it with a
  11 +configuration for your data source and then you're good to go
  12 +
  13 +```php
  14 +$object = load_your_data_from_somewhere();
  15 +
  16 +$config = array
  17 +(
  18 + 'blog_article' => array(
  19 + 'storage' => 'some_db_table',
  20 + 'attributes' => array(
  21 + 'typeof' => 'sioc:Blog',
  22 + ),
  23 + 'vocabularies' => array(
  24 + 'dcterms' => 'http://purl.org/dc/terms/',
  25 + 'sioc' => 'http://rdfs.org/sioc/ns#'
  26 + ),
  27 + 'properties' => array(
  28 + 'title' => array
  29 + (
  30 + 'rdf_name' => 'dcterms:title',
  31 + ),
  32 + 'content' => array(
  33 + 'rdf_name' => 'sioc:content',
  34 + ),
  35 + ),
  36 + )
  37 +);
  38 +
  39 +$mapper = new my_mapper_class(new createphp\config($config));
  40 +$controller = new createphp\controller($mapper);
  41 +
  42 +echo $controller
  43 +```
  44 +
  45 +This will output something like
  46 +
  47 +```html
  48 +<div about="http://some_domain.com/some_unique_identifier"
  49 + xmlns:dcterms="http://purl.org/dc/terms/"
  50 + xmlns:sioc="http://rdfs.org/sioc/ns#"
  51 + typeof="sioc:Blog">
  52 +
  53 + <div property="dcterms:title">
  54 + Some title
  55 + </div>
  56 +
  57 + <div property="sioc:content">
  58 + Article content
  59 + </div>
  60 +</div>
  61 +```
  62 +
  63 +Of course, the markup is completely configurable, and you can also render the
  64 +individual fields separately. If you include the CreateJS files into your page,
  65 +all specified fields will become editable. To actually save the data, you will
  66 +have to provide an access point for the REST service, like so:
  67 +
  68 +```php
  69 +$config = new createphp\config(load_my_configuration_from_somewhere());
  70 +$mapper = new my_mapper_class($config);
  71 +
  72 +$received_data = json_decode(file_get_contents("php://input"), true);
  73 +$service = new createphp\restservice($mapper, $received_data);
  74 +
  75 +$service->run();
  76 +```
  77 +
  78 +Word of Warning
  79 +---------------
  80 +The code is still very much in development. While it's kept in a constantly running
  81 +state, please note that the API might still change considerably. Suggestions and
  82 +feedback are of course welcome!
144 collection.php
... ... @@ -0,0 +1,144 @@
  1 +<?php
  2 +/**
  3 + * Collection holder
  4 + *
  5 + * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  6 + * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  7 + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  8 + * @package openpsa.createphp
  9 + */
  10 +
  11 +namespace openpsa\createphp;
  12 +
  13 +/**
  14 + * @package openpsa.createphp
  15 + */
  16 +class collection extends node implements \ArrayAccess, \Iterator
  17 +{
  18 + protected $_position = 0;
  19 +
  20 + protected $_config;
  21 + protected $_controller;
  22 +
  23 + public function __construct(array $settings, controller $parent)
  24 + {
  25 + $this->_config = $settings;
  26 +
  27 + foreach ($settings['attributes'] as $key => $value)
  28 + {
  29 + $this->set_attribute($key, $value);
  30 + }
  31 +
  32 + $this->_controller = $parent;
  33 + $parent_mapper = $this->_controller->get_mapper();
  34 + $parent_object = $this->_controller->get_object();
  35 + $mapper_class = get_class($parent_mapper);
  36 + $config = $parent_mapper->get_config();
  37 + $config->set_schema($settings['type'][0]);
  38 +
  39 + $children = $parent_mapper->get_children($parent_object, $config);
  40 +
  41 + if ($parent_mapper->is_editable($parent_object))
  42 + {
  43 + $object = $parent_mapper->prepare_object($config, $parent_object);
  44 + array_unshift($children, $object);
  45 + }
  46 +
  47 + // create controllers for children
  48 + foreach ($children as $child)
  49 + {
  50 + $mapper = new $mapper_class($config);
  51 + $controller = new controller($mapper, $this->_parent);
  52 + $controller->set_object($child, $settings['type'][0]);
  53 + $this->_children[] = $controller;
  54 + }
  55 + }
  56 +
  57 + function rewind()
  58 + {
  59 + $this->_position = 0;
  60 + }
  61 +
  62 + function current()
  63 + {
  64 + return $this->_children[$this->_position];
  65 + }
  66 +
  67 + function key()
  68 + {
  69 + return $this->_position;
  70 + }
  71 +
  72 + function next()
  73 + {
  74 + ++$this->_position;
  75 + }
  76 +
  77 + function valid()
  78 + {
  79 + return isset($this->_children[$this->_position]);
  80 + }
  81 +
  82 + public function offsetSet($offset, $value)
  83 + {
  84 + if (is_null($offset))
  85 + {
  86 + $this->_children[] = $value;
  87 + }
  88 + else
  89 + {
  90 + $this->_children[$offset] = $value;
  91 + }
  92 + }
  93 +
  94 + public function offsetExists($offset)
  95 + {
  96 + return isset($this->_children[$offset]);
  97 + }
  98 +
  99 + public function offsetUnset($offset)
  100 + {
  101 + unset($this->_children[$offset]);
  102 + }
  103 +
  104 + public function offsetGet($offset)
  105 + {
  106 + return isset($this->_children[$offset]) ? $this->_children[$offset] : null;
  107 + }
  108 +
  109 + /**
  110 + * Renders the start tag
  111 + *
  112 + * @param string $tag_name
  113 + * @return string
  114 + */
  115 + public function render_start($tag_name = false)
  116 + {
  117 + $mapper = $this->_controller->get_mapper();
  118 + // render this for admin users only
  119 + if ($this->_controller->is_editable())
  120 + {
  121 + // add about
  122 + $this->set_attribute('about', $mapper->create_identifier($this->_controller->get_object()));
  123 + }
  124 +
  125 + // add xml namespaces
  126 + foreach ($mapper->get_vocabularies() as $prefix => $uri)
  127 + {
  128 + $this->set_attribute('xmlns:' . $prefix, $uri);
  129 + }
  130 +
  131 + return parent::render_start($tag_name);
  132 + }
  133 +
  134 + public function render_content()
  135 + {
  136 + $ret = '';
  137 + foreach ($this->_children as $child)
  138 + {
  139 + $ret .= $child->render();
  140 + }
  141 + return $ret;
  142 + }
  143 +}
  144 +?>
66 config.php
... ... @@ -0,0 +1,66 @@
  1 +<?php
  2 +/**
  3 + * Config wrapper
  4 + *
  5 + * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  6 + * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  7 + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  8 + * @package openpsa.createphp
  9 + */
  10 +
  11 +namespace openpsa\createphp;
  12 +
  13 +/**
  14 + * @package openpsa.createphp
  15 + */
  16 +class config
  17 +{
  18 + /**
  19 + * The current schema
  20 + *
  21 + * @var string
  22 + */
  23 + protected $_schema;
  24 +
  25 + protected $_defaults = array
  26 + (
  27 + 'vocabularies' => array(),
  28 + 'properties' => array(),
  29 + 'attributes' => array(),
  30 + 'storage' => false
  31 + );
  32 +
  33 + protected $_property_defaults = array
  34 + (
  35 + 'type' => array('openpsa\createphp\propertyNode')
  36 + );
  37 +
  38 + protected $_data = array();
  39 +
  40 + public function __construct(array $data = array())
  41 + {
  42 + foreach ($data as $schema_name => $config)
  43 + {
  44 + if (empty($this->_schema))
  45 + {
  46 + $this->_schema = $schema_name;
  47 + }
  48 + foreach ($config['properties'] as $fieldname => $values)
  49 + {
  50 + $config['properties'][$fieldname] = array_merge($this->_property_defaults, $config['properties'][$fieldname]);
  51 + }
  52 + $this->_data[$schema_name] = array_merge($this->_defaults, $config);
  53 + }
  54 + }
  55 +
  56 + public function set_schema($schema_name)
  57 + {
  58 + $this->_schema = $schema_name;
  59 + }
  60 +
  61 + public function get($key)
  62 + {
  63 + return $this->_data[$this->_schema][$key];
  64 + }
  65 +}
  66 +?>
180 controller.php
... ... @@ -0,0 +1,180 @@
  1 +<?php
  2 +/**
  3 + * Abstract baseclass for the CreateController
  4 + *
  5 + * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  6 + * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  7 + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  8 + * @package openpsa.createphp
  9 + */
  10 +
  11 +namespace openpsa\createphp;
  12 +
  13 +/**
  14 + * @package openpsa.createphp
  15 + */
  16 +class controller extends node
  17 +{
  18 + /**
  19 + * Flag that shows whether or not the object is editable
  20 + *
  21 + * @var boolean
  22 + */
  23 + private $_editable = true;
  24 +
  25 + /**
  26 + * The mapper
  27 + *
  28 + * @var rdfMapper
  29 + */
  30 + protected $_mapper;
  31 +
  32 + /**
  33 + * The current storage object, if any
  34 + *
  35 + * @var mixed
  36 + */
  37 + protected $_object;
  38 +
  39 + /**
  40 + * Stores an array of rdf properties currently set for the controller
  41 + *
  42 + * @var array
  43 + */
  44 + protected $_properties = array();
  45 +
  46 + /**
  47 + * The constructor
  48 + *
  49 + * @param rdfMapper $mapper
  50 + * @param controller $parent the parent controller for collection children
  51 + */
  52 + public function __construct(rdfMapper $mapper, controller $parent = null)
  53 + {
  54 + $this->_mapper = $mapper;
  55 + $this->_parent = $parent;
  56 + }
  57 +
  58 + public function set_object($object, $schema_name)
  59 + {
  60 + $this->_object = $object;
  61 + $this->_properties = array();
  62 + $this->_mapper->get_config()->set_schema($schema_name);
  63 +
  64 + foreach ($this->_mapper->get_config()->get('attributes') as $key => $value)
  65 + {
  66 + $this->set_attribute($key, $value);
  67 + }
  68 +
  69 + $map = $this->_mapper->get_config()->get('properties');
  70 +
  71 + // use rdf mapper to create element representations
  72 + foreach ($map as $fieldname => $config)
  73 + {
  74 + $classname = array_shift($config['type']);
  75 + $this->$fieldname = new $classname($config, $this);
  76 +
  77 + if ($this->$fieldname instanceof propertyNode)
  78 + {
  79 + if (empty($config['rdf_name']))
  80 + {
  81 + $rdf_name = $this->_mapper->get_rdf_name($fieldname);
  82 + }
  83 + else
  84 + {
  85 + $rdf_name = $config['rdf_name'];
  86 + }
  87 + $this->$fieldname->set_attribute('property', $rdf_name);
  88 + $this->$fieldname->set_value($this->_mapper->get_property_value($object, $fieldname));
  89 + }
  90 + }
  91 +
  92 + $this->set_editable($this->_mapper->is_editable($object));
  93 + }
  94 +
  95 + public function get_object()
  96 + {
  97 + return $this->_object;
  98 + }
  99 +
  100 + /**
  101 + * Magic getter
  102 + *
  103 + * @param string $key
  104 + * @return node
  105 + */
  106 + public function __get($key)
  107 + {
  108 + if (isset($this->_properties[$key]))
  109 + {
  110 + return $this->_properties[$key];
  111 + }
  112 + return null;
  113 + }
  114 +
  115 + /**
  116 + * Magic setter
  117 + *
  118 + * @param string $key
  119 + * @param node $node
  120 + */
  121 + public function __set($key, node $node)
  122 + {
  123 + $this->_properties[$key] = $node;
  124 + }
  125 +
  126 + /**
  127 + * Mapper getter
  128 + *
  129 + * @return rdfMapper
  130 + */
  131 + public function get_mapper()
  132 + {
  133 + return $this->_mapper;
  134 + }
  135 +
  136 + public function set_editable($value)
  137 + {
  138 + $this->_editable = (bool) $value;
  139 + }
  140 +
  141 + public function is_editable()
  142 + {
  143 + return $this->_editable;
  144 + }
  145 +
  146 + /**
  147 + * Renders the start tag
  148 + *
  149 + * @param string $tag_name
  150 + * @return string
  151 + */
  152 + public function render_start($tag_name = false)
  153 + {
  154 + // render this for admin users only
  155 + if ($this->is_editable())
  156 + {
  157 + // add about
  158 + $this->set_attribute('about', $this->_mapper->create_identifier($this->_object));
  159 + }
  160 +
  161 + // add xml namespaces
  162 + foreach ($this->_mapper->get_vocabularies() as $prefix => $uri)
  163 + {
  164 + $this->set_attribute('xmlns:' . $prefix, $uri);
  165 + }
  166 +
  167 + return parent::render_start($tag_name);
  168 + }
  169 +
  170 + public function render_content()
  171 + {
  172 + $output = '';
  173 + foreach ($this->_properties as $key => $prop)
  174 + {
  175 + $output .= $prop->render();
  176 + }
  177 + return $output;
  178 + }
  179 +}
  180 +?>
154 node.php
... ... @@ -0,0 +1,154 @@
  1 +<?php
  2 +/**
  3 + * Encapsulates a node in the DOM tree
  4 + *
  5 + * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  6 + * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  7 + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  8 + * @package openpsa.createphp
  9 + */
  10 +
  11 +namespace openpsa\createphp;
  12 +
  13 +/**
  14 + * @package openpsa.createphp
  15 + */
  16 +abstract class node
  17 +{
  18 + /**
  19 + * HTML element to use
  20 + *
  21 + * @var string
  22 + */
  23 + protected $_tag_name = 'div';
  24 +
  25 + /**
  26 + * The element's attributes
  27 + *
  28 + * @var array
  29 + */
  30 + protected $_attributes = array();
  31 +
  32 + /**
  33 + * The element output template
  34 + *
  35 + * @var string
  36 + */
  37 + private $_template = "<__TAG_NAME__ __ATTRIBUTES__>__CONTENT__</__TAG_NAME__>\n";
  38 +
  39 + /**
  40 + * The parent node
  41 + *
  42 + * @var node
  43 + */
  44 + protected $_parent;
  45 +
  46 + /**
  47 + * The node's children, if any
  48 + *
  49 + * @var array
  50 + */
  51 + protected $_children = array();
  52 +
  53 + /**
  54 + * Adds an additional attribute to the wrapper tag
  55 + *
  56 + * @param string $key
  57 + * @param string $value
  58 + */
  59 + public function set_attribute($key, $value)
  60 + {
  61 + $this->_attributes[$key] = $value;
  62 + }
  63 +
  64 + /**
  65 + * Adds an additional attribute to the wrapper tag
  66 + *
  67 + * @param string $key
  68 + */
  69 + public function get_attribute($key)
  70 + {
  71 + return $this->_attributes[$key];
  72 + }
  73 +
  74 + /**
  75 + * Sets the template
  76 + *
  77 + * @param string $template
  78 + */
  79 + public function set_template($template)
  80 + {
  81 + $this->_template = $template;
  82 + }
  83 +
  84 + /**
  85 + * Renders the element
  86 + *
  87 + * @param string $tag_name
  88 + */
  89 + public function render_start($tag_name = false)
  90 + {
  91 + if (is_string($tag_name))
  92 + {
  93 + $this->_tag_name = $tag_name;
  94 + }
  95 +
  96 + $template = explode('__CONTENT__', $this->_template);
  97 + $template = $template[0];
  98 +
  99 + $replace = array
  100 + (
  101 + "__TAG_NAME__" => $this->_tag_name,
  102 + "__ATTRIBUTES__" => $this->render_attributes(),
  103 + );
  104 +
  105 + return strtr($template, $replace);
  106 + }
  107 +
  108 + /**
  109 + * Renders everything including wrapper html tag and properties
  110 + *
  111 + * @param string $tag_name
  112 + * @return string
  113 + */
  114 + public function render($tag_name = false)
  115 + {
  116 + $output = $this->render_start($tag_name);
  117 +
  118 + $output .= $this->render_content();
  119 +
  120 + $output .= $this->render_end();
  121 + return $output;
  122 + }
  123 +
  124 + public function render_attributes()
  125 + {
  126 + // add additional attributes
  127 + $attributes = '';
  128 + foreach ($this->_attributes as $key => $value)
  129 + {
  130 + $attributes .= ' ' . $key . '="' . $value . '"';
  131 + }
  132 + return $attributes;
  133 + }
  134 +
  135 + abstract function render_content();
  136 +
  137 + public function render_end()
  138 + {
  139 + $template = explode('__CONTENT__', $this->_template);
  140 + $template = $template[1];
  141 +
  142 + $replace = array
  143 + (
  144 + "__TAG_NAME__" => $this->_tag_name,
  145 + );
  146 + return strtr($template, $replace);
  147 + }
  148 +
  149 + public function __toString()
  150 + {
  151 + return $this->render();
  152 + }
  153 +}
  154 +?>
70 propertyNode.php
... ... @@ -0,0 +1,70 @@
  1 +<?php
  2 +/**
  3 + * Encapsulates a property node in the DOM tree
  4 + *
  5 + * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  6 + * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  7 + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  8 + * @package openpsa.createphp
  9 + */
  10 +
  11 +namespace openpsa\createphp;
  12 +
  13 +/**
  14 + * @package openpsa.createphp
  15 + */
  16 +class propertyNode extends node
  17 +{
  18 + /**
  19 + * the element's content
  20 + *
  21 + * @var string
  22 + */
  23 + private $_value = '';
  24 +
  25 + protected $_config;
  26 + protected $_controller;
  27 +
  28 + public function __construct(array $config, controller $controller)
  29 + {
  30 + $this->_config = $config;
  31 + $this->_controller = $controller;
  32 + }
  33 +
  34 + /**
  35 + * Sets the value
  36 + *
  37 + * @param string $value
  38 + */
  39 + public function set_value($value)
  40 + {
  41 + $this->_value = $value;
  42 + }
  43 +
  44 + /**
  45 + * Value getter
  46 + *
  47 + * @return string
  48 + */
  49 + public function get_value()
  50 + {
  51 + return $this->_value;
  52 + }
  53 +
  54 + public function render_content()
  55 + {
  56 + return $this->get_value();
  57 + }
  58 +
  59 + public function render($tag_name = false)
  60 + {
  61 + // add rdf name for admin only
  62 + if (!$this->_controller->is_editable())
  63 + {
  64 + unset($this->_attributes['property']);
  65 + }
  66 +
  67 + return parent::render($tag_name);
  68 + }
  69 +}
  70 +?>
155 rdfMapper.php
... ... @@ -0,0 +1,155 @@
  1 +<?php
  2 +/**
  3 + * Abstract baseclass for rdfMapper
  4 + *
  5 + * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  6 + * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  7 + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  8 + * @package openpsa.createphp
  9 + */
  10 +
  11 +namespace openpsa\createphp;
  12 +
  13 +/**
  14 + * @package openpsa.createphp
  15 + */
  16 +abstract class rdfMapper
  17 +{
  18 + /**
  19 + * the config array containing the mappings
  20 + *
  21 + * @var config
  22 + */
  23 + protected $_config;
  24 +
  25 + /**
  26 + * The vocabularies used
  27 + *
  28 + * @var array
  29 + */
  30 + protected $_vocabularies = array();
  31 +
  32 + /**
  33 + * Constructor
  34 + *
  35 + * @param config $config
  36 + */
  37 + public function __construct(config $config = null)
  38 + {
  39 + if (null === $config)
  40 + {
  41 + $config = new config;
  42 + }
  43 + $this->set_config($config);
  44 + }
  45 +
  46 + /**
  47 + * Config getter
  48 + *
  49 + * @return array
  50 + */
  51 + public function get_config()
  52 + {
  53 + return $this->_config;
  54 + }
  55 +
  56 + /**
  57 + * Config setter
  58 + *
  59 + * @param config $config
  60 + */
  61 + public function set_config(config $config)
  62 + {
  63 + $this->_config = $config;
  64 + }
  65 +
  66 + /**
  67 + * Register new namespace
  68 + *
  69 + * @param string $prefix
  70 + * @param string $uri
  71 + */
  72 + public function register_vocabulary($prefix, $uri)
  73 + {
  74 + $this->_vocabularies[$prefix] = $uri;
  75 + }
  76 +
  77 + /**
  78 + * Get all namespaces
  79 + *
  80 + * @return array
  81 + */
  82 + public function get_vocabularies()
  83 + {
  84 + foreach ($this->_config->get('vocabularies') as $prefix => $uri)
  85 + {
  86 + $this->register_vocabulary($prefix, $uri);
  87 + }
  88 + return $this->_vocabularies;
  89 + }
  90 +
  91 + /**
  92 + * Set object property
  93 + *
  94 + * @param mixed $key
  95 + * @param mixed $object
  96 + * @param mixed $value
  97 + * @return mixed
  98 + */
  99 + abstract function set_property_value($object, $key, $value);
  100 +
  101 + /**
  102 + * Get object property
  103 + *
  104 + * @param mixed $key
  105 + * @param mixed $object
  106 + * @return mixed
  107 + */
  108 + abstract function get_property_value($object, $key);
  109 +
  110 + abstract function get_rdf_name($fieldname);
  111 +
  112 + abstract function is_editable($object);
  113 +
  114 + /**
  115 + * Get object's children
  116 + *
  117 + * @param mixed $object
  118 + * @param array $config
  119 + * @return array
  120 + */
  121 + abstract function get_children($object, config $config);
  122 +
  123 + abstract function prepare_object(config $config);
  124 +
  125 + /**
  126 + * Save object
  127 + *
  128 + * @param mixed $object
  129 + */
  130 + abstract function store($object);
  131 +
  132 + /**
  133 + * Load object by identifier
  134 + *
  135 + * @param string $identifier
  136 + * @return mixed The storage object or false if nothing is found
  137 + */
  138 + abstract function get_by_identifier($identifier);
  139 +
  140 + /**
  141 + * Create identifier for passed object
  142 + *
  143 + * @param mixed $object
  144 + * @return string
  145 + */
  146 + abstract function create_identifier($object);
  147 +
  148 + /**
  149 + * Delete an object
  150 + *
  151 + * @param mixed $object
  152 + */
  153 + abstract function delete($object);
  154 +}
  155 +?>
208 restservice.php
... ... @@ -0,0 +1,208 @@
  1 +<?php
  2 +/**
  3 + * REST service backend
  4 + *
  5 + * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  6 + * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
  7 + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  8 + * @package openpsa.createphp
  9 + */
  10 +
  11 +namespace openpsa\createphp;
  12 +
  13 +/**
  14 + * @package openpsa.createphp
  15 + */
  16 +class restservice
  17 +{
  18 + /**
  19 + * The mapper object
  20 + *
  21 + * @var rdfMapper
  22 + */
  23 + protected $_mapper;
  24 +
  25 + /**
  26 + * The operation verb
  27 + *
  28 + * @var string
  29 + */
  30 + protected $_verb;
  31 +
  32 + /**
  33 + * The passed data, if any
  34 + *
  35 + * @var array
  36 + */
  37 + protected $_data;
  38 +
  39 + /**
  40 + * The constructor
  41 + *
  42 + * @param rdfMapper $mapper
  43 + */
  44 + public function __construct(rdfMapper $mapper, array $data = null)
  45 + {
  46 + $this->_data = $data;
  47 + $this->_verb = strtolower($_SERVER['REQUEST_METHOD']);
  48 + if (null !== $mapper)
  49 + {
  50 + $this->set_mapper($mapper);
  51 + }
  52 + }
  53 +
  54 + public function get_data()
  55 + {
  56 + return $this->_data;
  57 + }
  58 +
  59 + /**
  60 + * Get transmitted properties
  61 + *
  62 + * @return array
  63 + */
  64 + private function _get_properties()
  65 + {
  66 + $return = array();
  67 +
  68 + foreach ($this->_data as $key => $value)
  69 + {
  70 + if (substr($key, 0, 1) === '@')
  71 + {
  72 + continue;
  73 + }
  74 + $key = trim($key, '<>');
  75 + $return[$key] = $value;
  76 + }
  77 + return $return;
  78 + }
  79 +
  80 + /**
  81 + * Mapper setter
  82 + *
  83 + * @param rdfMapper $mapper
  84 + */
  85 + public function set_mapper(rdfMapper $mapper)
  86 + {
  87 + $this->_mapper = $mapper;
  88 + }
  89 +
  90 + /**
  91 + * Mapper getter
  92 + *
  93 + * @return rdfMapper
  94 + */
  95 + public function get_mapper()
  96 + {
  97 + return $this->_mapper;
  98 + }
  99 +
  100 + /**
  101 + * Run the service
  102 + */
  103 + public function run()
  104 + {
  105 + switch ($this->_verb)
  106 + {
  107 + case 'get':
  108 + // do not handle get
  109 + break;
  110 + case 'delete':
  111 + $this->_handle_delete();
  112 + break;
  113 + case 'post':
  114 + $this->_handle_create();
  115 + break;
  116 + case 'put':
  117 + $this->_handle_update();
  118 + break;
  119 + }
  120 + }
  121 +
  122 + /**
  123 + * Handle post request
  124 + */
  125 + private function _handle_create()
  126 + {
  127 + $map = $this->_mapper->get_config()->get('properties');
  128 + $received_data = $this->_get_properties();
  129 +
  130 + $parent = null;
  131 + foreach ($map as $fieldname => $config)
  132 + {
  133 + if (!isset($config['attributes']['rev']))
  134 + {
  135 + continue;
  136 + }
  137 +
  138 + $parentfield = $this->_expand_property_name($config['attributes']['rev']);
  139 + if (!empty($received_data[$parentfield]))
  140 + {
  141 + $parent_identifier = trim($received_data[$parentfield][0], '<>');
  142 + $parent = $this->_mapper->get_by_identifier($parent_identifier);
  143 + $this->_mapper->get_config()->set_schema($config['type'][1]);
  144 + }
  145 + }
  146 +
  147 + $object = $this->_mapper->prepare_object($this->_mapper->get_config(), $parent);
  148 + return $this->_store_data($object);
  149 + }
  150 +
  151 + /**
  152 + * Handle put request
  153 + */
  154 + private function _handle_update()
  155 + {
  156 + $object = $this->_mapper->get_by_identifier(trim($this->_data['@subject'], '<>'));
  157 + return $this->_store_data($object);
  158 + }
  159 +
  160 + private function _store_data($object)
  161 + {
  162 + $new_values = $this->_get_properties();
  163 +
  164 + $properties = $this->_mapper->get_config()->get('properties');
  165 +
  166 + foreach ($properties as $fieldname => $config)
  167 + {
  168 + //TODO: Figure out a proper way to do this
  169 + if (!empty($config['attributes']['rel']))
  170 + {
  171 + continue;
  172 + }
  173 + if (empty($config['rdf_name']))
  174 + {
  175 + $rdf_name = $this->_mapper->get_rdf_name($fieldname);
  176 + }
  177 + else
  178 + {
  179 + $rdf_name = $config['rdf_name'];
  180 + }
  181 + $expanded_name = $this->_expand_property_name($rdf_name);
  182 +
  183 + if (array_key_exists($expanded_name, $new_values))
  184 + {
  185 + $object = $this->_mapper->set_property_value($object, $fieldname, $new_values[$expanded_name]);
  186 + }
  187 + }
  188 +
  189 + return $this->_mapper->store($object);
  190 + }
  191 +
  192 + private function _expand_property_name($name)
  193 + {
  194 + $name = explode(":", $name);
  195 + $vocabularies = $this->_mapper->get_vocabularies();
  196 + return $vocabularies[$name[0]] . $name[1];
  197 + }
  198 +
  199 + /**
  200 + * Handle delete request
  201 + */
  202 + private function _handle_delete()
  203 + {
  204 + $object = $this->_mapper->get_by_identifier($_REQUEST["uri"]);
  205 + return $this->_mapper->delete($object);
  206 + }
  207 +}
  208 +?>

No commit comments for this range

Something went wrong with that request. Please try again.