/** * Wrap value in AeNumeric instance * * This method selects the respective class and wraps the passed value using * that class. If the value passed is not a numeric value, false is * returned. Numeric values are integer, float and numeric string values. * * @param mixed $value * * @return AeScalar|false */ public static function wrap($value) { if ($value instanceof AeNumeric) { return $value; } if ($value instanceof AeString) { $value = $value->getValue(); } if (is_numeric($value)) { switch (gettype($value)) { case 'integer': return new AeInteger($value); break; case 'double': return new AeFloat($value); break; case 'string': if ($value == round($value) && $value >= AeInteger::MIN && $value <= AeInteger::MAX) { return new AeInteger($value); } return new AeFloat($value); break; } } throw new AeNumericException('Invalid value type: expecting numeric, ' . AeType::of($value) . ' given', 400); }
/** * Set a float value * * @throws AeFloatException #400 on invalid value * * @param float $value * * @return AeFloat self */ public function setValue($value) { $value = (double) $value; if (!is_float($value)) { throw new AeFloatException('Invalid value passed: expecting float, ' . AeType::of($value) . ' given', 400); } $this->_value = $value; return $this; }
/** * Call event listener method * * Calls an event listener method and handles return values. * * <b>NOTE:</b> For backwards compatibility with the {@link AeObject::call()} * method, a second parameter is introduced. If the <var>$args</var> parameter * is a string, it is used as the method name, and <var>$ma</var> is used * as an array of parameters for that method * * @see AeCallback::call(), AeObject::call() * * @param array|string $args an array of parameters * @param array $ma * * @return mixed */ public function call($args, $ma = array()) { if (AeType::of($args) == 'string') { return parent::call($args, $ma); } if (parent::call($args) === false) { $args[0]->stop(); } return $this; }
/** * Set a boolean value * * Unlike PHP's cast to boolean, a string 'false' will result in boolean * false being set. * * @throws AeBooleanException #400 on invalid value * * @param bool $value * * @return AeBoolean self */ public function setValue($value) { if (is_string($value) && $value == 'false') { $value = false; } $value = (bool) $value; if (!is_bool($value)) { throw new AeBooleanException('Invalid value passed: expecting bool, ' . AeType::of($value) . ' given', 400); } $this->_value = $value; return $this; }
/** * Set timezone * * Sets the timezone value. This method accepts timezone names as strings. * You can get the full list of accepted values in the PHP manual: {@link * http://php.net/manual/en/timezones.php} (note, some timezones inside the * Others are not accepted): * <code> $tz = new AeDate_Timezone('Europe/Amsterdam'); * $date = new AeDate('2009-05-12 14:00:00', 'Europe/Moscow'); * * echo $date; // Tue, 12 May 2009 14:00:00 +0400 * echo $date->setTimezone($tz); // Tue, 12 May 2009 12:00:00 +0200</code> * * @throws AeDateTimezoneException #400 on invalid value * @throws AeDateTimezoneException #413 on unrecognized timezone identifier * * @param string|DateTimeZone $value * * @return AeDate_Timezone self */ public function setValue($value) { if ($value instanceof AeString) { $value = $value->getValue(); } if (!is_string($value) && !$value instanceof DateTimeZone) { throw new AeDateTimezoneException('Invalid value passed: expecting string or DateTimeZone, ' . AeType::of($value) . ' given', 400); } if (!$value instanceof DateTimeZone) { if (strpos($value, ' ') !== false) { $value = str_replace(' ', '_', trim($value)); } $zone = @timezone_open($value); if (!$zone) { throw new AeDateTimezoneException('Unrecognized timezone identifier: ' . $value, 413); } $value = $zone; } $this->_value = $value; return $this; }
/** * Wrap value in AeScalar instance * * This method selects the respective class and wraps the passed value using * that class. If the value passed is not a scalar value, false is returned. * Currently, only boolean, integer, float and string values are scalar. * * @param mixed $value * * @return AeScalar|false */ public static function wrap($value) { if ($value instanceof AeScalar) { return $value; } if (is_scalar($value)) { switch (AeType::of($value)) { case 'boolean': return new AeBoolean($value); break; case 'integer': return new AeInteger($value); break; case 'float': return new AeFloat($value); break; case 'string': return new AeString($value); break; } } throw new AeScalarException('Invalid value type: expecting scalar, ' . AeType::of($value) . ' given', 400); }
public function loadRecord($id, $foreign = null) { $type = AeType::of($foreign); if ($type == 'boolean') { $foreign = $foreign instanceof AeType ? $foreign->getValue() : $foreign; } else { if ($type != 'null') { throw new AeOrmModelException('Invalid foreign value type: expecting boolean or null, ' . $type . ' given', 400); } else { $foreign = $this->_autoForeign; } } $class = $this->_recordClass; if (!class_exists($class)) { throw new AeOrmModelException('Record class not found', 404); } $this->loadTableDefinition(); // TODO: add foreign records selection #$this->loadTableRelations(); $query = $this->_database->queryObject(); $query->select(); $query->from($this->_tableName, $this->_aliasName); // TODO: move this into a separate protected method $name = $this->_fields[$this->_primaryKey]['name']; $query->where($name, $query->bind($name, $id)); $row = $query->execute(1)->getRow(); if (AeType::of($row) == null) { throw new AeOrmModelException('Record not found', 404); } $record = new $class($this); // TODO: add foreign records assignment foreach ($this->_fields as $alias => $params) { $record->set($alias, $row[$params['name']]); } $record->state = AeOrm_Record::STATE_READY; return $record; }
/** * Read method * * Always returns string value to make save handler work as expected. * Returns empty string if there is no data to read * * @param string $id * * @return string */ public function read($id) { $db = $this->getConnection(); $query = $db->queryObject(); $query->select('data')->from($this->_storageTable)->where()->bind('id', $id); $field = $query->execute(1)->getField(); if (AeType::of($field) == 'null') { return ''; } return (string) $field; }
/** * Fire event * * Triggers an event, identified by <var>$name</var>, using <var>$args</var> * as additional event parameters: * <code> $myObj->fireEvent('foo', array('foo', 'bar', 'baz'));</code> * * An event handler for such fireEvent call might look something like this: * <code> function onFoo($event, $foo, $bar, $baz) * { * var_dump($foo, $bar, $baz); * }</code> * * The second parameter may also be a single variable instead of an array, * if you only want to pass one parameter to the event handler: * <code> $myObj->fireEvent('foo', 'bar');</code> * * This method returns false, if an event listener requested to stop the * default event action (see {@link AeEvent::preventDefault()}), true * otherwise. Note, that not all classes support the preventDefault action. * * @throws AeObjectException #400 on invalid name * * @param string $name event name * @param mixed|array $args event parameters * * @return bool */ public function fireEvent($name, $args = array()) { $type = AeType::of($name); if ($type != 'string') { throw new AeObjectException('Invalid name type: expecting string, ' . $type . ' given', 400); } if (is_array($this->___events)) { $name = strtolower((string) $name); if (isset($this->___events[$name]) && is_array($this->___events[$name])) { $events = $this->___events[$name]; if (count($events) > 0) { $event = new AeEvent($name, $this); if (!is_array($args)) { $args = array($event, $args); } else { array_unshift($args, $event); } foreach ($events as $listener) { $listener->call($args); if ($event->_getStopPropagation()) { break; } } if ($event->_getPreventDefault()) { return false; } } } } return true; }
/** * Get session variable * * Returns the session variable, identified by the <var>$name</var> * parameter, or the <var>$default</var>, if variable is not set or is null * * @uses AeSession::_check() to check for session state * * @throws AeSessionException #403 if session validation failed * @throws AeSessionException #408 if session has expired * @throws AeSessionException #412 if session has been destroyed * * @param string $name * @param mixed $default * * @return object|AeScalar|AeArray */ public function get($name, $default = null) { $name = (string) $name; if ($this->propertyExists($name)) { // *** Never wrap object properties return parent::get($name, $default); } $this->_check(); if (!preg_match($this->_keyPattern, (string) $name)) { throw new AeSessionException('Key name is invalid', 400); } $return = $this->_getByKey((string) $name, $_SESSION); return AeType::wrapReturn($return, $default); }
public static function set($class, $instance, $args = array()) { if (!is_object($instance)) { throw new AeInstanceException('Invalid value passed: expecting object, ' . AeType::of($instance) . ' given', 400); } $key = self::generateKey($class, $args); self::$_instances[$key] = $instance; return true; }
/** * Return offset value * * Method for the {@link ArrayAccess} interface implementation * * @throws AeArrayException #413 if offset does not exist * * @param mixed $offset * * @return mixed */ public function offsetGet($offset) { if ($offset instanceof AeScalar) { $offset = $offset->getValue(); } if (!$this->offsetExists($offset)) { throw new AeArrayException('Invalid offset value: offset does not exist', 413); } return AeType::wrapReturn($this->_value[$offset]); }
/** * Set document type * * Sets the document type to the one specified. You can use one of the * preset document types via class constants (see below), or provide your * own doctype definition parameters: * <code> $document->setType( * 'PUBLIC "-//W3C//DTD XHTML 1.1//EN"', * 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd' * , true); </code> * * The effect of the example above is similar to that of: * <code> $document->setType(AeDocument::TYPE_XHTML_11);</code> * * @see AeDocument::TYPE_HTML_401_STRICT, AeDocument::TYPE_HTML_401_FRAMESET, * AeDocument::TYPE_HTML_401_TRANSITIONAL * @see AeDocument::TYPE_XHTML_10_STRICT, AeDocument::TYPE_XHTML_10_FRAMESET, * AeDocument::TYPE_XHTML_10_TRANSITIONAL * @see AeDocument::TYPE_XHTML_11 * @see AeDocument::TYPE_HTML_5 * * @param int|string $type doctype constant or an FPI string * @param string $link doctype DTD link * @param bool $xmlHead if this is true, an xml header will be added * above the doctype * * @return AeDocument self */ public function setType($type = null, $link = null, $xmlHead = null) { if ($type === null) { $type = self::TYPE_XHTML_10_STRICT; } if (is_int($type)) { // *** Pre-defined constants switch ($type) { case self::TYPE_XHTML_11: $dt = array('PUBLIC "-//W3C//DTD XHTML 1.1//EN"', 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd', true); break; case self::TYPE_XHTML_10_TRANSITIONAL: $dt = array('PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd', true); break; case self::TYPE_XHTML_10_FRAMESET: $dt = array('PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd', true); break; case self::TYPE_HTML_401_STRICT: $dt = array('PUBLIC "-//W3C//DTD HTML 4.01//EN"', 'http://www.w3.org/TR/html4/strict.dtd'); break; case self::TYPE_HTML_401_TRANSITIONAL: $dt = array('PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"', 'http://www.w3.org/TR/html4/loose.dtd'); break; case self::TYPE_HTML_401_FRAMESET: $dt = array('PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"', 'http://www.w3.org/TR/html4/frameset.dtd'); break; case self::TYPE_HTML_5: $dt = ''; break; case self::TYPE_XHTML_10_STRICT: default: $dt = array('PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd', true); break; } } else { $dt = array(); } if ($type instanceof AeScalar) { $type = (string) $type; } if ($type instanceof AeArray) { $type = $type->getValue(); } if (empty($dt)) { if (!is_string($type) && !is_array($type)) { throw new AeDocumentException('Invalid type passed: expecting constant, string or array, ' . AeType::of($type) . ' given', 400); } else { if (is_string($type)) { $dt = array($type); } else { $dt = $type; } } } if ($link !== null) { if ($link instanceof AeScalar) { $link = (string) $link; } $dt[1] = $link; } if ($xmlHead !== null) { if ($xmlHead instanceof AeScalar) { $xmlHead = $xmlHead->toBoolean()->getValue(); } $dt[2] = $xmlHead; } $this->_type = $dt; return $this; }
/** * Get result rows * * Returns all result rows as a two-dimensional array. Returns null if the * result is empty.If <var>$key</var> parameter is set, that field will be * used as an index for the results array. * * If {@link AeType::$wrapReturn} is true, this method returns {@link AeNull} * instead of null * * @param string $key index field name * * @return AeArray */ public function getRows($key = null) { if ($this->getResult() === null && $this->getQuery()) { $this->execute(); } if ($this->getResult() === null) { return AeType::wrapReturn(null); } if (!is_null($key)) { $key = (string) $key; } $return = array(); while (AeType::of($row = $this->getRow()) != 'null') { if (!is_null($key) && isset($row[$key])) { $return[(string) $row[$key]] = $row; continue; } $return[] = $row; } $this->setResult(null); return AeType::wrapReturn($return); }
public function append($string, $length = null) { if (!$this->exists()) { throw new AeFileException('Cannot write: file does not exist', 404); } $string = (string) $string; if ($length !== null) { if ($length instanceof AeScalar) { $length = $length->getValue(); } $type = AeType::of($length); if ($type != 'integer') { throw new AeFileException('Invalid length value: expecting integer, ' . $type . ' given', 400); } } $fh = $this->_open('ab'); if ($length !== null) { $result = @fwrite($fh, $string, $length); } else { $result = @fwrite($fh, $string); } if ($result === false) { $e = error_get_last(); throw new AeFileException('Cannot write: ' . $e['message'], 500); } return $this; }
/** * Set a string value * * @throws AeStringException #400 on invalid value * * @param string $value * * @return AeString self */ public function setValue($value) { if (is_bool($value)) { $value = $value ? 'true' : 'false'; } else { $value = (string) $value; } if (!is_string($value)) { throw new AeStringException('Invalid value passed: expecting string, ' . AeType::of($value) . ' given', 400); } $this->_value = $value; return $this; }
/** * Set an integer value * * If the value passed is not an integer (float or a numeric string), it * will be converted to integer. If the value is out of the integer bounds, * false is returned * * @throws AeIntegerException #413 if value is out of integer range * @throws AeIntegerException #400 on invalid value * * @uses AeInteger::MIN * @uses AeInteger::MAX * * @param int $value * * @return AeInteger self */ public function setValue($value) { if ($value < self::MIN || $value > self::MAX) { throw new AeIntegerException('Invalid value passed: value is out of integer range', 413); } $value = (int) $value; if (!is_int($value)) { throw new AeIntegerException('Invalid value passed: expecting int, ' . AeType::of($value) . ' given', 400); } $this->_value = $value; return $this; }
public function getSection($name = null) { if ($name === null) { return $this->_section; } $name = (string) $name; return AeType::wrapReturn(isset($this->_properties[$name]) ? $this->_properties[$name] : array()); }
/** * Set date value * * The supported values are either a unix timestamp, a string, accepted by * the {@link strtotime() strtotime()} PHP function, a DateTime object or an * array with the following keys: year, month, day, hour, minute, second. If * one of the keys is not set, the current value is used. If none of them * are set (an array is empty), the current date and time is used: * <code> // The following three lines create date objects with the same value * $d1 = new AeDate(array( * 'year' => 2000, * 'month' => 12, * 'day' => 31, * 'hour' => 15, * 'minute' => 0, * 'second' => 0 * ), 'UTC'); * $d2 = new AeDate('2000-12-31 15:00:00', 'UTC'); * $d3 = new AeDate(978274800); // Unix timestamp * * echo $d1 . "\n" . $d2 . "\n" . $d3;</code> * * The above will output the following: * <pre> Sun, 31 Dec 2000 15:00:00 +0000 * Sun, 31 Dec 2000 15:00:00 +0000 * Sun, 31 Dec 2000 17:00:00 +0200</pre> * * You can also pass null to set the date to current date and time. * * @throws AeDateException #400 on invalid value * * @param array|string|float|int|DateTime $value * @param string|DateTimeZone|AeDate_Timezone $timezone * * @return AeDate self */ public function setValue($value, $timezone = null) { if ($value instanceof AeType) { $value = $value->getValue(); } // *** Illegal value type if (!is_array($value) && !is_string($value) && !is_int($value) && !is_float($value) && !$value instanceof DateTime) { throw new AeDateException('Invalid value passed: expecting array, string, integer, float or DateTime, ' . AeType::of($value) . ' given', 400); } $timezone = AeDate::timezone($timezone); $zone = @timezone_open(str_replace(' ', '_', $timezone->getValue())); // *** Convert a numeric string into a number if (is_string($value) && is_numeric($value)) { // *** The correct type is set below $value = (double) $value; } // *** Cast to integer if float is inside an integer value range if (is_float($value) && $value <= AeInteger::MAX && $value >= AeInteger::MIN) { $value = (int) $value; } if (is_array($value)) { foreach ($value as $k => $v) { if (!in_array((string) $k, array('year', 'month', 'day', 'hour', 'minute', 'second'))) { unset($value[$k]); } } $_string = ''; $_bits = array(); $_bits[] = isset($value['year']) ? $value['year'] : date('Y'); $_bits[] = isset($value['month']) ? $value['month'] : date('m'); $_bits[] = isset($value['day']) ? $value['day'] : date('d'); $_string .= implode('-', $_bits) . ' '; $_bits = array(); $_bits[] = isset($value['hour']) ? $value['hour'] : date('H'); $_bits[] = isset($value['minute']) ? $value['minute'] : date('i'); $_bits[] = isset($value['second']) ? $value['second'] : date('s'); $_string .= implode(':', $_bits); $value = new DateTime($_string, $zone); unset($_string, $_bits); } if ($value instanceof DateTime) { $value->setTimezone($zone); } else { if (!is_string($value)) { $value = date_create(date('Y-m-d H:i:s', $value)); $value->setTimezone($zone); } else { $value = date_create($value); $value->setTimezone($zone); } } $this->_value = $value; return $this; }
/** * Set interval * * Sets the interval value. This method accepts the following values: * - <b>DateInterval format</b> - the PHP's interval format, available since * PHP 5.3.0. The 'P1Y2M3DT4H5M6S' sets the * interval to 1 year, 2 months, 3 days, 4 * hours, 5 minutes and 6 seconds * - <b>An array of values</b> - * * The interval consists of two blocks, at least one should be present for * this method to accept the value passed. For the date use "P3D", "P3M", * "P3Y" or a combination of the three e.g. "P2M5D" (Y = Years, M = Months, * D = Days). <b>Must be year month day format</b> "P5Y", "P5M2D", "P5Y4D". For * the time use "T3H", "T3M", "T3S" or a combination of the three e.g. * "T5H20M" (H = Hours, M = Minutes, S = Seconds). For dateTime use * "P5D2M4YT5H20M". The digit before the letter (NOT P or T) can be any * amount: * <code> // 7 days * $foo = new AeDate_Interval('P7D'); * $bar = new AeDate_Interval(array('days' => 7)); * * // 72 hours * $foo = new AeDate_Interval('T72H'); * $bar = new AeDate_Interval(array('hours' => 72)); * * // 3 months, 3 minutes * $foo = new AeDate_Interval('P3MT3M'); * $bar = new AeDate_Interval(array('months' => 3, 'minutes' => 3)); * * // Empty interval (zero seconds) * $foo = new AeDate_Interval('T0S'); // Or you can just use '0' * $bar = new AeDate_Interval(array());</code> * * <b>NOTE:</b> the interval value passed is simplified and may return a * different string: * <code> // 50 hours * $foo = new AeDate_Interval('T50H'); * echo $foo; // outputs 'P2DT2H': 2 days and 2 hours</code> * This simplification, however, does not affect the number of days, since * different months have different number of days, but the interval itself * does not reflect a date and there is no way to convert days into months * without knowing the actual date: * <code> // 30 days and 72 hours * $foo = new AeDate_Interval('P30DT72H'); * echo $foo; // outputs 'P33D': 33 days</code> * * @throws AeDateIntervalException #400 on invalid value * * @param array|string $value * * @return AeDate_Interval self */ public function setValue($value) { if ($value instanceof AeType) { $value = $value->getValue(); } if (is_numeric($value) && $value == 0 || is_array($value) && empty($value)) { // *** All values are zero $this->_value = array(0, 0, 0, 0, 0, 0); return $this; } // *** Check DateInterval format if (is_string($value)) { if (!preg_match('#^(?:P(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)D)?)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)S)?)?$#', $value, $matches)) { throw new AeDateIntervalException('Invalid interval value: string value must match DateInterval format', 400); } if (count($matches) == 1) { return $this; } $this->_value = $this->_simplify(array(0 => (int) @$matches[1], 1 => (int) @$matches[2], 2 => (int) @$matches[3], 3 => (int) @$matches[4], 4 => (int) @$matches[5], 5 => (int) @$matches[6])); return $this; } if (is_array($value)) { $this->_value = $this->_simplify(array(0 => (int) @$value['years'], 1 => (int) @$value['months'], 2 => (int) @$value['days'], 3 => (int) @$value['hours'], 4 => (int) @$value['minutes'], 5 => (int) @$value['seconds'])); return $this; } throw new AeDateIntervalException('Invalid interval value: expecting string or array, ' . AeType::of($value) . ' given', 400); }
/** * Convert value to string * * Convert passed value to a valid php string * * @param string $name * @param mixed $value * @param AeXml_Node $node * * @return string */ protected function _valueToString($name, $value, AeInterface_Xml_Element $element) { if ($value instanceof AeType) { $value = $value->getValue(); } if (is_numeric($name)) { $child = $element->addChild('key')->setAttributes(array('key-name' => $name, 'key-type' => AeType::of($name))); } else { $child = $element->addChild($name); } if (is_array($value)) { foreach ($value as $k => $v) { $this->_valueToString($k, $v, $child); } } else { if (is_object($value)) { $child->setAttribute('type', 'object'); $value = serialize($value); $value = str_replace('&', '&', $value); $value = str_replace(chr(0), '&null;', $value); $child->setData($value); } else { $child->setAttribute('type', AeType::of($value)); if (is_bool($value)) { $value = $value ? 'true' : 'false'; } if (!is_null($value)) { $child->setData($value); } } } return $child; }
public function getArray($name, $default = array(), $callback = null) { $value = $this->_getClean($name); if (is_array($value)) { if (!empty($callback)) { if (AeType::of($callback) != 'string') { throw new AeInputException('Invalid callback value: expecting string, ' . AeType::of($callback) . ' given', 400); } $callback = (string) $callback; $method = 'get' . ucfirst($callback); if (!$this->methodExists($method)) { throw new AeInputException('Invalid callback value: method not supported', 404); } foreach ($value as $k => $v) { $key = $name . '.' . $k; if (is_array($v)) { $value[$k] = $this->getArray($key, $v, $callback); } else { $value[$k] = $this->call($method, array($key, $v)); } } } } else { $value = null; } return AeType::wrapReturn($value, $default); }
/** * Get Mime type * * Returns an image's Mime type to be used with the Content-Type header * * @uses AeImage::_getMime() * * @return string */ public function getMime() { if (!is_object($this->file)) { throw new AeImageException('No path value passed', 400); } return AeType::wrapReturn($this->_getMime($this->file->extension)); }
/** * Add child entity * * @throws AeXmlEntityElementException #400 on invalid value passed * * @param AeInterface_Xml_Entity|string $data entity or data to create * * @return AeInterface_Xml_Entity added child */ public function addData($data) { if (!$data instanceof AeInterface_Xml_Entity) { $type = AeType::of($data); if ($type != 'string') { throw new AeXmlEntityElementException('Invalid value passed: expecting XML entity or string, ' . $type . ' given', 400); } $data = new AeXml_Entity_Data($data); } return $this->addChild($data); }
/** * Call stored callback * * Call stored callback. This method accepts an infinite number of * parameters to be passed to the call. * * <b>NOTE:</b> For backwards compatibility with the {@link AeObject::call()} * method, a second parameter is introduced. If the <var>$args</var> parameter * is a string, it is used as the method name, and <var>$ma</var> is used * as an array of parameters for that method * * @see AeObject::call() * * @throws AeCallbackException #400 if no callback value is currently stored * @throws AeCallbackException #400 if arguments type is invalid * * @param array|string $args an array of parameters * @param array $ma * * @return mixed callback call result */ public function call($args = array(), $ma = array()) { $type = AeType::of($args); // *** Backwards compatibility with AeObject::call() if ($type == 'string' && $this->methodExists($name = (string) $args)) { return parent::call($name, $ma); } $callback = $this->getValue(false); if (!$callback) { throw new AeCallbackException('No callback value stored', 400); } if ($type != 'array') { throw new AeCallbackException('Invalid args type: expecting array, ' . $type . ' given', 400); } if (count($args) > 0) { return call_user_func_array($callback, $args); } else { if (count($this->_arguments) > 0) { return call_user_func_array($callback, $this->_arguments); } } return call_user_func($callback); }
/** * Set entity position * * Sets the entity's position to <var>$position</var>. If null is passed, * entity position is reset and parent is unassigned. * * @throws AeXmlEntityException #400 on invalid position value * @throws AeXmlEntityException #412 if no parent was assigned * * @param int|null $position * * @return AeInterface_Xml_Entity self */ public function setPosition($position) { if ($position instanceof AeType) { $position = $position->getValue(); } $type = AeType::of($position); if ($type != 'null') { if ($type != 'integer') { throw new AeXmlEntityException('Invalid value passed: expecting integer, ' . $type . ' given', 400); } if ($position < 0) { throw new AeXmlEntityException('Invalid value passed: position cannot be less than zero', 400); } if (!$this->hasParent()) { throw new AeXmlEntityException('Cannot set position: no parent assigned', 412); } } else { $this->_parent = null; } $this->_position = $position; return $this; }
public static function size($of, $human = false) { if ($of instanceof AeInterface_File) { return $of->getSize($human); } $type = AeType::of($of); if ($type != 'string') { throw new AeFileException('Invalid value passed: expecting file or path, ' . $type . ' given', 400); } if (!is_dir($of)) { $size = @filesize($of); if ($size === false) { $e = error_get_last(); throw new AeFileException('Delete failed:' . $e['message'], 500); } } else { $size = 0; foreach (scandir($of) as $name) { if ($name == '.' || $name == '..') { continue; } $size = self::size($of . SLASH . $name); } } if ($human === true) { // *** I doubt that values higher than GiB will occur, but let it stay $suffix = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'); $e = floor(log($size, 1024)); $size = round($size / pow(1024, $e), 2); if ($e > 0) { $size = number_format($size, 2, '.', ' '); } $size = $size . ' ' . $suffix[$e]; } return $size; }
/** * @todo finish method documentation * * @param object|array|string $name * @param mixed $value * * @return AeTemplate self */ public function assignEscape($name, $value = null) { if ($name instanceof AeArray || $name instanceof AeString) { $name = $name->getValue(); } // *** Assign from object if (is_object($name)) { foreach (get_object_vars($name) as $key => $value) { $this->assignEscape($key, $value); } return $this; } // *** Assign from array if (is_array($name)) { foreach ($name as $key => $value) { $this->assignEscape($key, $value); } return $this; } // *** Regular name => value assign if (is_string($name) && $value !== null) { // *** Get possible escape callbacks $args = @func_get_args(); $args = array_slice($args, 1); // *** Only assign if no conflict if ($this->propertyExists($name)) { throw new AeTemplateException('Cannot assign variable: name is reserved', 409); } $this->_properties[$name] = $this->call('escape', $args); return $this; } throw new AeTemplateException('Invalid value passed: expecting string, array or object, ' . AeType::of($name) . ' given', 400); }