Changeset 3007

Show
Ignore:
Timestamp:
10/10/08 15:58:22 (3 months ago)
Author:
felix
Message:

Merged revisions 3001-3002,3005 via svnmerge from
http://svn.agavi.org/branches/1.0

........

r3001 | david | 2008-10-08 20:28:51 +0200 (Wed, 08 Oct 2008) | 1 line


Changed singular/plural handling in AgaviXmlConfigDomElement? convenience methods, closes #878

........

r3002 | david | 2008-10-08 21:58:55 +0200 (Wed, 08 Oct 2008) | 1 line


Added getAttributes() and getAttributesNS() and cleaned up a bit (comments), refs #519

........

r3005 | felix | 2008-10-10 17:50:38 +0200 (Fri, 10 Oct 2008) | 3 lines


merges r3004 from http://svn.agavi.org/branches/dominik-validation-argument-sources
fixes a minor typo in AgaviValidationManager::setError() and AgaviValidationManager::setError() that produced an error about a non declared variable and swallowed errors

........

Location:
branches/felix-testing-implementation
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • branches/felix-testing-implementation

    • Property svnmerge-integrated changed from /branches/1.0:1-2892,2894-2984,2987-2988,2990-2998 to /branches/1.0:1-2892,2894-2984,2987-2988,2990-3006
  • branches/felix-testing-implementation/CHANGELOG

    r2992 r3007  
    441.0.0 beta 4 (October ??, 2008) 
    55------------------------------- 
     6 
     7CHG: Change singular/plural handling in AgaviXmlConfigDomElement convenience methods (#878) (David) 
    68 
    79FIX: XInclude failure leads to corrupt exception error message (#877) (David) 
  • branches/felix-testing-implementation/src/config/AgaviConfigHandlersConfigHandler.class.php

    r2785 r3007  
    2323 * 
    2424 * @author     Dominik del Bondio <ddb@bitxtender.com> 
     25 * @author     Noah Fontes <noah.fontes@bitextender.com> 
     26 * @author     David Zülke <david.zuelke@bitextender.com> 
    2527 * @copyright  Authors 
    2628 * @copyright  The Agavi Project 
     
    4951   * @author     Dominik del Bondio <ddb@bitxtender.com> 
    5052   * @author     Noah Fontes <noah.fontes@bitextender.com> 
     53   * @author     David Zülke <david.zuelke@bitextender.com> 
    5154   * @since      0.11.0 
    5255   */ 
     
    6063     
    6164    foreach($document->getConfigurationElements() as $configuration) { 
    62       if(!$configuration->hasChildren('handlers')) { 
     65      if(!$configuration->has('handlers')) { 
    6366        continue; 
    6467      } 
    6568       
    6669      // let's do our fancy work 
    67       foreach($configuration->getChildren('handlers') as $handler) { 
     70      foreach($configuration->get('handlers') as $handler) { 
    6871        $pattern = $handler->getAttribute('pattern'); 
    6972         
     
    7679          AgaviXmlConfigParser::STAGE_COMPILATION => array(), 
    7780        ); 
    78         if($handler->hasChildren('transformations')) { 
    79           foreach($handler->getChildren('transformations') as $transformation) { 
     81        if($handler->has('transformations')) { 
     82          foreach($handler->get('transformations') as $transformation) { 
    8083            $path = AgaviToolkit::literalize($transformation->getValue()); 
    8184            $for = $transformation->getAttribute('for', AgaviXmlConfigParser::STAGE_SINGLE); 
     
    122125          ), 
    123126        ); 
    124         if($handler->hasChildren('validations')) { 
    125           foreach($handler->getChildren('validations') as $validation) { 
     127        if($handler->has('validations')) { 
     128          foreach($handler->get('validations') as $validation) { 
    126129            $path = AgaviToolkit::literalize($validation->getValue()); 
    127130            $type = null; 
     
    158161  } 
    159162   
    160   public function guessValidationType($path) 
     163  /** 
     164   * Convenience method to quickly guess the type of a validation file using its 
     165   * file extension. 
     166   * 
     167   * @param      string The path to the file. 
     168   * 
     169   * @return     string An AgaviXmlConfigParser::VALIDATION_TYPE_* const value. 
     170   * 
     171   * @throws     AgaviException If the type could not be determined. 
     172   * 
     173   * @author     David Zülke <david.zuelke@bitextender.com> 
     174   * @since      1.0.0 
     175   */ 
     176  protected function guessValidationType($path) 
    161177  { 
    162178    switch(pathinfo($path, PATHINFO_EXTENSION)) { 
  • branches/felix-testing-implementation/src/config/AgaviTestSuitesConfigHandler.class.php

    r2850 r3007  
    5858    // loop over <configuration> elements 
    5959    foreach($document->getConfigurationElements() as $configuration) { 
    60       foreach($configuration->getChildren('suites') as $current) { 
     60      foreach($configuration->get('suites') as $current) { 
    6161        $suite =  array('class' => $current->getAttribute('class', 'AgaviTestSuite')); 
    6262        $suite['testfiles'] = array(); 
    63         foreach($current->getChildren('testfiles') as $file) { 
     63        foreach($current->get('testfiles') as $file) { 
    6464          $suite['testfiles'][] = $file->textContent; 
    6565        } 
  • branches/felix-testing-implementation/src/config/util/dom/AgaviXmlConfigDomElement.class.php

    r2946 r3007  
    44{ 
    55  /** 
    6    * Overloaded method for accessing child nodes. Does the pluralizing etc, and 
    7    * provides convenient access through a potentially set default namespace. 
    8    * 
    9    * @param      string The child element name. 
    10    * 
    11    * @return     mixed A DOMNodeList or an AgaviXmlConfigDomElement. 
    12    * 
    13    * @author     David Zülke <dz@bitxtender.com> 
    14    * @since      1.0.0 
    15    */ 
    16   public function __get($name) { 
    17     // TODO: add {namespace}element support 
    18     // should look into the default ns, IMO. otherwise, you gotta use getChild() 
    19     // must use singular/plural handling 
    20   } 
    21    
    22   public function __isset($name) { 
    23     // TODO: add {namespace}element support 
    24     // should look into the default ns, IMO. otherwise, you gotta use hasChild() 
    25   } 
    26    
     6   * __toString() magic method, returns the element value. 
     7   * 
     8   * @see        AgaviXmlConfigDomElement::getValue() 
     9   * 
     10   * @return     string The element value. 
     11   * 
     12   * @author     David Zülke <david.zuelke@bitextender.com> 
     13   * @since      1.0.0 
     14   */ 
    2715  public function __toString() 
    2816  { 
     
    3018  } 
    3119   
     20  /** 
     21   * Returns the element name. 
     22   * 
     23   * @return     string The element name. 
     24   * 
     25   * @author     David Zülke <david.zuelke@bitextender.com> 
     26   * @since      1.0.0 
     27   */ 
    3228  public function getName() 
    3329  { 
     
    3733  } 
    3834   
     35  /** 
     36   * Returns the element value. 
     37   * 
     38   * @return     string The element value. 
     39   * 
     40   * @author     David Zülke <david.zuelke@bitextender.com> 
     41   * @since      1.0.0 
     42   */ 
    3943  public function getValue() 
    4044  { 
     
    5054   * @return     Iterator An iterator. 
    5155   * 
    52    * @author     David Zülke <dz@bitxtender.com> 
     56   * @author     David Zülke <david.zuelke@bitextender.com> 
    5357   * @since      1.0.0 
    5458   */ 
     
    6468  } 
    6569   
     70  /** 
     71   * Retrieve singular form of given element name. 
     72   * This does special splitting only of the last part of the name if the name 
     73   * of the element contains hyphens, underscores or dots. 
     74   * 
     75   * @param      string The element name to singularize. 
     76   * 
     77   * @return     string The singularized element name. 
     78   * 
     79   * @author     Noah Fontes <noah.fontes@bitextender.com> 
     80   * @since      1.0.0 
     81   */ 
    6682  protected function singularize($name) 
    6783  { 
     84    // TODO: shouldn't this be static? 
    6885    $names = preg_split('#([_\-\.])#', $name, -1, PREG_SPLIT_DELIM_CAPTURE); 
    6986    $names[count($names) - 1] = AgaviInflector::singularize(end($names)); 
     
    7289   
    7390  /** 
     91   * Convenience method to retrieve child elements of the given name. 
     92   * Accepts singular or plural forms of the name, and will detect and handle 
     93   * parent containers with plural names properly. 
     94   * 
     95   * @param      string The name of the element(s) to check for. 
     96   * @param      string The namespace URI. If null, the document default 
     97   *                    namespace will be used. If an empty string, no namespace 
     98   *                    will be used. 
     99   * 
     100   * @return     DOMNodeList A list of the child elements. 
     101   * 
     102   * @author     David Zülke <david.zuelke@bitextender.com> 
     103   * @since      1.0.0 
     104   */ 
     105  public function get($name, $namespaceUri = null) 
     106  { 
     107    return $this->getChildren($name, $namespaceUri, true); 
     108  } 
     109   
     110  /** 
     111   * Convenience method to check if there are child elements of the given name. 
     112   * Accepts singular or plural forms of the name, and will detect and handle 
     113   * parent containers with plural names properly. 
     114   * 
     115   * @param      string The name of the element(s) to check for. 
     116   * @param      string The namespace URI. If null, the document default 
     117   *                    namespace will be used. If an empty string, no namespace 
     118   *                    will be used. 
     119   * 
     120   * @return     bool True if one or more child elements with the given name 
     121   *                  exist, false otherwise. 
     122   * 
     123   * @author     David Zülke <david.zuelke@bitextender.com> 
     124   * @since      1.0.0 
     125   */ 
     126  public function has($name, $namespaceUri = null) 
     127  { 
     128    return $this->hasChildren($name, $namespaceUri, true); 
     129  } 
     130   
     131  /** 
    74132   * Count the number of child elements with a given name. 
    75133   * 
     
    78136   *                    namespace will be used. If an empty string, no namespace 
    79137   *                    will be used. 
     138   * @param      bool   Whether or not to apply automatic singular/plural 
     139   *                    handling that skips plural container elements. 
    80140   * 
    81141   * @return     int The number of child elements with the given name. 
    82142   * 
    83143   * @author     Noah Fontes <noah.fontes@bitextender.com> 
    84    * @since      1.0.0 
    85    */ 
    86   public function countChildren($name, $namespaceUri = null) 
    87   { 
    88     // check for child elements(!) using XPath 
    89     // if arg is true, then only check for elements from our default namespace 
     144   * @author     David Zülke <david.zuelke@bitextender.com> 
     145   * @since      1.0.0 
     146   */ 
     147  public function countChildren($name, $namespaceUri = null, $pluralMagic = false) 
     148  { 
     149    // if arg is null, then only check for elements from our default namespace 
     150    // if namespace uri is null, use default ns. if empty string, use no ns 
    90151    $namespaceUri = ($namespaceUri === null ? $this->ownerDocument->getDefaultNamespaceUri() : $namespaceUri); 
    91     $singularName = $this->singularize($name); 
    92      
    93     $xpath = $this->ownerDocument->getXpath(); 
    94     if($namespaceUri) { 
    95       return (int)$xpath->evaluate(sprintf('count(child::*[local-name() = "%2$s" and namespace-uri() = "%3$s"]) + count(child::*[local-name() = "%1$s" and namespace-uri() = "%3$s"]/*[local-name() = "%2$s" and namespace-uri() = "%3$s"])', $name, $singularName, $namespaceUri), $this); 
     152     
     153    // init our vars 
     154    $query = ''; 
     155    $singularName = null; 
     156     
     157    if($pluralMagic) { 
     158      // we always assume that we either get plural names, or the singular of the singular is not different from the singular :) 
     159      $singularName = $this->singularize($name); 
     160      if($namespaceUri) { 
     161        $query = 'count(child::*[local-name() = "%2$s" and namespace-uri() = "%3$s"]) + count(child::*[local-name() = "%1$s" and namespace-uri() = "%3$s"]/*[local-name() = "%2$s" and namespace-uri() = "%3$s"])'; 
     162      } else { 
     163        $query = 'count(%1$s/%2$s) + count(%2$s)'; 
     164      } 
    96165    } else { 
    97       return (int)$xpath->evaluate(sprintf('count(%2$s) + count(%1$s/%2$s)', $name, $singularName), $this); 
    98     } 
     166      if($namespaceUri) { 
     167        $query = 'count(child::*[local-name() = "%1$s" and namespace-uri() = "%3$s"])'; 
     168      } else { 
     169        $query = 'count(%1$s)'; 
     170      } 
     171    } 
     172     
     173    return (int)$this->ownerDocument->getXpath()->evaluate(sprintf($query, $name, $singularName, $namespaceUri), $this); 
    99174  } 
    100175   
     
    107182   *                    namespace will be used. If an empty string, no namespace 
    108183   *                    will be used. 
     184   * @param      bool   Whether or not to apply automatic singular/plural 
     185   *                    handling that skips plural container elements. 
    109186   * 
    110187   * @return     bool True if one or more child elements with the given name 
     
    112189   * 
    113190   * @author     Noah Fontes <noah.fontes@bitextender.com> 
    114    * @since      1.0.0 
    115    */ 
    116   public function hasChildren($name, $namespaceUri = null) 
    117   { 
    118     return $this->countChildren($name, $namespaceUri) !== 0; 
     191   * @author     David Zülke <david.zuelke@bitextender.com> 
     192   * @since      1.0.0 
     193   */ 
     194  public function hasChildren($name, $namespaceUri = null, $pluralMagic = false) 
     195  { 
     196    return $this->countChildren($name, $namespaceUri, $pluralMagic) !== 0; 
    119197  } 
    120198   
     
    126204   *                    namespace will be used. If an empty string, no namespace 
    127205   *                    will be used. 
     206   * @param      string Whether or not to apply pluralization magic in selects. 
     207   * @param      bool   Whether or not to apply automatic singular/plural 
     208   *                    handling that skips plural container elements. 
    128209   * 
    129210   * @return     DOMNodeList A list of the child elements. 
    130211   * 
    131212   * @author     Noah Fontes <noah.fontes@bitextender.com> 
    132    * @since      1.0.0 
    133    */ 
    134   public function getChildren($name, $namespaceUri = null) 
    135   { 
    136     // check for child elements(!) using XPath 
    137     // if arg is true, then only check for elements from our default namespace 
     213   * @author     David Zülke <david.zuelke@bitextender.com> 
     214   * @since      1.0.0 
     215   */ 
     216  public function getChildren($name, $namespaceUri = null, $pluralMagic = false) 
     217  { 
     218    // if arg is null, then only check for elements from our default namespace 
    138219    // if namespace uri is null, use default ns. if empty string, use no ns 
    139220    $namespaceUri = ($namespaceUri === null ? $this->ownerDocument->getDefaultNamespaceUri() : $namespaceUri); 
    140     $singularName = $this->singularize($name); 
    141      
    142     $xpath = $this->ownerDocument->getXpath(); 
    143     if($namespaceUri) { 
    144       return $xpath->query(sprintf('child::*[local-name() = "%2$s" and namespace-uri() = "%3$s"] | child::*[local-name() = "%1$s" and namespace-uri() = "%3$s"]/*[local-name() = "%2$s" and namespace-uri() = "%3$s"]', $name, $singularName, $namespaceUri), $this); 
     221     
     222    // init our vars 
     223    $query = ''; 
     224    $singularName = null; 
     225     
     226    if($pluralMagic) { 
     227      // we always assume that we either get plural names, or the singular of the singular is not different from the singular :) 
     228      $singularName = $this->singularize($name); 
     229      if($namespaceUri) { 
     230        $query = 'child::*[local-name() = "%2$s" and namespace-uri() = "%3$s"] | child::*[local-name() = "%1$s" and namespace-uri() = "%3$s"]/*[local-name() = "%2$s" and namespace-uri() = "%3$s"]'; 
     231      } else { 
     232        $query = '%1$s/%2$s | %2$s'; 
     233      } 
    145234    } else { 
    146       return $xpath->query(sprintf('%1$s/%2$s | %2$s', $name, $singularName), $this); 
    147     } 
     235      if($namespaceUri) { 
     236        $query = 'child::*[local-name() = "%1$s" and namespace-uri() = "%3$s"]'; 
     237      } else { 
     238        $query = '%1$s'; 
     239      } 
     240    } 
     241     
     242    return $this->ownerDocument->getXpath()->query(sprintf($query, $name, $singularName, $namespaceUri), $this); 
    148243  } 
    149244   
     
    166261  public function hasChild($name, $namespaceUri = null) 
    167262  { 
     263    return $this->getChild($name, $namespaceUri) !== null; 
     264  } 
     265   
     266  /** 
     267   * Return a single child element with a given name. 
     268   * Only returns anything if there is exactly one child of this name. 
     269   * 
     270   * @param      string The name of the element. 
     271   * @param      string The namespace URI. If null, the document default 
     272   *                    namespace will be used. If an empty string, no namespace 
     273   *                    will be used. 
     274   * 
     275   * @return     DOMElement The child element, or null if none exists. 
     276   * 
     277   * @author     Noah Fontes <noah.fontes@bitextender.com> 
     278   * @author     David Zülke <david.zuelke@bitextender.com> 
     279   * @since      1.0.0 
     280   */ 
     281  public function getChild($name, $namespaceUri = null) 
     282  { 
     283    // if arg is null, then only check for elements from our default namespace 
    168284    // if namespace uri is null, use default ns. if empty string, use no ns 
    169     return $this->countChildren($name, $namespaceUri) === 1; 
    170      
    171     // XXX: not necessary for single elements? 
    172     // remember singular/plural support 
    173   } 
    174    
    175   /** 
    176    * Return a single child element with a given name. 
    177    * 
    178    * @param      string The name of the element. 
    179    * @param      string The namespace URI. If null, the document default 
    180    *                    namespace will be used. If an empty string, no namespace 
    181    *                    will be used. 
    182    * 
    183    * @return     DOMElement The child element, or null if none xists. 
    184    * 
    185    * @author     Noah Fontes <noah.fontes@bitextender.com> 
    186    * @since      1.0.0 
    187    */ 
    188   public function getChild($name, $namespaceUri = null) 
    189   { 
    190     $list = $this->getChildren($name, $namespaceUri); 
    191      
    192     if($list->length > 0) { 
    193       return $list->item(0); 
    194     } 
    195     return null; 
     285    $namespaceUri = ($namespaceUri === null ? $this->ownerDocument->getDefaultNamespaceUri() : $namespaceUri); 
     286     
     287    if($namespaceUri) { 
     288      $query = 'self::node()[count(child::*[local-name() = "%1$s" and namespace-uri() = "%2$s"]) = 1]/*[local-name() = "%1$s" and namespace-uri() = "%2$s"]'; 
     289    } else { 
     290      $query = 'self::node()[count(child::%1$s) = 1]/%1$s'; 
     291    } 
     292     
     293    return $this->ownerDocument->getXpath()->query(sprintf($query, $name, $namespaceUri), $this)->item(0); 
    196294  } 
    197295   
     
    209307   * @see        DOMElement::getAttribute() 
    210308   * 
    211    * @author     David Zülke <dz@bitxtender.com> 
     309   * @author     David Zülke <david.zuelke@bitextender.com> 
    212310   * @since      1.0.0 
    213311   */ 
     
    239337   * @see        DOMElement::getAttributeNS() 
    240338   * 
    241    * @author     David Zülke <dz@bitxtender.com> 
     339   * @author     David Zülke <david.zuelke@bitextender.com> 
    242340   * @since      1.0.0 
    243341   */ 
     
    254352   
    255353  /** 
     354   * Retrieve all attributes of the element that are in no namespace. 
     355   * 
     356   * @return     array An associative array of attribute names and values. 
     357   * 
     358   * @author     David Zülke <david.zuelke@bitextender.com> 
     359   * @since      1.0.0 
     360   */ 
     361  public function getAttributes() 
     362  { 
     363    return $this->getAttributesNS(''); 
     364  } 
     365   
     366  /** 
     367   * Retrieve all attributes of the element that are in the given namespace. 
     368   * 
     369   * @return     array An associative array of attribute names and values. 
     370   * 
     371   * @author     David Zülke <david.zuelke@bitextender.com> 
     372   * @since      1.0.0 
     373   */ 
     374  public function getAttributesNS($namespaceUri) 
     375  { 
     376    $retval = array(); 
     377     
     378    foreach($this->ownerDocument->getXpath()->query(sprintf('@*[namespace-uri() = "%s"]', $namespaceUri), $this) as $attribute) { 
     379      $retval[$attribute->localName] = $attribute->nodeValue; 
     380    } 
     381     
     382    return $retval; 
     383  } 
     384   
     385  /** 
    256386   * Check whether or not the element has Agavi parameters as children. 
    257387   * 
     
    264394  { 
    265395    if($this->ownerDocument->isAgaviConfiguration()) { 
    266       return $this->hasChildren('parameters', AgaviXmlConfigParser::NAMESPACE_AGAVI_ENVELOPE_LATEST); 
     396      return $this->has('parameters', AgaviXmlConfigParser::NAMESPACE_AGAVI_ENVELOPE_LATEST); 
    267397    } 
    268398     
     
    281411   * 
    282412   * @author     Noah Fontes <noah.fontes@bitextender.com> 
     413   * @author     David Zülke <david.zuelke@bitextender.com> 
    283414   * @since      1.0.0 
    284415   */ 
     
    289420     
    290421    if($this->ownerDocument->isAgaviConfiguration()) { 
    291       $elements = $this->getChildren('parameters', AgaviXmlConfigParser::NAMESPACE_AGAVI_ENVELOPE_LATEST); 
     422      $elements = $this->get('parameters', AgaviXmlConfigParser::NAMESPACE_AGAVI_ENVELOPE_LATEST); 
    292423       
    293424      foreach($elements as $element) { 
  • branches/felix-testing-implementation/src/validator/AgaviValidationManager.class.php

    r2999 r3007  
    819819  public function setError($name, $message) 
    820820  { 
    821     $name = new AgaviValidationArgument($fieldname); 
     821    $name = new AgaviValidationArgument($name); 
    822822    $incident = new AgaviValidationIncident(null, AgaviValidator::ERROR); 
    823823    $incident->addError(new AgaviValidationError($message, null, array($name))); 
     
    842842    $incident = new AgaviValidationIncident(null, AgaviValidator::ERROR); 
    843843    foreach($errors as $name => $error) { 
    844       $name = new AgaviValidationArgument($fieldname); 
     844      $name = new AgaviValidationArgument($name); 
    845845      $incident->addError(new AgaviValidationError($error, null, array($name))); 
    846846    }