1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8class Search_Action_Factory 9{ 10 private $actions = []; 11 12 function register(array $actions) 13 { 14 $this->actions = array_merge($this->actions, $actions); 15 } 16 17 function fromMatch($match) 18 { 19 $parser = new WikiParser_PluginArgumentParser; 20 $arrayBuilder = new Search_Formatter_ArrayBuilder; 21 $arguments = $parser->parse($match->getArguments()); 22 23 if (! empty($arguments['name'])) { 24 $sequence = $this->build($arguments['name'], $arrayBuilder->getData($match->getBody())); 25 26 if (isset($arguments['group'])) { 27 $sequence->setRequiredGroup($arguments['group']); 28 } 29 30 return $sequence; 31 } 32 } 33 34 function build($name, array $data) 35 { 36 $sequence = new Search_Action_Sequence($name); 37 38 if (! isset($data['step'])) { 39 $data['step'] = []; 40 } 41 42 if (isset($data['step']['action'])) { 43 $data['step'] = [$data['step']]; 44 } 45 46 foreach ($data['step'] as $definition) { 47 $sequence->addStep($this->buildStep($definition)); 48 } 49 50 return $sequence; 51 } 52 53 private function buildStep($definition) 54 { 55 if (empty($definition['action'])) { 56 return new Search_Action_UnknownStep; 57 } 58 59 $action = trim($definition['action']); 60 61 if (! isset($this->actions[$action])) { 62 return new Search_Action_UnknownStep($action); 63 } 64 65 $actionClass = $this->actions[$action]; 66 unset($definition['action']); 67 68 if (! class_exists($actionClass)) { 69 return new Search_Action_UnknownStep($action); 70 } 71 72 return new Search_Action_ActionStep(new $actionClass, $definition); 73 } 74} 75