public function add(Torrent $torrent) { $id = $torrent->getId(); Assert::assert($id, 'id')->notNull(); if (!isset($this->torrents[$id])) { $this->torrents[$id] = $torrent; } }
/** * @param string $message * @param RequestInterface|null $request * @param ResponseInterface|null $response * @param \Exception|null $previous */ public function __construct(string $message, RequestInterface $request = null, ResponseInterface $response = null, \Exception $previous = null) { Assert::assert($message, 'message')->notEmpty(); $code = !is_null($response) && !$response instanceof PromiseInterface ? $response->getStatusCode() : 0; parent::__construct($message, $code, $previous); $this->request = $request; $this->response = $response; }
/** * @param mixed $givenValue * @param int[]|string[] $expectedValues * @throws \InvalidArgumentException */ public function __construct($givenValue, array $expectedValues) { Assert::assert($expectedValues, 'expectedValues')->notEmpty(); foreach ($expectedValues as $value) { if (!is_int($value) && !is_string($value)) { throw new \InvalidArgumentException(sprintf('Unexpected value in $expectedValues, allowed int, string. Given "%s"', gettype($value))); } } $message = sprintf('Value must be one from ["%s"]. Actual value: "%s"', implode('", "', $expectedValues), print_r($givenValue, true)); parent::__construct($message); }
/** * Добавить новую картинку в коллекцию * * @param $item array * @param $newImageKey string * @param $functionConvert * * @return array */ public function newImageInCollection($item, $newImageKey, $functionConvert) { Assert::assert($newImageKey, 'newImageKey')->notEmpty()->string(); $buf = unserialize($item[$this->collectionKey]); foreach ($buf as $key2 => &$elem) { if (file_exists($this->docRoot . $elem['path']['original'])) { $result = $functionConvert($this->docRoot . $elem['path']['original'], $this->config); $elem['path'][$newImageKey] = $result; } } $item[$this->collectionKey] = serialize($buf); return $item; }
public function testToStringWithInvalidTypeFixtures() { $name = 'variable'; $expectedMessage = (new InvalidNotArrayException($name))->getMessage(); try { Assert::assert([], $name)->toString(); $this->fail(sprintf('Not fail with: %s', $expectedMessage)); } catch (InvalidNotArrayException $error) { $this->assertSame($expectedMessage, $error->getMessage()); } }
/** * @param int $logoId * * @return Article * @throws \InvalidArgumentException */ public function setLogoId($logoId) { Assert::assert($logoId, 'logoId')->notEmpty()->positive()->int(); $this->logoId = $logoId; return $this; }
/** * @param string $method * @param string $url * @param array $queryParams * @return ResponseInterface */ private function call(string $method, string $url, array $queryParams = []) { if (!in_array($method, ['GET', 'POST'], true)) { throw new \DomainException(sprintf('Invalid or not supported request method "%s"', $method)); } Assert::assert($url)->notEmpty(); try { $response = $this->getGuzzleClient()->request($method, $url, $queryParams); } catch (GuzzleServerException $e) { throw new ServerException($e->getMessage(), $e->getRequest(), $e->getResponse(), $e); } catch (GuzzleClientException $e) { throw new ClientException($e->getMessage(), $e->getRequest(), $e->getResponse(), $e); } catch (GuzzleTransferException $e) { throw new ServerException($e->getMessage(), null, null, $e); } if ($response->getStatusCode() !== 200) { throw new ServerException(sprintf('Response status: %d "%s"', $response->getStatusCode(), $response->getReasonPhrase()), null, $response); } return $response; }
/** * @param string $path * * @return Article * @throws \InvalidArgumentException */ public function setPath($path) { Assert::assert($path, 'path')->notEmpty()->string(); $this->path = $path; return $this; }
public function __construct(string $message) { Assert::assert($message, 'message')->notEmpty(); parent::__construct($message); }
/** * @param string $password * * @return Article * @throws \InvalidArgumentException */ public function setPassword($password) { Assert::assert($password, 'password')->notEmpty()->string(); $this->password = $password; return $this; }
/** * @param string $argumentName * @param callable $func * @throws InvalidNotEmptyException * @throws InvalidNotObjectException * @throws InvalidStringException */ public function checkPositiveFloatArgument($argumentName, callable $func) { Assert::assert($argumentName, 'argumentName')->string()->notEmpty(); $fixture = -1.5; $expectedMessage = (new NumberNotPositiveException($argumentName, $fixture))->getMessage(); try { $func($fixture); /** @noinspection PhpUndefinedMethodInspection */ $this->fail('Not fail with: ' . $expectedMessage); } catch (NumberNotPositiveException $e) { /** @noinspection PhpUndefinedMethodInspection */ $this->assertSame($expectedMessage, $e->getMessage()); } }
/** * @param string $name * * @return Article * @throws \InvalidArgumentException */ public function setName($name) { Assert::assert($name, 'name')->notEmpty()->string(); $this->name = $name; return $this; }