/**
  * 
  */
 public function setElement(MString $element)
 {
     if ($element->equals(S("p"))) {
         parent::setElement($element);
     } else {
         throw new MException(Sf("Invalid element '%s', must be paragraph 'p'", $element));
     }
 }
 /**
  * @return MApplicationNamespace
  */
 public function __construct(MString $name)
 {
     if (!$name->isEmpty()) {
         parent::__construct($name);
     } else {
         throw new MInvalidOperationException(S("Cannot instantiate a namespace with an empty name"));
     }
 }
 /**
  * 
  * @param MString $dictString
  * 
  * @return MDictionary
  */
 public static function parseString(MString $dictString)
 {
     $arr = $dictString->componentsSeparatedByString(S("|"));
     $dict = new MMutableDictionary();
     foreach ($arr->toArray() as $dictKeyPair) {
         $dictKeyPairArr = $dictKeyPair->componentsSeparatedByString(S(":"));
         $key = $dictKeyPairArr->objectAtIndex(0)->urlDecodedString();
         $value = $dictKeyPairArr->objectAtIndex(1)->urlDecodedString();
         $dict->setObjectForKey($key, $value);
     }
     return $dict;
 }
Example #4
0
 public static function genericordering($sql, $chop = '30')
 {
     $db = MFactory::getDbo();
     $options = array();
     $db->setQuery($sql);
     $items = $db->loadObjectList();
     // Check for a database error.
     if ($db->getErrorNum()) {
         MError::raiseNotice(500, $db->getErrorMsg());
         return false;
     }
     if (empty($items)) {
         $options[] = MHtml::_('select.option', 1, MText::_('MOPTION_ORDER_FIRST'));
         return $options;
     }
     $options[] = MHtml::_('select.option', 0, '0 ' . MText::_('MOPTION_ORDER_FIRST'));
     for ($i = 0, $n = count($items); $i < $n; $i++) {
         $items[$i]->text = MText::_($items[$i]->text);
         if (MString::strlen($items[$i]->text) > $chop) {
             $text = MString::substr($items[$i]->text, 0, $chop) . "...";
         } else {
             $text = $items[$i]->text;
         }
         $options[] = MHtml::_('select.option', $items[$i]->value, $items[$i]->value . '. ' . $text);
     }
     $options[] = MHtml::_('select.option', $items[$i - 1]->value + 1, $items[$i - 1]->value + 1 . ' ' . MText::_('MOPTION_ORDER_LAST'));
     return $options;
 }
Example #5
0
 function _buildViewWhere()
 {
     $db = MFactory::getDBO();
     $search = $this->mainframe->getUserStateFromRequest($this->option . '.queries.search', 'search', '', 'string');
     $search = MString::strtolower($search);
     $where = array();
     if ($search) {
         $where[] = 'LOWER(title) LIKE ' . $db->Quote('%' . $db->getEscaped($search, true) . '%', false);
     }
     $where = count($where) ? ' WHERE ' . implode(' AND ', $where) : '';
     return $where;
 }
Example #6
0
 public static function abridge($text, $length = 50, $intro = 30)
 {
     // Abridge the item text if it is too long.
     if (MString::strlen($text) > $length) {
         // Determine the remaining text length.
         $remainder = $length - ($intro + 3);
         // Extract the beginning and ending text sections.
         $beg = MString::substr($text, 0, $intro);
         $end = MString::substr($text, MString::strlen($text) - $remainder);
         // Build the resulting string.
         $text = $beg . '...' . $end;
     }
     return $text;
 }
Example #7
0
 public function __construct($form = null)
 {
     // If there is a form passed into the constructor set the form and form control properties.
     if ($form instanceof MForm) {
         $this->form = $form;
         $this->formControl = $form->getFormControl();
     }
     // Detect the field type if not set
     if (!isset($this->type)) {
         $parts = MString::splitCamelCase(get_class($this));
         if ($parts[0] == 'M') {
             $this->type = MString::ucfirst($parts[count($parts) - 1], '_');
         } else {
             $this->type = MString::ucfirst($parts[0], '_') . MString::ucfirst($parts[count($parts) - 1], '_');
         }
     }
 }
Example #8
0
 public static function stringURLUnicodeSlug($string)
 {
     // Replace double byte whitespaces by single byte (East Asian languages)
     $str = preg_replace('/\\xE3\\x80\\x80/', ' ', $string);
     // Remove any '-' from the string as they will be used as concatenator.
     // Would be great to let the spaces in but only Firefox is friendly with this
     $str = str_replace('-', ' ', $str);
     // Replace forbidden characters by whitespaces
     $str = preg_replace('#[:\\#\\*"@+=;!><&\\.%()\\]\\/\'\\\\|\\[]#', " ", $str);
     // Delete all '?'
     $str = str_replace('?', '', $str);
     // Trim white spaces at beginning and end of alias and make lowercase
     $str = trim(MString::strtolower($str));
     // Remove any duplicate whitespace and replace whitespaces by hyphens
     $str = preg_replace('#\\x20+#', '-', $str);
     return $str;
 }
Example #9
0
 public function test(&$element, $value, $group = null, &$input = null, &$form = null)
 {
     // If the field is empty and not required, the field is valid.
     $required = (string) $element['required'] == 'true' || (string) $element['required'] == 'required';
     if (!$required && empty($value)) {
         return true;
     }
     $urlParts = MString::parse_url($value);
     // See http://www.w3.org/Addressing/URL/url-spec.txt
     // Use the full list or optionally specify a list of permitted schemes.
     if ($element['schemes'] == '') {
         $scheme = array('http', 'https', 'ftp', 'ftps', 'gopher', 'mailto', 'news', 'prospero', 'telnet', 'rlogin', 'tn3270', 'wais', 'url', 'mid', 'cid', 'nntp', 'tel', 'urn', 'ldap', 'file', 'fax', 'modem', 'git');
     } else {
         $scheme = explode(',', $element['schemes']);
     }
     // This rule is only for full URLs with schemes because  parse_url does not parse
     // accurately without a scheme.
     // @see http://php.net/manual/en/function.parse-url.php
     if (!array_key_exists('scheme', $urlParts)) {
         return false;
     }
     $urlScheme = (string) $urlParts['scheme'];
     $urlScheme = strtolower($urlScheme);
     if (in_array($urlScheme, $scheme) == false) {
         return false;
     }
     // For some schemes here must be two slashes.
     if (($urlScheme == 'http' || $urlScheme == 'https' || $urlScheme == 'ftp' || $urlScheme == 'sftp' || $urlScheme == 'gopher' || $urlScheme == 'wais' || $urlScheme == 'gopher' || $urlScheme == 'prospero' || $urlScheme == 'telnet' || $urlScheme == 'git') && substr($value, strlen($urlScheme), 3) !== '://') {
         return false;
     }
     // The best we can do for the rest is make sure that the strings are valid UTF-8
     // and the port is an integer.
     if (array_key_exists('host', $urlParts) && !MString::valid((string) $urlParts['host'])) {
         return false;
     }
     if (array_key_exists('port', $urlParts) && !is_int((int) $urlParts['port'])) {
         return false;
     }
     if (array_key_exists('path', $urlParts) && !MString::valid((string) $urlParts['path'])) {
         return false;
     }
     return true;
 }
Example #10
0
 protected static function loadClass($entity, $type)
 {
     if (strpos($type, '.')) {
         list($prefix, $type) = explode('.', $type);
     } else {
         $prefix = 'M';
     }
     $class = MString::ucfirst($prefix, '_') . 'Form' . MString::ucfirst($entity, '_') . MString::ucfirst($type, '_');
     if (class_exists($class)) {
         return $class;
     }
     // Get the field search path array.
     $paths = MFormHelper::addPath($entity);
     // If the type is complex, add the base type to the paths.
     if ($pos = strpos($type, '_')) {
         // Add the complex type prefix to the paths.
         for ($i = 0, $n = count($paths); $i < $n; $i++) {
             // Derive the new path.
             $path = $paths[$i] . '/' . strtolower(substr($type, 0, $pos));
             // If the path does not exist, add it.
             if (!in_array($path, $paths)) {
                 $paths[] = $path;
             }
         }
         // Break off the end of the complex type.
         $type = substr($type, $pos + 1);
     }
     // Try to find the class file.
     $type = strtolower($type) . '.php';
     foreach ($paths as $path) {
         if ($file = MPath::find($path, $type)) {
             require_once $file;
             if (class_exists($class)) {
                 break;
             }
         }
     }
     // Check for all if the class exists.
     return class_exists($class) ? $class : false;
 }
Example #11
0
 protected function generateNewTitle($category_id, $alias, $title)
 {
     // Alter the title & alias
     $table = $this->getTable();
     while ($table->load(array('alias' => $alias, 'catid' => $category_id))) {
         $title = MString::increment($title);
         $alias = MString::increment($alias, 'dash');
     }
     return array($title, $alias);
 }
 /**
  * 
  *
  * @return MString
  */
 public function toString()
 {
     if ($this->element()) {
         $markup = new MMutableString();
         $indentString = MString::stringWithRepeatingString(S(" "), $this->indentLevel());
         $properties = new MMutableArray();
         foreach ($this->properties()->allKeys()->toArray() as $name) {
             $value = $this->properties()->objectForKey($name);
             $properties->addObject(Sf("%s=\"%s\"", $name, $value));
         }
         if ($properties->count() > 0) {
             $markup->appendFormat("%s<%s %s", $indentString, $this->element(), $properties->componentsJoinedByString(S(" ")));
         } else {
             $markup->appendFormat("%s<%s", $indentString, $this->element());
         }
         if ($this->text()) {
             $markup->appendFormat(">%s</%s>", $this->text()->stringByEncodingHTMLEntities(), $this->element());
         } else {
             if ($this->subviews()->count() > 0) {
                 $markup->appendLine(S(">"));
                 $markup->appendLine(parent::toString());
                 $markup->appendString(Sf("%s</%s>", $indentString, $this->element()));
             } else {
                 $markup->appendString(S("/>"));
             }
         }
         if ($this->shouldAppendEmptyLine()) {
             $markup->appendLine();
         }
         return $markup;
     } else {
         return parent::toString();
     }
 }
Example #13
0
/**
 * Creates a new MString using the specified format
 *
 * This function works the same way as PHP's sprintf
 * function but instead of outputting a string, it
 * outputs a Mango String (MString) object. This is a
 * convenience method, it has the same effect as using
 * MString::stringWithFormat();
 *
 * @see MString
 * @see MString::stringWithFormat()
 *
 * @param string $format The format string to use
 * @param string[] $args The arguments to use inside the
 * formatted string
 *
 * @return MString Returns the formatted String
 */
function Sf()
{
    $args = func_get_args();
    return MString::_stringWithFormat($args);
}
 /**
  * 
  *
  * @return void
  */
 public function writeLine(MString $line)
 {
     $string = new MMutableString($line);
     if (!$line->endsWith(S("\n"))) {
         $string->appendString(S("\n"));
     }
     $this->writeString($string);
 }
Example #15
0
 public static function setWorkarounds($data, $options = array())
 {
     $loptions = array();
     $loptions['nopathway'] = 0;
     $loptions['nohead'] = 0;
     $loptions['nomodules'] = 0;
     $loptions['modulemode'] = 0;
     if (isset($options['nopathway'])) {
         $loptions['nopathway'] = $options['nopathway'];
     }
     if (isset($options['nohead'])) {
         $loptions['nohead'] = $options['nohead'];
     }
     if (isset($options['nomodules'])) {
         $loptions['nomodules'] = $options['nomodules'];
     }
     if (isset($options['modulemode'])) {
         $loptions['modulemode'] = $options['modulemode'];
     }
     $app = MFactory::getApplication();
     $document = MFactory::getDocument();
     $buffer1 = $document->getBuffer();
     if (!is_array($buffer1)) {
         $buffer1 = array();
     }
     if (!isset($buffer1['module']) || !is_array($buffer1['module'])) {
         $buffer1['module'] = array();
     }
     $cached['body'] = $data;
     if ($loptions['nohead'] != 1 && method_exists($document, 'getHeadData')) {
         if ($loptions['modulemode'] == 1) {
             $headnow = $document->getHeadData();
             $unset = array('title', 'description', 'link', 'links', 'metaTags');
             foreach ($unset as $un) {
                 unset($headnow[$un]);
                 unset($options['headerbefore'][$un]);
             }
             $cached['head'] = array();
             foreach ($headnow as $now => $value) {
                 if (isset($options['headerbefore'][$now])) {
                     $nowvalue = array_map('serialize', $headnow[$now]);
                     $beforevalue = array_map('serialize', $options['headerbefore'][$now]);
                     $newvalue = array_diff_assoc($nowvalue, $beforevalue);
                     $newvalue = array_map('unserialize', $newvalue);
                     if (($now == 'script' || $now == 'style') && is_array($newvalue) && is_array($options['headerbefore'][$now])) {
                         foreach ($newvalue as $type => $currentScriptStr) {
                             if (isset($options['headerbefore'][$now][strtolower($type)])) {
                                 $oldScriptStr = $options['headerbefore'][$now][strtolower($type)];
                                 if ($oldScriptStr != $currentScriptStr) {
                                     $newvalue[strtolower($type)] = MString::substr($currentScriptStr, MString::strlen($oldScriptStr));
                                 }
                             }
                         }
                     }
                 } else {
                     $newvalue = $headnow[$now];
                 }
                 if (!empty($newvalue)) {
                     $cached['head'][$now] = $newvalue;
                 }
             }
         } else {
             $cached['head'] = $document->getHeadData();
         }
     }
     if ($app->isSite() && $loptions['nopathway'] != 1) {
         $pathway = $app->getPathWay();
         $cached['pathway'] = isset($data['pathway']) ? $data['pathway'] : $pathway->getPathway();
     }
     if ($loptions['nomodules'] != 1) {
         $buffer2 = $document->getBuffer();
         if (!is_array($buffer2)) {
             $buffer2 = array();
         }
         if (!isset($buffer2['module']) || !is_array($buffer2['module'])) {
             $buffer2['module'] = array();
         }
         $cached['module'] = array_diff_assoc($buffer2['module'], $buffer1['module']);
     }
     return $cached;
 }
Example #16
0
 /**
  * 
  *
  * @return MRange
  */
 public function rangeOfString(MString $string)
 {
     $pos = strpos($this->stringValue(), $string->stringValue());
     if ($pos !== false) {
         return MRangeMake($pos, $string->length());
     } else {
         return MRange::RANGE_NOT_FOUND;
     }
 }
 /**
  * 
  */
 public function typeClassName()
 {
     if ($this->type() == MEntityDescriptionProperty::StringType) {
         return MString::className();
     } else {
         if ($this->type() == MEntityDescriptionProperty::IntegerType) {
             return MNumber::className();
         } else {
             if ($this->type() == MEntityDescriptionProperty::FloatType) {
                 return MNumber::className();
             } else {
                 if ($this->type() == MEntityDescriptionProperty::BooleanType) {
                     return MNumber::className();
                 } else {
                     if ($this->type() == MEntityDescriptionProperty::DateType) {
                         return MDate::className();
                     } else {
                         if ($this->type() == MEntityDescriptionProperty::BinaryType) {
                             return MData::className();
                         } else {
                             return S("");
                         }
                     }
                 }
             }
         }
     }
 }
Example #18
0
 /**
  * 
  */
 public function toString()
 {
     return MString::stringWithFormat("MArray[%d]:\n%s", $this->count(), var_export($this->array, true));
 }
Example #19
0
 protected static function _sortObjects(&$a, &$b)
 {
     $key = self::$sortKey;
     for ($i = 0, $count = count($key); $i < $count; $i++) {
         if (isset(self::$sortDirection[$i])) {
             $direction = self::$sortDirection[$i];
         }
         if (isset(self::$sortCase[$i])) {
             $caseSensitive = self::$sortCase[$i];
         }
         if (isset(self::$sortLocale[$i])) {
             $locale = self::$sortLocale[$i];
         }
         $va = $a->{$key}[$i];
         $vb = $b->{$key}[$i];
         if ((is_bool($va) or is_numeric($va)) and (is_bool($vb) or is_numeric($vb))) {
             $cmp = $va - $vb;
         } elseif ($caseSensitive) {
             $cmp = MString::strcmp($va, $vb, $locale);
         } else {
             $cmp = MString::strcasecmp($va, $vb, $locale);
         }
         if ($cmp > 0) {
             return $direction;
         }
         if ($cmp < 0) {
             return -$direction;
         }
     }
     return 0;
 }
Example #20
0
 /**
  *
  *
  * @return MData
  */
 public static function parseBase64String(MString $base64String)
 {
     return new MData(base64_decode($base64String->stringValue()));
 }
Example #21
0
 /**
  *
  */
 public function isKindOfClass(MString $class)
 {
     return is_a($this, $class->stringValue());
 }
Example #22
0
 public function parse($uri)
 {
     $retval = false;
     $this->_uri = $uri;
     if ($_parts = MString::parse_url($uri)) {
         $retval = true;
     }
     if (isset($_parts['query']) && strpos($_parts['query'], '&amp;')) {
         $_parts['query'] = str_replace('&amp;', '&', $_parts['query']);
     }
     $this->_scheme = isset($_parts['scheme']) ? $_parts['scheme'] : null;
     $this->_user = isset($_parts['user']) ? $_parts['user'] : null;
     $this->_pass = isset($_parts['pass']) ? $_parts['pass'] : null;
     $this->_host = isset($_parts['host']) ? $_parts['host'] : null;
     $this->_port = isset($_parts['port']) ? $_parts['port'] : null;
     $this->_path = isset($_parts['path']) ? $_parts['path'] : null;
     $this->_query = isset($_parts['query']) ? $_parts['query'] : null;
     $this->_fragment = isset($_parts['fragment']) ? $_parts['fragment'] : null;
     if (isset($_parts['query'])) {
         parse_str($_parts['query'], $this->_vars);
     }
     return $retval;
 }
 /**
  * Sets the object which represents the value for a certain attribute of this object
  *
  * Attributes are the properties and relationships of this object. This method sets
  * the object which represents the value of that attribute.
  *
  * You cannot use this method to set the value of a ToMany relationship, for that
  * you should use MManagedObject::addObjectToRelationship and
  * MManagedObject::removeObjectFromRelationship
  *
  * @see MManagedObject::addObjectToRelationship
  * @see MManagedObject::removeObjectFromRelationship
  *
  * @param MEntityDescriptionAttribute $attribute The attribute which you'd like to set
  * the value of
  * @param MObject $object The Object representing the value for that attribute
  *
  * @return void
  */
 public function setObjectForAttribute(MEntityDescriptionAttribute $attribute, MObject $object = null)
 {
     $this->fireFault();
     if ($attribute instanceof MEntityDescriptionProperty) {
         if ($object) {
             if ($attribute->type() == MEntityDescriptionProperty::StringType) {
                 if (!$object instanceof MString) {
                     throw new MInvalidManagedObjectOperationException($this, Sf("Invalid type [%s], expected [%s]!", str($object->className()), str(MString::className())));
                 }
             } else {
                 if ($attribute->type() == MEntityDescriptionProperty::IntegerType) {
                     if (!$object instanceof MNumber) {
                         throw new MInvalidManagedObjectOperationException($this, Sf("Invalid type [%s], expected [%s]!", str($object->className()), str(MNumber::className())));
                     }
                 } else {
                     if ($attribute->type() == MEntityDescriptionProperty::FloatType) {
                         if (!$object instanceof MNumber) {
                             throw new MInvalidManagedObjectOperationException($this, Sf("Invalid type [%s], expected [%s]!", str($object->className()), str(MNumber::className())));
                         }
                     } else {
                         if ($attribute->type() == MEntityDescriptionProperty::BooleanType) {
                             if (!$object instanceof MNumber) {
                                 throw new MInvalidManagedObjectOperationException($this, Sf("Invalid type [%s], expected [%s]!", str($object->className()), str(MNumber::className())));
                             }
                         } else {
                             if ($attribute->type() == MEntityDescriptionProperty::DateType) {
                                 if (!$object instanceof MDate) {
                                     throw new MInvalidManagedObjectOperationException($this, Sf("Invalid type [%s], expected [%s]!", str($object->className()), str(MDate::className())));
                                 }
                             } else {
                                 if ($attribute->type() == MEntityDescriptionProperty::BinaryType) {
                                     if (!$object instanceof MData) {
                                         throw new MInvalidManagedObjectOperationException($this, Sf("Invalid type [%s], expected [%s]!", str($object->className()), str(MData::className())));
                                     }
                                 } else {
                                     throw new MInvalidManagedObjectOperationException($this, Sf("Unsupported type [%s]!", $attribute->type()));
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $this->updatedData->setObjectForKey($attribute->name(), $object);
     } else {
         if ($attribute instanceof MEntityDescriptionRelationship) {
             if ($attribute->to() == MEntityDescriptionRelationship::ToMany) {
                 throw new MInvalidManagedObjectOperationException($this, S("Could not set a ToMany relationship, please use add/remove"));
             }
             if (($oldArr = $this->relationships->objectForKey($attribute->name())) != null) {
                 if ($oldArr->lastObject()) {
                     $this->removeObjectFromRelationship($attribute, $oldArr->lastObject());
                 }
             }
             $this->addObjectToRelationship($attribute, $object);
         } else {
             throw new MManagedObjectException($this, S("Unknown attribute type!"));
         }
     }
 }
Example #24
0
 /**
  * 
  *
  * @return MDate
  */
 public static function parseString(MString $string)
 {
     return MDate::parse($string->stringValue());
 }
Example #25
0
 /**
  * 
  */
 public function toString()
 {
     return MString::stringWithFormat("%s[%s]: \"%s\"\n%s", $this->className(), $this->code(), $this->description(), $this->previousException());
 }
 /**
  * Returns an instance of MEntityDescriptionAttribute matching
  * the specified path
  *
  * The path is a string which should be in the following format
  *
  * "EntityName.attributeName"
  *
  * @param MString $path The path of the attribute you wish to retrieve
  *
  * @return MEntityDescriptionAttribute The attribute matching the specified
  * path, or null if no matching attribute is found
  */
 public function attributeWithPath(MString $path)
 {
     $pathArray = $path->componentsSeparatedByString(S("."));
     if ($pathArray->count() == 2) {
         $entityName = $pathArray->objectAtIndex(0);
         $attributeName = $pathArray->objectAtIndex(1);
         return $this->entityWithName($entityName)->attributeWithName($attributeName);
     } else {
         return null;
     }
 }
 /**
  * Parses one or more Managed Objects from inside the specified XML string
  * and inserts them into this Managed Object Context
  *
  * @param MString $string A string containing the XML representation of the objects to be parsed
  * @param callable $callback A callback function which will be called every time a new
  * object is parsed and added into the Managed Object Context. The method signature for the
  * callback is callback(MManagedObject $object);
  *
  * @return MArray An Array containing the parsed objects
  */
 public function parseObjectsFromString(MString $string, callable $callback = null)
 {
     $xml = simplexml_load_string($string->stringValue());
     return $this->parseObjectsFromXML($xml, $callback);
 }
 /**
  * 
  *
  * @return void
  */
 public function replace(MString $find, MString $replace)
 {
     $this->string = str_replace($find->stringValue(), $replace->stringValue(), $this->stringValue());
 }
Example #29
0
 public function transliterate($string)
 {
     include_once dirname(__FILE__) . '/latin_transliterate.php';
     if ($this->transliterator !== null) {
         return call_user_func($this->transliterator, $string);
     }
     $string = MLanguageTransliterate::utf8_latin_to_ascii($string);
     $string = MString::strtolower($string);
     return $string;
 }