public function testStrHasTags() { $cases = ['<' => false, '< a >' => false]; foreach ($cases as $case => $expect) { $this->assertSame($expect, \Owl\str_has_tags($case), $case); } }
protected function checkScalar($key, $value, array $rule) { if (isset($rule['same'])) { if ($value === $rule['same']) { return true; } throw $this->exception($key, sprintf('must strict equal [%s], current value is [%s]', $rule['same'], $value)); } elseif (isset($rule['eq'])) { if ($value == $rule['eq']) { return true; } throw $this->exception($key, sprintf('must equal [%s], current value is [%s]', $rule['eq'], $value)); } elseif (isset($rule['enum_same'])) { if (in_array($value, $rule['enum_same'], true)) { return true; } throw $this->exception($key, sprintf('must be strict equal one of [%s], current value is "%s"', implode(', ', $rule['enum_same']), $value)); } elseif (isset($rule['enum_eq'])) { if (in_array($value, $rule['enum_eq'])) { return true; } throw $this->exception($key, sprintf('must be equal one of [%s], current value is "%s"', implode(', ', $rule['enum_eq']), $value)); } elseif ($regexp = $rule['regexp']) { if (!preg_match($regexp, $value)) { throw $this->exception($key, sprintf('mismatch regexp %s, current value is "%s"', $regexp, $value)); } } if ($rule['type'] === 'boolean') { if (!is_bool($value)) { throw $this->exception($key, sprintf('must be TRUE or FALSE, current value is "%s"', $value)); } } elseif ($rule['type'] === 'integer' || $rule['type'] === 'numeric') { if ($value < 0 && !$rule['allow_negative']) { throw $this->exception($key, sprintf('not allow negative numeric, current value is "%s"', $value)); } if ($value == 0 && !$rule['allow_zero']) { throw $this->exception($key, sprintf('not allow zero, current value is "%s"', $value)); } } elseif (!$rule['allow_tags'] && \Owl\str_has_tags($value)) { throw $this->exception($key, sprintf('content not allow tags, current value is "%s"', $value)); } if ($callback = $rule['callback']) { if (!call_user_func_array($callback, [$value, $key, $rule])) { throw $this->exception($key, 'custom test failed'); } } return true; }
/** * 检查所有赋值的有效性. * * "fresh"对象会检查所有值 * 非"fresh"对象只检查修改过的值 * * @return true * * @throws \Owl\DataMapper\Exception\UnexpectedPropertyValueException */ public function validate() { $attributes = static::getMapper()->getAttributes(); $keys = $this->isFresh() ? array_keys($attributes) : array_keys($this->dirty); foreach ($keys as $key) { $attribute = $attributes[$key]; if ($attribute['auto_generate'] && $this->isFresh()) { continue; } $value = $this->get($key); $type = Type::factory($attribute['type']); if ($type->isNull($value)) { if (!$attribute['allow_null']) { throw new Exception\UnexpectedPropertyValueException(sprintf('%s: Property "%s", not allow null', get_class($this), $key)); } } else { if ($attribute['regexp'] && !preg_match($attribute['regexp'], $value)) { throw new Exception\UnexpectedPropertyValueException(sprintf('%s: Property "%s", mismatching pattern %s', get_class($this), $key, $attribute['regexp'])); } if (!$attribute['allow_tags'] && \Owl\str_has_tags($value)) { throw new Exception\UnexpectedPropertyValueException(sprintf('%s: Property "%s", cannot contain tags', get_class($this), $key)); } try { $type->validateValue($value, $attribute); } catch (\Exception $ex) { $message = sprintf('%s: Property "%s", %s', get_class($this), $key, $ex->getMessage()); throw new Exception\UnexpectedPropertyValueException($message, 0, $ex); } } } return true; }