Beispiel #1
0
 /**
  * Pluralize the $singular word unless $count is one.  If $plural
  * form is not supplied, inflector will be used.
  *
  * @param  integer      $count      Count determines singular or plural
  * @param  string       $singular   Singular form
  * @param  string|null  $plural     Plural form (optional)
  */
 public function pluralize($count, $singular, $plural = null)
 {
     if ($count == '1') {
         $word = $singular;
     } else {
         if ($plural) {
             $word = $plural;
         } else {
             $word = Mad_Support_Inflector::pluralize($singular);
         }
     }
     return "{$count} {$word}";
 }
 /**
  * Construct association object
  *
  * @param   string  $assocName
  * @param   array   $options
  * @param   object  $model
  */
 public function __construct($assocName, $options, Mad_Model_Base $model)
 {
     $valid = array('className', 'conditions', 'order', 'foreignKey', 'primaryKey', 'associationForeignKey', 'associationPrimaryKey', 'joinTable', 'uniq', 'include', 'finderSql', 'deleteSql', 'insertSql');
     $this->_options = Mad_Support_Base::assertValidKeys($options, $valid);
     $this->_assocName = $assocName;
     $this->_model = $model;
     $this->_conn = $model->connection();
     // get inflections
     $toMethod = Mad_Support_Inflector::camelize($this->_assocName, 'lower');
     $toMethod = str_replace('/', '_', $toMethod);
     $singular = Mad_Support_Inflector::singularize($toMethod);
     $toClass = ucfirst($singular);
     $this->_methods = array($toMethod => 'getObjects', $toMethod . '=' => 'setObjects', $singular . 'Ids' => 'getObjectIds', $singular . 'Ids=' => 'setObjectIds', $singular . 'Count' => 'getObjectCount', 'add' . $toClass => 'addObject', Mad_Support_Inflector::pluralize('replace' . $toClass) => 'replaceObjects', Mad_Support_Inflector::pluralize('delete' . $toClass) => 'deleteObjects', Mad_Support_Inflector::pluralize('clear' . $toClass) => 'clearObjects', Mad_Support_Inflector::pluralize('find' . $toClass) => 'findObjects');
 }
Beispiel #3
0
 /**
  * Construct association object
  * 
  * @param   string  $assocName
  * @param   array   $options
  */
 public function __construct($assocName, $options, Mad_Model_Base $model)
 {
     $valid = array('className', 'foreignKey', 'associationForeignKey', 'primaryKey', 'associationPrimaryKey', 'include', 'select', 'conditions', 'order', 'finderSql', 'through', 'dependent' => 'nullify');
     $this->_options = Mad_Support_Base::assertValidKeys($options, $valid);
     $this->_assocName = $assocName;
     $this->_model = $model;
     $this->_conn = $model->connection();
     // throw fatal error if through option is invalid
     $this->_throughClass = Mad_Support_Inflector::classify($this->_options['through']);
     class_exists($this->_throughClass);
     // get inflections
     $toMethod = Mad_Support_Inflector::camelize($this->_assocName, 'lower');
     $toMethod = str_replace('/', '_', $toMethod);
     $singular = Mad_Support_Inflector::singularize($toMethod);
     $toClass = ucfirst($singular);
     $this->_methods = array($toMethod => 'getObjects', $singular . 'Ids' => 'getObjectIds', $singular . 'Count' => 'getObjectCount', 'add' . $toClass => 'addObject', Mad_Support_Inflector::pluralize('delete' . $toClass) => 'deleteObjects', Mad_Support_Inflector::pluralize('clear' . $toClass) => 'clearObjects', Mad_Support_Inflector::pluralize('find' . $toClass) => 'findObjects');
 }
Beispiel #4
0
 /** 
  * Proxy to parent Mad_Support_ArrayObject#toXml, except that 
  * we know the explicit model type. 
  */
 public function toXml($options = array())
 {
     if (!isset($options['root'])) {
         $options['root'] = Mad_Support_Inflector::pluralize(get_class($this->_model));
     }
     return parent::toXml($options);
 }
Beispiel #5
0
 /**
  * Convert array collection to XML
  * @param   array   $array
  * @param   array   $options
  */
 public function arrayToXml($array, $options = array())
 {
     $firstElt = current($array);
     $firstType = is_object($firstElt) ? get_class($firstElt) : gettype($firstElt);
     $sameTypes = true;
     foreach ($array as $element) {
         // either an array or object with toXml method
         if (!is_array($element) && !is_callable(array($element, 'toXml'))) {
             throw new Mad_Support_Exception("Not all elements respond to toXml");
         }
         if (get_class($element) != $firstType) {
             $sameTypes = false;
         }
     }
     if (!isset($options['root'])) {
         if ($sameTypes && count($array) > 0) {
             $options['root'] = Mad_Support_Inflector::pluralize($firstType);
         } else {
             $options['root'] = 'records';
         }
     }
     if (!isset($options['children'])) {
         $options['children'] = Mad_Support_Inflector::singularize($options['root']);
     }
     if (!isset($options['indent'])) {
         $options['indent'] = 2;
     }
     if (!isset($options['skipTypes'])) {
         $options['skipTypes'] = false;
     }
     if (empty($options['builder'])) {
         $options['builder'] = new Mad_Support_Builder(array('indent' => $options['indent']));
     }
     $root = $options['root'];
     unset($options['root']);
     $children = $options['children'];
     unset($options['children']);
     if (!array_key_exists('dasherize', $options) || !empty($options['dasherize'])) {
         $root = Mad_Support_Inflector::dasherize($root);
     }
     if (empty($options['skipInstruct'])) {
         $options['builder']->instruct();
     }
     $opts = array_merge($options, array('root' => $children));
     $builder = $options['builder'];
     $attrs = $options['skipTypes'] ? array() : array('type' => 'array');
     // no elements in array
     if (count($array) == 0) {
         $builder->tag($root, '', $attrs);
         // build xml from elements
     } else {
         $tag = $builder->startTag($root, '', $attrs);
         $opts['skipInstruct'] = true;
         foreach ($array as $element) {
             // associative array
             if (is_array($element) && !is_int(key($element))) {
                 $this->hashToXml($element, $opts);
                 // array
             } elseif (is_array($element)) {
                 $this->arrayToXml($element, $opts);
                 // object
             } else {
                 $element->toXml($opts);
             }
         }
         $tag->end();
     }
     return $builder->__toString();
 }
Beispiel #6
0
 /**                
  * Serialize the collection to XML.
  */
 public function toXml($options = array())
 {
     if (!isset($options['root'])) {
         $options['root'] = Mad_Support_Inflector::pluralize(get_class($this->_model));
     }
     $conversion = new Mad_Support_ArrayConversion();
     return $conversion->toXml($this, $options);
 }
Beispiel #7
0
 public function testPluralize()
 {
     $this->assertEquals('briefcases', Mad_Support_Inflector::pluralize('briefcase'));
     $this->assertEquals('categories', Mad_Support_Inflector::pluralize('category'));
 }