/** * @param \SimpleXMLElement $nodeData * @param EncoderNode $nodeProxy * @param $isSingle * @return mixed */ protected function decodeRawNode($nodeData, EncoderNode $nodeProxy, $isSingle) { $path = $nodeProxy->getNodeNameSingle(); if (!$isSingle) { $path = $nodeProxy->getNodeName() . '/' . $path; } $children = $nodeData->xpath($path); return $isSingle ? $children[0] : $children; }
function __construct($classPrepend = null, $nodeTypeName = null) { parent::__construct('buildings', 'building', $classPrepend !== null ? $classPrepend : '\\PE\\Samples\\Farm', $nodeTypeName); $type = $this->addVariable(new EncoderNodeVariable('type')); $typePostNodeGetter = $type->postNodeGetter(new PostNodeGetter('getBuildingType', array(NodeAccessor::VARIABLE_OBJECT))); $typePostNodeGetter->alwaysExecute(true); }
function __construct() { parent::__construct('accessorMethodActionTypeNodes', 'accessorMethodActionTypeNode', '\\PE\\Samples\\Specials'); $this->addVariable(new EncoderNodeVariable('special')); $special = $this->addVariable(new EncoderNodeVariable('node', false)); $special->postNodeSetter(new PostNodeSetter('addNodeToSpecial', array(NodeAccessor::VARIABLE_NAME))); $special->postNodeGetter(new PostNodeGetter('getNodeFromSpecial', array(NodeAccessor::VARIABLE_NAME))); }
function __construct($addVariables = true) { parent::__construct('requiredConstructorsVariables', 'requiredConstructorVariables', '\\PE\\Samples\\Specials'); if ($addVariables === true) { $this->addVariable(new EncoderNodeVariable('name')); $this->addVariable(new EncoderNodeVariable('variableCategory')); } }
protected function _objectFileName() { if ($this->overrideObjectFileName) { return null; } else { return parent::_objectFileName(); } }
/** * Setup our test * (runs before each test) * * @return void */ protected function setUp() { // make sure we delete all the added nodes before we create a new TestCase EncoderNode::clean(); // Create a new FQ app, // since we need one pretty much everywhere $this->_peApp = new Encoder(); }
function __construct($addAfterAttributes = true) { parent::__construct('addAfterDecodeParents', 'addAfterDecodeParent', '\\PE\\Samples\\Specials'); $this->addVariable(new EncoderNodeVariable('name')); $addAfterDecodeChildSetter = new NodeChildSetter('addChild'); $addAfterDecodeChildSetter->setAfterChildren(false); $addAfterDecodeChildSetter->setAfterAttributes($addAfterAttributes); $this->addChildNode(new EncoderNodeChild('addAfterDecodeChild', $addAfterDecodeChildSetter, new NodeChildGetter('getChildren'))); $this->addChildNode(new EncoderNodeChild('addAfterDecodeChildrenRequire', new NodeChildSetter('addChildRequires'), new NodeChildGetter('getChildrenRequires'))); }
function __construct() { parent::__construct('variableTypes', 'variableType', '\\PE\\Samples\\Specials'); $required = $this->addVariable(new EncoderNodeVariable('required')); $required->setType(EncoderNodeVariable::TYPE_STRING); $required->preNodeSetter(new PreNodeSetter('preNodeRequiredSetter', array(NodeAccessor::VARIABLE_NODE, NodeAccessor::VARIABLE_NAME, NodeAccessor::VARIABLE_VALUE, NodeAccessor::VARIABLE_PARENT))); $required->postNodeSetter(new PostNodeSetter('postNodeRequiredSetter', array(NodeAccessor::VARIABLE_NODE, NodeAccessor::VARIABLE_NAME, NodeAccessor::VARIABLE_VALUE, NodeAccessor::VARIABLE_OBJECT, NodeAccessor::VARIABLE_PARENT))); $required->preNodeGetter(new PreNodeGetter('preNodeRequiredGetter', array(NodeAccessor::VARIABLE_NODE, NodeAccessor::VARIABLE_NAME, NodeAccessor::VARIABLE_OBJECT, NodeAccessor::VARIABLE_PARENT))); $required->postNodeGetter(new PostNodeGetter('postNodeRequiredGetter', array(NodeAccessor::VARIABLE_NODE, NodeAccessor::VARIABLE_NAME, NodeAccessor::VARIABLE_VALUE, NodeAccessor::VARIABLE_OBJECT, NodeAccessor::VARIABLE_PARENT))); $optional = $this->addVariable(new EncoderNodeVariable('optional')); $optional->preNodeSetter(new PreNodeSetter('preNodeOptionalSetter', array(NodeAccessor::VARIABLE_NODE, NodeAccessor::VARIABLE_NAME, NodeAccessor::VARIABLE_VALUE, NodeAccessor::VARIABLE_PARENT))); $optional->postNodeSetter(new PostNodeSetter('postNodeOptionalSetter', array(NodeAccessor::VARIABLE_NODE, NodeAccessor::VARIABLE_NAME, NodeAccessor::VARIABLE_VALUE, NodeAccessor::VARIABLE_OBJECT, NodeAccessor::VARIABLE_PARENT))); $optional->preNodeGetter(new PreNodeGetter('preNodeOptionalGetter', array(NodeAccessor::VARIABLE_NODE, NodeAccessor::VARIABLE_NAME, NodeAccessor::VARIABLE_OBJECT, NodeAccessor::VARIABLE_PARENT))); $optional->postNodeGetter(new PostNodeGetter('postNodeOptionalGetter', array(NodeAccessor::VARIABLE_NODE, NodeAccessor::VARIABLE_NAME, NodeAccessor::VARIABLE_VALUE, NodeAccessor::VARIABLE_OBJECT, NodeAccessor::VARIABLE_PARENT))); }
function __construct() { parent::__construct('encoderNodeVariableApplyToSettersNode', 'encoderNodeVariableApplyToSetterNode', '\\PE\\Samples\\Specials'); $nodeSimple = $this->addVariable(new EncoderNodeVariable('nodeSimple')); $nodeSimple->postNodeSetter(new PostNodeSetter('nodeSimple', array(NodeAccessor::VARIABLE_NAME))); $nodeFull = $this->addVariable(new EncoderNodeVariable('nodeFull')); $nodeFull->postNodeSetter(new PostNodeSetter('nodeFull', array(NodeAccessor::VARIABLE_NAME, NodeAccessor::VARIABLE_VALUE, NodeAccessor::VARIABLE_OBJECT, NodeAccessor::VARIABLE_PARENT))); $nodeWithoutVariables = $this->addVariable(new EncoderNodeVariable('nodeWithoutVariables')); $nodeWithoutVariables->postNodeSetter(new PostNodeSetter('nodeWithoutVariables')); $nodeWithoutVariablesEmpty = $this->addVariable(new EncoderNodeVariable('nodeWithoutVariablesEmpty')); $nodeWithoutVariablesEmpty->postNodeSetter(new PostNodeSetter('nodeWithoutVariables', array())); $nodeWithoutVariablesNull = $this->addVariable(new EncoderNodeVariable('nodeWithoutVariablesNull')); $nodeWithoutVariablesNull->postNodeSetter(new PostNodeSetter('nodeWithoutVariables', array())); $nodeUnknownVariable = $this->addVariable(new EncoderNodeVariable('nodeUnknownVariable')); $nodeUnknownVariable->postNodeSetter(new PostNodeSetter('nodeSimple', array('unknown_variable'))); $this->addVariable(new EncoderNodeVariable('var')); }
function __construct() { parent::__construct('optionalVariables', 'optionalVariables', '\\PE\\Samples\\Specials'); $this->addVariable(new EncoderNodeVariable('name')); $this->addVariable(new EncoderNodeVariable('otherVariable')); }
function __construct() { parent::__construct('things', 'thing', '\\PE\\Samples\\General'); $this->addVariable(new EncoderNodeVariable('thingVar')); }
/** * Retrieve a node type based on its node type name * * @param string $nodeTypeName * @return EncoderNode|null Returns the EncoderNode object for the requested type. Null if if no type is found */ public function getType($nodeTypeName) { return EncoderNode::getNodeType($this->getNodeName(), $nodeTypeName); }
function __construct() { parent::__construct('thingsContainer', 'thingContainer', '\\PE\\Samples\\General'); $this->addChildNode(new EncoderNodeChild('things', new NodeChildSetter('addThing'), new NodeChildGetter('getThings'))); }
public function testVariableMethods() { $node = EncoderNode::addNode($this->node()); $nodeVariable = new EncoderNodeVariable('nodeVariable'); $node->addVariable($nodeVariable); $collection = $node->getVariableCollection(); $this->assertTrue($collection->variableExists('nodeVariable')); $this->assertFalse($collection->variableExists('unknown')); $this->assertEquals($nodeVariable, $node->getVariable('nodeVariable')); $this->assertNull($node->getVariable('unknown')); $this->assertEquals($nodeVariable, $collection->getVariableById('nodeVariable')); $this->assertNull($collection->getVariableById('unknown')); }
protected function _encode($object, EncoderNode $node, EncoderOptions $options, $parent = null, $nodeIterationIndex = null, $childObjectIterationIndex = null) { $variableCollection = $node->getVariableCollection(); $objectGetterVariables = $variableCollection->getObjectGetterVariables(); $optionNodeIndex = $node->getNodeName() . '[' . $childObjectIterationIndex . ']'; $attributesRaw = array(); $postNodeStaticOptions = array(NodeAccessor::VARIABLE_NODE => $node, NodeAccessor::VARIABLE_OBJECT => $object, NodeAccessor::VARIABLE_PARENT => $parent, NodeAccessor::VARIABLE_OPTIONS => $options, NodeAccessor::VARIABLE_NODE_ITERATION_INDEX => $nodeIterationIndex, NodeAccessor::VARIABLE_CHILD_OBJECT_ITERATION_INDEX => $childObjectIterationIndex); $preNodeGetterVariables = $variableCollection->getPreNodeGetterVariables(); foreach ($preNodeGetterVariables as $preNodeGetterVariable) { $variableId = $preNodeGetterVariable->getId(); $postNodeGetter = $preNodeGetterVariable->getPreNodeGetter(); $actionOptions = array_merge($postNodeStaticOptions, array(NodeAccessor::VARIABLE_NODE_DATA => $attributesRaw, NodeAccessor::VARIABLE_NAME => $variableId)); if ($newAttributeData = $postNodeGetter->apply($actionOptions)) { if (is_array($newAttributeData)) { $attributesRaw = $newAttributeData; } } } // get all the variables values from the object foreach ($objectGetterVariables as $objectGetterVariable) { $variableId = $objectGetterVariable->getId(); $objectGetter = $objectGetterVariable->getObjectGetter(); $attributesRaw[$variableId] = $objectGetter->apply($object); } $postNodeGetterVariables = $variableCollection->getPostNodeGetterVariables(); foreach ($postNodeGetterVariables as $postNodeGetterVariable) { $variableId = $postNodeGetterVariable->getId(); $hasVariable = array_key_exists($variableId, $attributesRaw); $postNodeGetter = $postNodeGetterVariable->getPostNodeGetter(); if ($hasVariable || $postNodeGetter->alwaysExecute()) { $actionOptions = array_merge($postNodeStaticOptions, array(NodeAccessor::VARIABLE_NODE_DATA => $attributesRaw, NodeAccessor::VARIABLE_NAME => $variableId, NodeAccessor::VARIABLE_VALUE => $hasVariable ? $attributesRaw[$variableId] : null)); if ($newAttributeData = $postNodeGetter->apply($actionOptions)) { if (is_array($newAttributeData)) { $attributesRaw = $newAttributeData; } } } } $optionNodeKey = $options->option('key', $node); $optionNodeValue = $options->option('value', $node); $optionEncodeAttributes = $options->option('attributes', $node); $optionEncodeChildren = $options->option('children', $node); // should we encode the node's children or not? $encodeChildren = true; // should we encode the node's attributes or not? $encodeAttributes = true; if (is_bool($optionEncodeAttributes)) { $encodeAttributes = $optionEncodeAttributes; } if (is_bool($optionEncodeChildren)) { $encodeChildren = $optionEncodeChildren; } if ($optionNodeValue !== null || $optionNodeKey !== null) { $encodeAttributes = false; } $nodesRaw = array(); $childrenProcessed = array(); if ($encodeChildren === true) { $children = $node->getChildren(); foreach ($children as $childNodeName => $child) { if (!EncoderNode::nodeExists($child->getChildNodeName())) { throw new EncoderException(sprintf('Cannot set the node name (%s) of a node child because it doesn\'t exist. Please add the requested node with "EncoderNode::addNode()". Current node name "%s" with class name "%s"', $child->getChildNodeName(), $node->getNodeName(), get_class($node))); } $childOptionPath = $optionNodeIndex . ':' . $childNodeName; $optionChildIteration = $options->option('iterate', $childOptionPath); $optionChildKey = $options->option('key', $childOptionPath); $isIterated = $optionChildIteration !== null; $childIteration = $optionChildIteration === null ? 1 : $optionChildIteration; $getChildObjectsMethod = $child->getGetter()->getMethod(); if (!method_exists($object, $getChildObjectsMethod)) { throw new EncoderException(sprintf('Getter method "%s" for node "%s" does not exist in class "%s"', $getChildObjectsMethod, $childNodeName, get_class($object))); } $childObjects = $object->{$getChildObjectsMethod}(); if (!$child->isArray()) { $childObjects = array($childObjects); } else { if (!is_array($childObjects)) { throw new EncoderException(sprintf('Children object for node "%s" must be an array. EncoderNodeChilds are returning an array by default. If this behavior is not desired, turn it off using "$childNode->isArray(false)" or set "isArray" as an options to the EncoderNodeChild instance', $childNodeName)); } } $rawChildrenInformationIteration = array(); $nodeIteration = 0; for ($i = 0; $i < $childIteration; $i++) { $childObjectIteration = 0; foreach ($childObjects as $childObject) { $childNodeType = EncoderNode::getNodeTypeByObject($childObject); if ($childNodeType === null) { throw new EncoderException(sprintf('Child node type for object "%s (child of "%s")" for node "%s" not found', get_class($childObject), $node->getNodeName(), $childNodeName)); } $childNodeData = $this->_encode($childObject, $childNodeType, $options, $object, $nodeIteration, $childObjectIteration); $rawChildNode = $childNodeData['raw']; $rawChildNodeAttributes = $rawChildNode['attributes']; $rawChildNodeChildren = $rawChildNode['children']; $processedChildNode = $childNodeData['processed']; $rawChildrenInformationIteration[$nodeIteration][$childObjectIteration] = array('attributes' => $rawChildNodeAttributes, 'children' => $rawChildNodeChildren, 'nodeName' => $childNodeName); if ($optionChildKey !== null) { if ($isIterated) { $childrenProcessed[$nodeIteration][$rawChildNodeAttributes[$optionChildKey]] = $processedChildNode; } else { $childrenProcessed[$rawChildNodeAttributes[$optionChildKey]] = $processedChildNode; } } else { if (!isset($childrenProcessed[$childNodeName])) { $childrenProcessed[$childNodeName] = array(); } $childrenProcessedTemp = array($childObjectIteration => $processedChildNode); if ($isIterated) { $childrenProcessed[$childNodeName] = array_merge_recursive($childrenProcessed[$childNodeName], $this->encodeNodeChildren($childNodeType, $childNodeName, $child, array($nodeIteration => $childrenProcessedTemp))); } else { $childrenProcessed[$childNodeName] = array_merge_recursive($childrenProcessed[$childNodeName], $this->encodeNodeChildren($childNodeType, $childNodeName, $child, $childrenProcessedTemp)); } } $childObjectIteration++; } $nodeIteration++; } if (count($rawChildrenInformationIteration)) { $rawIteratedChildren = $isIterated ? $rawChildrenInformationIteration : $rawChildrenInformationIteration[0]; $nodesRaw[$childNodeName] = $rawIteratedChildren; } } } $attributesProcessed = $this->encodeAttributes($attributesRaw); if (!$encodeAttributes) { $attributesProcessed = array(); } $nodeData = array_merge($attributesProcessed, $childrenProcessed); if ($optionNodeValue !== null) { if (!array_key_exists($optionNodeValue, $attributesRaw)) { throw new EncoderException(sprintf('Option "value" cannot be mapped to "%s" because it does not exist in "%s"', $optionNodeValue, $node->getNodeName())); } $nodeData = $attributesRaw[$optionNodeValue]; } return array('processed' => $nodeData, 'raw' => array('attributes' => $attributesRaw, 'children' => $nodesRaw, 'nodeName' => $node->getNodeName())); }
public function addAddAfterDecodeNodes($addAfterAttributes = true) { EncoderNode::addNode(new AddAfterDecodeParentNode($addAfterAttributes)); EncoderNode::addNode(new AddAfterDecodeChildNode()); EncoderNode::addNode(new AddAfterDecodeChildRequiresNode()); }
function __construct($classPrepend = null, $nodeTypeName = null) { parent::__construct('animals', 'animal', $classPrepend !== null ? $classPrepend : '\\PE\\Samples\\Farm', $nodeTypeName); $this->addVariable(new EncoderNodeVariable('type')); $this->addVariable(new EncoderNodeVariable('name')); }
function __construct() { parent::__construct('singleChildren', 'singleChild', '\\PE\\Samples\\Specials'); $thing = $this->addChildNode(new EncoderNodeChild('thing', new NodeChildSetter('setThing'), new NodeChildGetter('getThing'))); $thing->isArray(false); }
function __construct() { parent::__construct('addAfterDecodeChildrenRequire', 'addAfterDecodeChildRequire', '\\PE\\Samples\\Specials'); $name = $this->addVariable(new EncoderNodeVariable('name')); $name->postNodeSetter(new PostNodeSetter('nodeSetName', array(NodeAccessor::VARIABLE_VALUE, NodeAccessor::VARIABLE_PARENT))); }
function __construct() { parent::__construct('nonArrayGetterMethods', 'nonArrayGetterMethod', '\\PE\\Samples\\Erroneous'); $this->addChildNode(new EncoderNodeChild('things', new NodeChildSetter('addThing'), new NodeChildGetter('getThings'))); }
function __construct() { parent::__construct('farms', 'farm', '\\PE\\Samples\\Farm'); $this->addChildNode(new EncoderNodeChild('buildings', new NodeChildSetter('addBuilding'), new NodeChildGetter('getBuildings'))); }
function __construct() { parent::__construct('nonArrayGetterMethodsOnPurpose', 'nonArrayGetterMethodOnPurpose', '\\PE\\Samples\\Specials'); $things = $this->addChildNode(new EncoderNodeChild('things', new NodeChildSetter('addThing'), new NodeChildGetter('getThing'))); $things->isArray(false); }
function __construct() { parent::__construct('noGetterMethods', 'noGetterMethod', '\\PE\\Samples\\Erroneous'); $this->addVariable(new EncoderNodeVariable('nonExistent')); }
function __construct() { parent::__construct('hello-worlds', 'hello-world', null); $this->addVariable(new EncoderNodeVariable('foo')); }
function __construct($nodeTypeName = null) { parent::__construct('hasDefaultTypes', 'hasDefaultType', '\\PE\\Samples\\Specials', $nodeTypeName); }
function __construct($setupLoader) { parent::__construct('classLoaders', 'classLoader', '\\PE\\Samples\\Loader'); $this->setupLoader = $setupLoader; }
function __construct() { parent::__construct('addAfterDecodeChildren', 'addAfterDecodeChild', '\\PE\\Samples\\Specials'); $this->addVariable(new EncoderNodeVariable('name')); }
function __construct() { parent::__construct('requiredVariables', 'requiredVariable', '\\PE\\Samples\\Specials'); $thing = $this->addVariable(new EncoderNodeVariable('required')); $thing->getObjectSetter()->required(true); }
public function processOptionsFromNode(EncoderNode $node) { return array_merge($this->_processOptions($node->getNodeNameSingle()), $this->_processOptions($node->getNodeName())); }