/** * Constructor * * @param array $params * @throws Exception */ public function __construct(array $params) { Tebru\assert(isset($params['value']), new AnnotationConditionMissingException(sprintf('An argument was not passed to a "%s" annotation.', get_class($this)))); $this->value = $params['value']; if (isset($params['var'])) { $this->var = $params['var']; } }
/** * Decrypts data encrypted through encrypt() method * * @param string $data * @return mixed * @throws IvSizeMismatchException If the IV length has been altered * @throws MacHashMismatchException If the data has been altered */ public function decrypt($data) { // if this is not an encrypted string if (false === strpos($data, '|')) { return $data; } list($encryptedData, $mac, $iv) = $this->strategy->decodeData($data); Tebru\assert($mac === $this->strategy->getMac($encryptedData), new MacHashMismatchException('MAC hashes do not match')); Tebru\assert(strlen($iv) === $this->strategy->getIvSize(), new IvSizeMismatchException('IV size does not match expectation')); $serializedData = $this->strategy->decryptData($encryptedData, $iv); $decrypted = unserialize($serializedData); return $decrypted; }
/** * Constructor * * @param array $params * @throws OutOfRangeException */ public function __construct(array $params) { Tebru\assert(isset($params['value']), new AnnotationConditionMissingException(sprintf('An argument was not passed to a "%s" annotation.', get_class($this)))); // convert to array if (!is_array($params['value'])) { $params['value'] = [$params['value']]; } // loop through each string and break on ':' foreach ($params['value'] as $header) { $pos = strpos($header, ':'); Tebru\assert(false !== $pos, new AnnotationConditionMissingException('Header in an incorrect format. Expected "Name: value"')); $name = trim(substr($header, 0, $pos)); $value = trim(substr($header, $pos + 1)); $this->headers[$name] = $value; } }
/** * Create a new service * * @param string|object $service * @return object $service * @throws InvalidServiceTypeException */ public function create($service) { // if it's an object, we just want to return it if (is_object($service)) { return $service; } // if it's not a string, we don't know how to handle this type Tebru\assert(is_string($service), new InvalidServiceTypeException(sprintf('Could not create client. Expected object or string, got "%s"', gettype($service)))); // get the class as a string // if $service is already a class, use that, otherwise, if (class_exists($service)) { $class = $service; } elseif (interface_exists($service)) { $class = GeneratedClassMetaDataProvider::NAMESPACE_PREFIX . '\\' . $service; } else { throw new InvalidServiceTypeException(sprintf('Could not create client. "%s" should be a class or interface.', $service)); } return new $class($this->baseUrl, $this->httpClient, $this->serializer, $this->serializationContext, $this->deserializationContext); }
/** * Decodes data/iv and returns array * * @param string $data * @return array */ public function decodeData($data) { $decoded = explode('|', $data); Tebru\assert(3 === sizeof($decoded), new InvalidNumberOfEncryptionPieces('Encrypted string has been modified, wrong number of pieces found')); return [base64_decode($decoded[0]), base64_decode($decoded[1]), base64_decode($decoded[2])]; }
/** * Build the rest adapter * * @return RestAdapter * @throws BaseUrlMissingException */ public function build() { Tebru\assert(null !== $this->baseUrl, new BaseUrlMissingException(sprintf('Could not build RestAdapter with null $baseUrl'))); if (null === $this->httpClient) { $this->httpClient = new Client(); } if (null === $this->serializer) { $this->serializer = SerializerBuilder::create()->build(); } $adapter = new RestAdapter($this->baseUrl, $this->httpClient, $this->serializer, $this->serializationContext, $this->deserializationContext); return $adapter; }
/** * Generate a REST client based on an interface * * @param ClassMetaDataProvider $classMetaDataProvider * @param GeneratedClassMetaDataProvider $generatedClassMetaDataProvider * @return string */ public function generate(ClassMetaDataProvider $classMetaDataProvider, GeneratedClassMetaDataProvider $generatedClassMetaDataProvider) { // get interface methods $parsedMethods = $classMetaDataProvider->getInterfaceMethods(); // loop through class annotations $class = new ClassModel(); foreach ($classMetaDataProvider->getClassAnnotations() as $classAnnotation) { if ($classAnnotation instanceof Headers) { $class->addHeaders($classAnnotation->getHeaders()); } } // loop over class methods foreach ($classMetaDataProvider->getReflectionMethods() as $index => $classMethod) { $parsedMethod = array_shift($parsedMethods); $parameters = $classMethod->getParameters(); $method = new Method(); $method->setName($classMetaDataProvider->getMethodName($parsedMethod)); $method->setDeclaration($classMetaDataProvider->getMethodDeclaration($parsedMethod)); // loop through method annotations foreach ($classMetaDataProvider->getMethodAnnotations($classMethod) as $methodAnnotation) { if ($methodAnnotation instanceof HttpRequest) { foreach ($methodAnnotation->getParameters() as $parameter) { $this->assertParameter($parameters, $parameter); } } if ($methodAnnotation instanceof AnnotationToVariableMap) { $this->assertParameter($parameters, $methodAnnotation->getName()); } $handler = $this->annotationHandlerFactory->make($methodAnnotation); $handler->handle($method, $methodAnnotation); } $methodOptions = $method->getOptions(); $methodParts = $method->getParts(); Tebru\assert(null !== $method->getType(), new GenerationException('During Generation, the http method annotation was not found.')); Tebru\assert(empty($methodOptions['body']) || empty($methodParts), new GenerationException(sprintf('During Generation, both a @Body and @Part annotation were found on method "%s"', $method->getName()))); $class->addMethod($method); } // render class as string $template = $this->twig->loadTemplate('service.php.twig'); return $template->render(['uses' => $classMetaDataProvider->getUseStatements(), 'namespace' => $generatedClassMetaDataProvider->getNamespaceFull(), 'className' => $classMetaDataProvider->getInterfaceNameShort(), 'interfaceName' => $classMetaDataProvider->getInterfaceNameFull(), 'methods' => $class->getMethods()]); }
/** * Get the interface statement from the namespace statement * * @return Interface_ */ private function getInterface() { if (null !== $this->interface) { return $this->interface; } $interface = array_filter($this->namespace->stmts, function ($element) { return $element instanceof Interface_; }); Tebru\assert(1 === count($interface), new ClassParsingException('Could not get interface. $interface must be an array with one element')); $this->interface = array_shift($interface); return $this->interface; }
/** * @expectedException \OutOfRangeException * @expectedExceptionMessage My test message */ public function testAssertConditionChangeException() { $array = [0]; Tebru\assert(isset($array['key']), new \OutOfRangeException('My test message')); }