protected function validate(\ICanboogie\Errors $errors) { global $core; $request = $this->request; $token = $request['token']; if (!$token) { $errors['token'] = $this->t("The nonce login Token is required."); return false; } $this->ticket = $ticket = $core->models['users.noncelogin']->filter_by_token($token)->one; if (!$ticket) { $errors['token'] = $this->t("Unknown token."); return false; } if ($ticket->expire_at < DateTime::now()) { $errors['expire_at'] = $this->t("This nonce login ticket has expired at :date.", array(':date' => $ticket->expire_at->local->as_db)); return false; } if ($ticket->ip != $request->ip) { $errors['ip'] = $this->t("The IP address doesn't match the one of the initial request."); return false; } try { $ticket->user; } catch (RecordNotFound $e) { $errors['uid'] = $this->t("The user associated with this nonce login no longer exists."); return false; } return true; }
/** * Returns the {@link DateTime} instance of a property. * * @param mixed $property Reference to the property to return. * * @return DateTime The function always return a {@link DateTime} instance. */ public static function get(&$property) { if ($property instanceof DateTime) { return $property; } return $property = $property === null ? DateTime::none() : new DateTime($property, 'utc'); }
public function test_expire_at() { $t = new Ticket(); $d = $t->expire_at; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertTrue($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('0000-00-00 00:00:00', $d->as_db); $t->expire_at = '2013-03-07 18:30:45'; $d = $t->expire_at; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertFalse($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('2013-03-07 18:30:45', $d->as_db); $t->expire_at = new DateTime('2013-03-07 18:30:45', 'utc'); $d = $t->expire_at; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertFalse($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('2013-03-07 18:30:45', $d->as_db); $t->expire_at = null; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $t->expire_at = DateTime::now(); $properties = $t->__sleep(); $this->assertArrayHasKey('expire_at', $properties); $array = $t->to_array(); $this->assertArrayHasKey('expire_at', $array); }
/** * Adds the `status` and `notify` properties if they are not defined, they default to * `pending` and `no`. * * @throws \InvalidArgumentException if the value of the `notify` property is not one of `no`, * `yes`, `author` or `done`. */ public function save(array $properties, $key = null, array $options = array()) { $properties += array(Comment::CREATED => DateTime::now(), Comment::STATUS => 'pending', Comment::NOTIFY => 'no'); if (!in_array($properties[Comment::NOTIFY], array('no', 'yes', 'author', 'done'))) { throw new \InvalidArgumentException(\ICanBoogie\format('Invalid value for property %property: %value', array('%property' => Comment::NOTIFY, '%value' => $properties[Comment::NOTIFY]))); } return parent::save($properties, $key, $options); }
/** * Returns the expire date. * * @return \ICanBoogie\DateTime */ protected function volatile_get_expire_at() { $datetime = $this->expire_at; if ($datetime instanceof DateTime) { return $datetime; } return $this->expire_at = $datetime === null ? DateTime::none() : new DateTime($datetime, 'utc'); }
protected function lazy_get_properties() { $properties = parent::lazy_get_properties(); if (!$this->key) { $properties['created'] = DateTime::now(); } return $properties; }
protected function lazy_get_properties() { $properties = parent::lazy_get_properties(); unset($properties['user_id']); if (!$this->key) { $properties['user_id'] = $this->app->user->id; $properties['created'] = DateTime::now(); } return $properties; }
/** * @param string|int|\DateTime $time If time is provided as a numeric value it is used as * "@{$time}" and the time zone is set to UTC. * @param \DateTimeZone|string $timezone A {@link \DateTimeZone} object representing the desired * time zone. If the time zone is empty `utc` is used instead. */ public function __construct($time = 'now', $timezone = null) { if ($time instanceof \DateTime) { $time = $time->getTimestamp(); } if (is_numeric($time)) { $time = '@' . $time; $timezone = null; } parent::__construct($time, $timezone ?: 'utc'); }
/** * Overrides the method to handle the following properties: * * `uid`: Only users with the PERMISSION_ADMINISTER permission can choose the user of records. * If the user saving a record has no such permission, the Node::UID property is removed from * the properties created by the parent method. * * `siteid`: If the user is creating a new record or the user has no permission to choose the * record's site, the property is set to the value of the working site's id. * * Also, the following default values are used: * * - `uid`: 0 * - `nativeid`: 0 * - `language`: an empty string */ protected function lazy_get_properties() { global $core; $properties = parent::lazy_get_properties() + [Node::UID => 0, Node::NATIVEID => 0, Node::LANGUAGE => '']; $user = $core->user; if (!$user->has_permission(Module::PERMISSION_ADMINISTER, $this->module)) { unset($properties[Node::UID]); } if (!$this->key || !$user->has_permission(Module::PERMISSION_MODIFY_BELONGING_SITE)) { $properties[Node::SITEID] = $core->site_id; } $properties[Node::UPDATED_AT] = DateTime::now(); return $properties; }
/** * @inheritdoc */ public function cast_value($value, $type = null) { if ($value instanceof \DateTimeInterface) { $value = DateTime::from($value); return $value->utc->as_db; } if ($value === false) { return 0; } if ($value === true) { return 1; } return $value; }
public function test_process() { $request = Request::from(['is_post' => true, 'request_params' => [Operation::DESTINATION => 'nodes', Operation::NAME => 'save', 'title' => "My Example"]]); $operation = new FakeSaveOperation(); $response = $operation($request); $record = $operation->record; $this->assertEmpty($record->uid); $this->assertEquals(1, $record->siteid); $this->assertNotEmpty($record->uuid); $this->assertEquals("My Example", $record->title); $this->assertEquals("my-example", $record->slug); $this->assertEquals("nodes", $record->constructor); $this->assertEmpty($record->is_online); $this->assertEmpty($record->nativeid); $this->assertEquals(DateTime::now()->utc, $record->created_at); $this->assertEquals(DateTime::now()->utc, $record->updated_at); }
public static function markup_time_ago(array $args, $engine, $template) { static $properties = ['y' => "year", 'm' => "month", 'd' => "day", 'h' => "hour", 'i' => "minute", 's' => "second"]; $datetime = $args['select']; if (!$datetime) { return null; } $diff = DateTime::now()->diff($datetime); foreach ($properties as $property => $name) { $value = $diff->{$property}; if (!$value) { continue; } if ($property == 's' && $value < 60) { $str = "Just now"; } else { $str = $value == 1 ? "one {$name} ago" : "{$value} " . \ICanBoogie\pluralize($name) . " ago"; } return $template ? $engine($template, $str) : $str; } }
protected function alter_persistent_properties(array $properties, \ICanBoogie\ActiveRecord\Model $model) { $properties = parent::alter_persistent_properties($properties, $model); if (!$this->result_id && $properties['created_at']->is_empty) { $properties['created_at'] = DateTime::now(); } if ($properties['updated_at']->is_empty) { $properties['updated_at'] = DateTime::now(); } return $properties; }
public function test_create_at_MUST_be_modified_during_save() { $node = new Node(); $node->title = "Example"; $this->assertTrue($node->updated_at->is_empty); $node->save(); $now = DateTime::now()->utc; $this->assertFalse($node->created_at->is_empty); $this->assertEquals($now, $node->created_at); $node->updated_at = '-2 week'; $node->save(); $this->assertEquals($now, $node->created_at); $node->delete(); }
/** * If the date returned by the parent is empty the method returns a date created from * {@link DEFAULT_EXPIRES}. * * @return DateTime|Headers\Date */ protected function get_expires() { $expires = parent::get_expires(); if (!$expires->is_empty) { return $expires; } return DateTime::from(self::DEFAULT_EXPIRES); }
protected function validate(\ICanboogie\Errors $errors) { global $core; $email = $this->request['email']; if (!$email) { $errors['email'] = new FormattedString('The field %field is required!', array('%field' => 'Votre adresse E-Mail')); return false; } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors['email'] = new FormattedString("Invalid email address: %email.", array('%email' => $email)); return false; } $user = $this->record; if (!$user) { $errors['email'] = new FormattedString("Unknown email address."); return false; } if ($user->language) { $core->locale = $user->language; } $expire_at = null; $ticket = $this->module->model->filter_by_uid($user->uid)->one; if ($ticket) { $expire_at = $ticket->expire_at; } if ($expire_at && time() + Module::FRESH_PERIOD - $expire_at->timestamp < Module::COOLOFF_DELAY) { throw new PermissionRequired(new FormattedString("nonce_login_request.operation.already_sent", array(':time' => DateTime::from('@' . ($expire_at->timestamp - Module::FRESH_PERIOD + Module::COOLOFF_DELAY), 'utc')->local->format('H:i'))), 403); } return true; }
public function test_date() { $r = new Content(); $d = $r->date; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertTrue($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('0000-00-00 00:00:00', $d->as_db); $r->date = '2013-03-07 18:30:45'; $d = $r->date; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertFalse($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('2013-03-07 18:30:45', $d->as_db); $r->date = new DateTime('2013-03-07 18:30:45', 'utc'); $d = $r->date; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $this->assertFalse($d->is_empty); $this->assertEquals('UTC', $d->zone->name); $this->assertEquals('2013-03-07 18:30:45', $d->as_db); $r->date = null; $this->assertInstanceOf('ICanBoogie\\DateTime', $d); $r->date = DateTime::now(); $properties = $r->__sleep(); $this->assertArrayHasKey('date', $properties); $array = $r->to_array(); $this->assertArrayHasKey('date', $array); }
/** * Parse the conditions for the {@link where()} and {@link having()} methods. * * {@link \DateTimeInterface} conditions are converted to strings. * * @return array An array made of the condition string and its arguments. */ private function deferred_parse_conditions() { $args = debug_backtrace(0, 2)[1]['args']; $conditions = array_shift($args); if (is_array($conditions)) { $c = ''; $conditions_args = []; foreach ($conditions as $column => $arg) { if (is_array($arg) || $arg instanceof self) { $joined = ''; if (is_array($arg)) { foreach ($arg as $value) { $joined .= ',' . (is_numeric($value) ? $value : $this->model->quote($value)); } $joined = substr($joined, 1); } else { $joined = (string) $arg; $conditions_args = array_merge($conditions_args, $arg->args); } $c .= ' AND `' . ($column[0] == '!' ? substr($column, 1) . '` NOT' : $column . '`') . ' IN(' . $joined . ')'; } else { $conditions_args[] = $arg; $c .= ' AND `' . ($column[0] == '!' ? substr($column, 1) . '` !' : $column . '` ') . '= ?'; } } $conditions = substr($c, 5); } else { $conditions_args = []; if ($args) { if (is_array($args[0])) { $conditions_args = $args[0]; } else { # # We dereference values otherwise the caller would get a corrupted array. # foreach ($args as $key => $value) { $conditions_args[$key] = $value; } } } } foreach ($conditions_args as &$value) { if ($value instanceof \DateTimeInterface) { $value = DateTime::from($value)->utc->as_db; } } return [$conditions ? '(' . $conditions . ')' : null, $conditions_args]; }