public function parse($mapping) { $mapping = Assert::isString($mapping); $this->setOriginalMapping($mapping); $this->parseInternal($mapping, self::UNTIL_END); return $this->getMethods(); }
public function mappableItemAt($index) { $index = Assert::isString($index); if (!array_key_exists($index, $this->getModel()->getValue())) { throw new CatchableException('No Index could be found (available: ' . implode(', ', array_keys($this->getModel()->getValue())) . ')'); } return $this->getModel()->getValue()[$index]; }
/** * testIsFloat_negativeDecimal * @return void * @expectedException \InvalidArgumentException * @expectedExceptionMessage Must be Float: Array */ public function testIsFloat_array() { Assert::isFloat(['a']); }
/** * arrayFirst * @param array $value * @return mixed */ private function arrayFirst($value) { $value = Assert::isArray($value); foreach ($value as $part) { return $part; } return null; }
/** * Converts a string of any date to a timestamp * @param int|string|Date $date * @throws InvalidArgumentException * @return int */ public function parse($date) { if (is_int($date) || is_double($date) || is_string($date) && preg_match('@^[0-9]+$@', $date)) { return (double) $date; } elseif ($date instanceof Date) { return $date->getEpoch(); } elseif (is_null($date)) { return false; } else { $date = trim(Assert::isString($date)); } $date = $this->parseHoursMinutes($date); if (preg_match('@^[0-9]+$@', $date)) { return (int) $date; } $date = $this->stripTimezone($date); $date = $this->stripRange($date); if (preg_match('@^(\\d{4}-\\d{2}-\\d{2} )?24:00(:00)?$@', trim($date), $matches)) { if (isset($matches[1]) && strlen($matches[1]) > 0) { $date = strtotime($matches[1]); } else { $date = time(); } return mktime(0, 0, 0, date('n', $date), date('j', $date) + 1, date('Y', $date)); } //change a/p to am/pm $date = preg_replace('@(^|[^a-z])(a|p)([^a-z]|$)@i', '\\1\\2m\\3', $date); //Add a colon from 315am,1215 am $date = preg_replace('@(^|[^0-9])(\\d{1,2})(\\d{2})\\s*(am|pm)@i', '\\1\\2:\\3\\4', $date); //Fix for dashes instead of slashes $date = preg_replace('@(\\d{2})-(\\d{2})-(\\d{4})@', '\\1/\\2/\\3', $date); //Fix for dots instead of slashes $date = preg_replace('@(\\d{2})\\.(\\d{2})\\.(\\d{4})@', '\\1/\\2/\\3', $date); //Fix for UK/euro Version if (in_array(Formatter::getDefault()->getDateFormat(), array(Formatter::DATE_FORMAT_UK, Formatter::DATE_FORMAT_EURO))) { //Switch the d/m/Y to the US format m/d/Y to be able to parse $date = preg_replace('@(\\d{2})/(\\d{2})/(\\d{4})@', '\\2/\\1/\\3', $date); } switch (strtolower($date)) { case '9999-12-31': return 253402232400; break; case '0000-00-00': return 0; break; case '0000-00-00 00:00:00': return 0; break; case '24:00:00': case '24:00': return mktime(0, 0, 0, date('n'), date('j') + 1, date('Y')); break; case 'thanksgiving': $firstDay = mktime(0, 0, 0, 11, 1); $add = 26 - date('w', $firstDay); if ($add < 22) { $add += 7; } return mktime(0, 0, 0, 11, $add); break; default: return strtotime($date); } }
/** * call * @param bool|false $useOriginalObject * @return Boolean|Map|Number|ObjectInterface|String * @throws CatchableException * @throws \Exception */ public function call($useOriginalObject = false) { if ($this->getName() instanceof RawValue) { return $this->getName()->getValue(); } else { $currentObject = $useOriginalObject ? $this->getOriginalObject() : $this->getCurrentObject(); if ($this->getName() == '') { return $currentObject; } $mappableFields = $currentObject->getMappableFields(); Assert::isArray($mappableFields); $validMappings = []; foreach ($mappableFields as $mappableField) { $validMappings[] = $mappableField->getName(); if ($mappableField->getName() == $this->getName()) { try { $returnVar = $mappableField->handle($this->getParameters()); } catch (CatchableException $e) { $this->throwMappingException($e->getMessage()); } if (is_object($returnVar) && $returnVar instanceof ObjectInterface) { return $returnVar; } elseif ($mappableField->getReturnType() == ParseMethod::RETURN_STRING || is_null($returnVar)) { return new String($returnVar); } elseif (in_array($mappableField->getReturnType(), [ParseMethod::RETURN_DOUBLE, ParseMethod::RETURN_INT])) { return new Number($returnVar); } elseif (in_array($mappableField->getReturnType(), [ParseMethod::RETURN_DATE, ParseMethod::RETURN_DATETIME])) { return new Date($returnVar); } elseif ($mappableField->getReturnType() == ParseMethod::RETURN_BOOLEAN) { return new Boolean($returnVar); } elseif ($mappableField->getReturnType() == ParseMethod::RETURN_MAP) { return new Map($returnVar); } elseif ($mappableField->getReturnType() == ParseMethod::RETURN_MIXED) { return Root::createObject($returnVar); } else { throw new \Exception('To Implement: ' . $mappableField->getReturnType()); } } } $this->throwMappingException('Could not find a method with the name of: ' . $this->getName() . ' valid options are: ' . implode("\n", $validMappings)); } }
/** * validate * @param mixed $value * @return Date|DateTime|bool|float|int */ public function validate($value) { if ($this->isAsParsable()) { return $value; } else { $value = self::parseLexiconParameter($value); } switch ($this->getAllowedType()) { case Method::RETURN_BOOLEAN: $value = Assert::isBool($value); break; case Method::RETURN_DOUBLE: $value = Assert::isFloat($value); break; case Method::RETURN_INT: $value = Assert::isInt($value); break; case Method::RETURN_DATE: $value = new Date($value); break; case Method::RETURN_DATETIME: $value = new DateTime($value); break; } return $value; }
/** * String constructor. * @param bool $value */ public function __construct($value) { $value = Assert::isBool($value); $this->setValue($value); }
/** * Setter * @param int $weekOffset * @throws InvalidArgumentException */ public function setWeekOffset($weekOffset) { $weekOffset = Assert::isInt($weekOffset); if ($weekOffset > 6 || $weekOffset < 0) { throw new InvalidArgumentException('Week Offset must be between 0 and 6'); } $this->weekOffset = $weekOffset; }
/** * call * @param string $rawMapping * @param int $index * @param ObjectInterface $currentObject * @param string $currentMapping * @param array $parameters * @return ObjectInterface * @throws \Exception */ private function call($rawMapping, $index, ObjectInterface $currentObject, $currentMapping, array $parameters = []) { if ($currentMapping == '') { return $currentObject; } $mappableFields = $currentObject->getMappableFields(); Assert::isArray($mappableFields); $validMappings = []; foreach ($mappableFields as $mappableField) { $validMappings[] = $mappableField->getName(); if ($mappableField->getName() == $currentMapping) { try { $returnVar = $mappableField->handle($parameters); } catch (CatchableException $e) { $this->throwMappingException($e->getMessage(), $currentMapping, $index); } if (is_object($returnVar) && $returnVar instanceof ObjectInterface) { return $returnVar; } elseif ($mappableField->getReturnType() == Method::RETURN_STRING || is_null($returnVar)) { return new String($returnVar); } elseif (in_array($mappableField->getReturnType(), [Method::RETURN_DOUBLE, Method::RETURN_INT])) { return new Number($returnVar); } elseif (in_array($mappableField->getReturnType(), [Method::RETURN_DATE, Method::RETURN_DATETIME])) { return new Date($returnVar); } elseif ($mappableField->getReturnType() == Method::RETURN_BOOLEAN) { return new Boolean($returnVar); } elseif ($mappableField->getReturnType() == Method::RETURN_MAP) { return new Map($returnVar); } else { throw new \Exception('To Implement: ' . $mappableField->getReturnType()); } } } $this->throwMappingException('Could not find a method with the name of: ' . $currentMapping . ' valid options are: ' . implode("\n", $validMappings), $rawMapping, $index); }