コード例 #1
0
ファイル: Object.class.php プロジェクト: xp-framework/core
 /**
  * Creates a string representation of this object. In general, the toString 
  * method returns a string that "textually represents" this object. The result 
  * should be a concise but informative representation that is easy for a 
  * person to read. It is recommended that all subclasses override this method.
  * 
  * Per default, this method returns:
  * ```
  *   [fully-qualified-class-name] '{' [members-and-value-list] '}'
  * ```
  * 
  * Example:
  * ```
  *   lang.Object {
  *     __id => "0.43080500 1158148350"
  *   }
  * ```
  *
  * @return  string
  */
 public function toString()
 {
     if (!$this->__id) {
         $this->__id = uniqid('', true);
     }
     return \xp::stringOf($this);
 }
コード例 #2
0
 /**
  * Locate a font
  *
  * @param   string font
  * @return  string
  */
 protected function locate($font)
 {
     $windows = strncasecmp(PHP_OS, 'Win', 3) === 0;
     // Compile extension list
     $extensions = ['.ttf', '.TTF'];
     if (strncasecmp($ext = substr($font, -4, 4), '.ttf', 4) === 0) {
         $font = substr($font, 0, -4);
         $extensions[] = $ext;
     }
     // Compose TTF search path
     if ($windows) {
         $search = ['.\\', getenv('WINDIR') . '\\fonts\\'];
     } else {
         $search = ['./', '/usr/X11R6/lib/X11/fonts/TrueType/', '/usr/X11R6/lib/X11/fonts/truetype/', '/usr/X11R6/lib/X11/fonts/TTF/', '/usr/share/fonts/TrueType/', '/usr/share/fonts/truetype/', '/usr/openwin/lib/X11/fonts/TrueType/'];
     }
     // Check for absolute filenames
     if (DIRECTORY_SEPARATOR === $font[0] || $windows && strlen($font) > 1 && (':' === $font[1] || '/' === $font[0])) {
         array_unshift($search, dirname($font) . DIRECTORY_SEPARATOR);
         $font = basename($font);
     }
     // Search
     foreach ($search as $dir) {
         foreach ($extensions as $ext) {
             if (file_exists($q = $dir . $font . $ext)) {
                 return $q;
             }
         }
     }
     throw new \lang\IllegalArgumentException('Could not locate font "' . $font . '[' . implode(', ', $extensions) . ']" in ' . \xp::stringOf($search));
 }
コード例 #3
0
 /**
  * Find an entry
  *
  * @param   string name
  * @return  peer.ftp.FtpEntry entry or NULL if nothing was found
  * @throws  io.IOException in case listing fails
  * @throws  peer.ProtocolException in case listing yields an unexpected result
  */
 protected function findEntry($name)
 {
     if (NULL === ($list = $this->connection->listingOf($this->name . $name, '-ald'))) {
         return NULL;
         // Not found
     }
     // If we get more than one result and the first result ends with a
     // dot, the server ignored the "-d" option and listed the directory's
     // contents instead. In this case, replace the "." by the directory
     // name. Otherwise, we don't expect more than one result!
     $entry = $list[0];
     if (($s = sizeof($list)) > 1) {
         if ('.' === $entry[strlen($entry) - 1]) {
             $entry = substr($entry, 0, -1) . basename($name);
         } else {
             throw new ProtocolException('List "' . $this->name . $name . '" yielded ' . $s . ' result(s), expected: 1 (' . xp::stringOf($list) . ')');
         }
     }
     // Calculate base
     $base = $this->name;
     if (FALSE !== ($p = strrpos(rtrim($name, '/'), '/'))) {
         $base .= substr($name, 0, $p + 1);
     }
     return $this->connection->parser->entryFrom($entry, $this->connection, $base);
 }
コード例 #4
0
 /**
  * Processes cell value
  *
  * @param   var in
  * @return  var
  * @throws  lang.FormatException
  */
 public function process($in)
 {
     if (!(null === $in || is_numeric($in))) {
         throw new \lang\FormatException('Cannot format non-number ' . \xp::stringOf($in));
     }
     return $this->proceed(number_format($in, $this->decimals, $this->decimalPoint, $this->thousandsSeparator));
 }
コード例 #5
0
 /**
  * Executes this check
  *
  * @param   xp.compiler.ast.Node node
  * @param   xp.compiler.types.Scope scope
  * @return  bool
  */
 public function verify(\xp\compiler\ast\Node $node, \xp\compiler\types\Scope $scope)
 {
     $a = \cast($node, 'xp.compiler.ast.AssignmentNode');
     if (!$this->isWriteable($a->variable)) {
         return ['A403', 'Cannot assign to ' . ($a->variable instanceof \lang\Generic ? nameof($a->variable) : \xp::stringOf($a->variable)) . 's'];
     }
 }
コード例 #6
0
 /**
  * Send a message
  *
  * @param   peer.mail.Message message the Message object to send
  * @return  bool success
  */
 public function send($message)
 {
     // Sanity check: Is this a message?
     if (!$message instanceof Message) {
         throw new TransportException('Can only send messages (given: ' . xp::typeOf($message) . ')', new IllegalArgumentException('Parameter message is not a Message object'));
     }
     // Sanity check: Do we have at least one recipient?
     $to = '';
     for ($i = 0, $s = sizeof($message->to); $i < $s; $i++) {
         if (!$message->to[$i] instanceof InternetAddress) {
             continue;
         }
         // Ignore!
         $to .= $message->to[$i]->toString($message->getCharset()) . ', ';
     }
     if (empty($to)) {
         throw new TransportException('No recipients defined (recipients[0]: ' . xp::typeOf($message->to[0]), new IllegalArgumentException('Recipient #0 is not an InternetAddress object'));
     }
     // Copy message and unset To / Subject. PHPs mail() function will add them
     // to the mail twice, otherwise
     $tmp = clone $message;
     unset($tmp->to);
     unset($tmp->subject);
     if (FALSE === mail(substr($to, 0, -2), QuotedPrintable::encode($message->getSubject(), $message->getCharset()), strtr($message->getBody(), array("\r\n" => "\n", "\r" => "\n")), rtrim($tmp->getHeaderString(), "\n"), $this->parameters)) {
         throw new TransportException('Could not send mail to ' . xp::stringOf($message->to[0]), new IOException('Call to mail() failed'));
     }
     return TRUE;
 }
コード例 #7
0
 /**
  * Register an algorithm
  *
  * @param   string name
  * @param   lang.XPClass<security.password.Algorithm> impl
  * @throws  lang.IllegalArgumentException in case the given class is not an Algorithm 
  */
 public static function setAlgorithm($name, \lang\XPClass $impl)
 {
     if (!$impl->isSubclassOf('security.password.Algorithm')) {
         throw new \lang\IllegalArgumentException('Given argument is not an Algorithm class (' . \xp::stringOf($impl) . ')');
     }
     self::$algorithms[$name] = $impl;
 }
コード例 #8
0
 /**
  * Constructor. Accepts one of the following:
  *
  * <ul>
  *   <li>The values TRUE or FALSE</li>
  *   <li>An integer - any non-zero value will be regarded TRUE</li>
  *   <li>The strings "true" and "false", case-insensitive</li>
  *   <li>Numeric strings - any non-zero value will be regarded TRUE</li>
  * </ul>
  *
  * @param   var value
  * @throws  lang.IllegalArgumentException if value is not acceptable
  */
 public function __construct($value)
 {
     if (TRUE === $value || FALSE === $value) {
         $this->value = $value;
     } else {
         if (is_int($value)) {
             $this->value = 0 !== $value;
         } else {
             if ('0' === $value) {
                 $this->value = FALSE;
             } else {
                 if (is_string($value) && ($l = strlen($value)) && strspn($value, '1234567890') === $l) {
                     $this->value = TRUE;
                 } else {
                     if (0 === strncasecmp($value, 'true', 4)) {
                         $this->value = TRUE;
                     } else {
                         if (0 === strncasecmp($value, 'false', 5)) {
                             $this->value = FALSE;
                         } else {
                             throw new IllegalArgumentException('Not a valid boolean: ' . xp::stringOf($value));
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #9
0
 /**
  * Export this keypair
  *
  * @param   string passphrase default NULL
  * @return  string key
  */
 public function export($passphrase = null)
 {
     if (false === openssl_pkey_export($this->_res, $out, $passphrase)) {
         throw new SecurityException('Could not export key: ' . \xp::stringOf(OpenSslUtil::getErrors()));
     }
     return $out;
 }
コード例 #10
0
 /**
  * Processes cell value
  *
  * @param   var in
  * @return  var
  * @throws  lang.FormatException
  */
 public function process($in)
 {
     if (!$in->getClass()->isEnum()) {
         throw new FormatException('Cannot format non-enum ' . xp::stringOf($in));
     }
     return $this->proceed($in->name());
 }
コード例 #11
0
ファイル: Lookup.class.php プロジェクト: Gamepay/xp-framework
 public function get($key)
 {
     $offset = $key instanceof Generic ? $key->hashCode() : serialize($key);
     if (!isset($this->elements[$offset])) {
         throw new NoSuchElementException('No such key ' . xp::stringOf($key));
     }
     return $this->elements[$offset];
 }
コード例 #12
0
ファイル: Links.class.php プロジェクト: xp-framework/rest
 /**
  * Parser helper function
  *
  * @param  text.Tokenizer $st
  * @param  string $tokens
  * @return string
  * @throws lang.FormatException
  */
 private function expect($st, $tokens)
 {
     $parsed = $st->nextToken($tokens);
     if (false === strpos($tokens, $parsed)) {
         throw new FormatException('Expected [' . $tokens . '], have ' . \xp::stringOf($parsed));
     }
     return $parsed;
 }
コード例 #13
0
ファイル: Node.class.php プロジェクト: xp-lang/compiler
 /**
  * Creates a string representation of this node.
  *
  * @return  string
  */
 public function toString()
 {
     $s = nameof($this) . '(line ' . $this->position[0] . ', offset ' . $this->position[1] . ")@{\n";
     foreach (get_object_vars($this) as $name => $value) {
         '__id' !== $name && 'position' !== $name && 'holder' !== $name && ($s .= sprintf("  [%-20s] %s\n", $name, str_replace("\n", "\n  ", \xp::stringOf($value))));
     }
     return $s . '}';
 }
コード例 #14
0
ファイル: Objects.class.php プロジェクト: johannes85/core
 /**
  * Returns a string representation
  *
  * @param  var $val
  * @param  string $default the value to use for NULL
  * @return string
  */
 public static function stringOf($val, $default = '')
 {
     if (null === $val) {
         return $default;
     } else {
         return \xp::stringOf($val);
     }
 }
コード例 #15
0
 public function get($key)
 {
     $offset = $key->literal();
     if (!isset($this->elements[$offset])) {
         throw new NoSuchElementException('No such key ' . xp::stringOf($key));
     }
     return $this->elements[$offset];
 }
コード例 #16
0
 /**
  * Creates a string representation of this object
  *
  * @return  string
  */
 public function toString()
 {
     $r = $this->getClassName() . '@(class= ' . $this->className . ") {\n";
     for ($i = 0; $i < $this->methods->length; $i++) {
         $r .= '  - ' . xp::stringOf($this->methods[$i], '  ') . "\n";
     }
     return $r . '}';
 }
コード例 #17
0
ファイル: Lookup.class.php プロジェクト: xp-framework/core
 public function get($key)
 {
     $offset = Objects::hashOf($key);
     if (!isset($this->elements[$offset])) {
         throw new \util\NoSuchElementException('No such key ' . \xp::stringOf($key));
     }
     return $this->elements[$offset];
 }
コード例 #18
0
 /**
  * Creates a string representation of this object
  *
  * @return  string
  */
 public function toString()
 {
     $s = $this->getClassName() . '(size= ' . $this->pool->size() . ")@{\n";
     foreach ($this->pool->keys() as $url) {
         $s .= '  ' . $url->getURL() . ' => ' . xp::stringOf($this->pool->get($url)) . "\n";
     }
     return $s . '}';
 }
コード例 #19
0
 /**
  * Write
  *
  * @param  var[] $args
  * @return void
  */
 private function write0($args)
 {
     $line = '';
     foreach ($args as $arg) {
         $line .= is_string($arg) ? $arg : \xp::stringOf($arg);
     }
     $this->out->write(preg_replace($this->patterns, $this->replacements, $line));
 }
コード例 #20
0
 /**
  * Creates a string representation of this object
  *
  * @return  string
  */
 public function toString()
 {
     $fields = '';
     foreach (array_keys($this->fields) as $name) {
         $fields .= sprintf("  [%-20s] %s\n", $name, xp::stringOf($this->fields[$name]));
     }
     return sprintf("%s@(name= %s) {\n%s}", $this->getClassName(), $this->className, $fields);
 }
コード例 #21
0
ファイル: ParseTree.class.php プロジェクト: xp-forge/markdown
 /**
  * Creates a string representation
  *
  * @return string
  */
 public function toString()
 {
     $s = '';
     foreach ($this->nodes as $node) {
         $s .= '  ' . str_replace("\n", "\n  ", $node->toString()) . "\n";
     }
     return nameof($this) . "@{\n" . "urls  : " . \xp::stringOf($this->urls) . "\n" . "nodes : [\n" . $s . "]\n" . "}";
 }
コード例 #22
0
 /**
  * Creates a string representation of this object
  *
  * @return  string
  */
 public function toString()
 {
     $s = $this->getClassName() . '@(' . $this->__name . ") {\n";
     foreach (array_keys($this->__members) as $member) {
         $s .= sprintf("  [%-20s] %s\n", $member, xp::stringOf($this->__members[$member]));
     }
     return $s . '}';
 }
コード例 #23
0
 /**
  * Creates a string representation of this iterator
  *
  * @return  string
  */
 public function toString()
 {
     $s = $this->getClassName() . '(' . $this->_size . ")@{\n";
     for ($i = 0; $i < $this->_size; $i++) {
         $s .= '  ' . xp::stringOf($this->list[$i], '  ') . "\n";
     }
     return $s . '}';
 }
コード例 #24
0
 /**
  * Create a string representation of this list
  *
  * @return  string
  */
 public function toString()
 {
     $s = sizeof($this->items);
     $r = sprintf("%s (operator='%s',#items=%d,truncated=%s)@{\n", nameof($this), $this->operator, $s, var_export($this->truncated, 1));
     for ($i = 0; $i < $s; $i++) {
         $r .= '  ' . \xp::stringOf($this->items[$i], '  ') . "\n";
     }
     return $r .= '}';
 }
コード例 #25
0
ファイル: LoginFrame.class.php プロジェクト: xp-forge/stomp
 /**
  * Set supported STOMP versions
  *
  * @param   [:string] v list of supported versions
  */
 public function setSupportedVersions(array $versions)
 {
     foreach ($versions as $v) {
         if (strlen($v) == 0) {
             throw new \lang\IllegalArgumentException('Invalid protocol version: ' . \xp::stringOf($v));
         }
     }
     $this->versions = $versions;
 }
コード例 #26
0
 /**
  * Constructor
  *
  * @param   unittest.TestCase base
  * @param   var[] args
  */
 public function __construct($base, $args)
 {
     $uniq = '';
     foreach ((array) $args as $arg) {
         $uniq .= ', ' . xp::stringOf($arg);
     }
     parent::__construct($base->getName() . '(' . substr($uniq, 2) . ')');
     $this->base = $base;
 }
コード例 #27
0
 /**
  * Enable cryptography on a given socket
  *
  * @param  peer.Socket $s
  * @param  [:int] $methods
  * @return void
  * @throws io.IOException
  */
 protected function enable($s, $methods)
 {
     foreach ($methods as $name => $method) {
         if (stream_socket_enable_crypto($s->getHandle(), true, $method)) {
             $this->cat && $this->cat->debug('@@@ Enabling', $name, 'cryptography');
             return;
         }
     }
     throw new \io\IOException('Cannot establish secure connection, tried ' . \xp::stringOf($methods));
 }
コード例 #28
0
 /**
  * Print arguments and append a newline
  *
  * @param   var... args
  */
 public function writeLine(...$args)
 {
     foreach ($args as $arg) {
         if (is_string($arg)) {
             $this->out->write($arg);
         } else {
             $this->out->write(\xp::stringOf($arg));
         }
     }
     $this->out->write("\n");
 }
コード例 #29
0
ファイル: ArrayNode.class.php プロジェクト: xp-lang/compiler
 /**
  * Resolve this node's value.
  *
  * @return  var
  */
 public function resolve()
 {
     $resolved = [];
     foreach ($this->values as $i => $value) {
         if (!$value instanceof Resolveable) {
             throw new \lang\IllegalStateException('Value at offset ' . $i . ' is not resolveable: ' . \xp::stringOf($value));
         }
         $resolved[] = $value->resolve();
     }
     return $resolved;
 }
コード例 #30
0
 /**
  * Measure a closure
  *
  * @see    http://php.net/manual/en/language.types.callable.php
  * @param  var block a callable
  * @return self
  * @throws lang.IllegalArgumentException when block is not callable
  */
 public static function measure($block)
 {
     if (!is_callable($block)) {
         throw new IllegalArgumentException('Cannot call ' . xp::stringOf($block));
     }
     $self = new self();
     $self->start = microtime(TRUE);
     $block();
     $self->stop = microtime(TRUE);
     return $self;
 }