/** * Create a file access mode instance based on a PHP file access modes string. * This method won't verify the validity of the file access mode string. * * @param string $mode PHPs file access mode string. * * @return FileAccessMode|null The file access mode instance, or null on failure. */ public static function createFromMode($mode) { // Return $mode if it's an FileAccessMode instance if ($mode instanceof FileAccessMode) { return $mode; } // Create and return a new file access mode instance return new FileAccessMode(StringUtils::contains($mode, '+'), !StringUtils::contains($mode, 'r') || StringUtils::contains($mode, '+'), !StringUtils::contains($mode, 'r'), StringUtils::contains($mode, 'x'), StringUtils::contains($mode, 'w'), StringUtils::contains($mode, 'a'), StringUtils::contains($mode, 't'), StringUtils::contains($mode, 'b')); }
public function equals($other, $sameType = true) { // Make sure the types are equal if ($sameType && !(get_class() === get_class($other))) { return false; } // Convert $other into a string, return false if failed if (($other = FilesystemObjectHelper::asPath($other, false)) === null) { return false; } // Compare the paths, return the result return StringUtils::equals($this->getPath(), $other, false, true); }
/** * List files and directories in the directory. * * @param int $sortOrder [optional] The order of the entries being returned. Using alphabetical order by default. * Order types: * - SCANDIR_SORT_ASCENDING * - SCANDIR_SORT_DESCENDING * - SCANDIR_SORT_NONE * @param resource $context [optional] The directory context. See PHPs scandir() function for more information. * @param bool $ignorePeriodDirs [optional] True to ignore period dirs such as /. and /.., false otherwise. * * @return Array|null A list of filesystem objects as an array or null on failure. * * @see scandir(); */ public function scan($sortOrder = SCANDIR_SORT_ASCENDING, $context, $ignorePeriodDirs = false) { // Make sure the directory handle was opened if (!$this->isOpened()) { return null; } // Scan the directory if ($context !== null) { $scan = scandir($this->handle, $sortOrder, $context); } else { $scan = scandir($this->handle, $sortOrder); } // Make sure the result was valid if ($scan === false) { return null; } // Create an array of filesystem objects $entries = array(); foreach ($scan as $entry) { // Check whether period directories should be ignored if ($ignorePeriodDirs && StringUtils::equals($entry, array('.', '..'), false, true)) { continue; } $entries[] = FileSystemObject::from($this->dir, $entry); } // Return the result return $entries; }
/** * Check if it's the birthday. This check whether the month and day are equal to the specified date and time. * * @param DateTime|PHPDateTime|string|null $birthday [optional] The DateTime or PHPDateTime instance, the date and * time as a string or null to use the now() date and time. * * @return boolean True if it's the birthday, false otherwise. False is also returned on failure. */ public function isBirthday($birthday) { // Parse the date and time, return null on failure if (($birthday = static::parse($birthday, $this->getTimezone())) === null) { return null; } // Check whether the month and day are equal, return the result return StringUtils::equals($this->format('md'), $birthday->format('md')); }
/** * Get the value as a boolean. * * @return bool Registry value. * * @throws Exception Throws an exception if an error occurred. */ public function getBoolValue() { // Get the value as a string $value = $this->getValue(); // Parse and return the value if (StringUtils::equals($value, array('true', 't', 'yes', 'y'), false, true)) { return true; } if (StringUtils::equals($value, array('false', 'f', 'no', 'n'), false, true)) { return false; } // Try to cast the value to a boolean, return the result return (bool) $value; }
/** * Set a property of the date interval object. * * @param string $name The name of the property to set. * @param int $value The value to set the property to. * * @return static The DateTime instance. * * @throws InvalidArgumentException Throw an exception on failure. */ public function __set($name, $value) { // Set the number of years if (StringUtils::equals($name, 'intervalYears', true)) { return $this->setYears($value); } // Set the number of months if (StringUtils::equals($name, 'intervalMonths', true)) { return $this->setMonths($value); } // Set the number of weeks if (StringUtils::equals($name, 'intervalWeeks', true)) { return $this->setWeeks($value); } // Set the number of days if (StringUtils::equals($name, 'intervalDays', true)) { return $this->setDays($value); } // Set the number of hours if (StringUtils::equals($name, 'intervalHours', true)) { return $this->setHours($value); } // Set the number of minutes if (StringUtils::equals($name, 'intervalMinutes', true)) { return $this->setMInutes($value); } // Set the number of seconds if (StringUtils::equals($name, 'intervalSeconds', true)) { return $this->setSeconds($value); } // Unknown property setter, throw an exception throw new InvalidArgumentException('Unknown property setter \'' . $name . '\''); }
/** * Get all language files in the language directory. * * @return array Language files. * * @throws Exception Throws if an error occurred. */ private static function getLanguageFiles() { // Get the language directory and make sure it exists $langDir = static::getLanguageDirectory(); if (!$langDir->isDirectory()) { throw new Exception('Language file directory doesn\'t exist, or isn\'t accessible!'); } // Create a directory scanner to list all language files $scanner = new DirectoryScanner($langDir); // Define an array for the language files $langFiles = array(); // Process each file in this directory while (($file = $scanner->read()) !== null) { // Make sure this file is a file if (!$file->isFile()) { continue; } // Make sure the extension is valid if (!StringUtils::equals($file->getExtension(false), 'ini', false, true)) { continue; } // Add the file to the language files list $langFiles[] = $file; } // Return the list of files return $langFiles; }
/** * Compare the file access mode with an other file access mode instance. * * @param FileAccessMode|string $other The other file access mode instance to compare this instance to. A file * access mode string may be supplied which causes this method to compare the two file access modes as strings. * @param bool $exact [optional] True to ensure the two instances exactly equal each other with all of their * properties, false to just compare the file access mode strings of both instances since different instances may * share the same file access mode string. This option is only available if $other was a FileAccessMode instance. * * @return bool True if the two instances equal, false otherwise. False will also be returned if the other instance * is invalid. */ public function equals($other, $exact = false) { // Make sure $other isn't null if ($other === null) { return false; } // Directly compare the two instances if ($this === $other) { return true; } // Compare the two instances as a string, if $other is a string. if (is_string($other)) { return StringUtils::equals($this->getMode(), $other, false, true); } // Compare the PHPs file access mode strings if the comparison doesn't have to be exact. if (!$exact) { return StringUtils::equals($this->getMode(), $other->getMode(), false); } // The two instances doesn't seem to equal, return false return false; }
/** * Check whether this timezone equals another timezone. * * @param DateTimeZone|PHPDateTimeZone|string|DateTime|PHPDateTime $timezone The other timezone as DateTimeZone or * PHPDateTimeZone instance, or the timezone ID as a string. A DateTime or PHPDateTime instance may be supplied * to use it's timezone. * * @return bool True if the timezones equal, false if not. False will also be returned if an error occurred. */ public function equals($timezone) { // Parse the timezone if (($timezone = static::parse($timezone)) === null) { return false; } // Compare the timezone IDs of both instances, return the result return StringUtils::equals($this->getName(), $timezone->getName(), false); }
<?php use carbon\core\util\StringUtils; // Initialize the app require_once '../app/init.php'; // Get the session ID if ($sessionId == null) { $sessionId = getSessionKey(); } // Make sure the session ID is valid if (StringUtils::equals($sessionId, '', true, true)) { returnError('Session error.'); } /** * Return an error. * * @param string $msg The message * @return string */ function returnError($msg) { // Return the error as JSON returnJson(array('error' => $msg)); } /** * Return an array with data as JSON. * * @param array $array The array to return as JSON. */ function returnJson($array) {