Пример #1
0
 public function make(Str $name, Str $value) : HeaderInterface
 {
     if ((string) $name->toLower() !== 'content-type') {
         throw new InvalidArgumentException();
     }
     $matches = $value->getMatches('~(?<type>[\\w*]+)/(?<subType>[\\w*]+)(?<params>(; ?\\w+=\\"?[\\w\\-.]+\\"?)+)?~');
     return new ContentType(new ContentTypeValue((string) $matches->get('type'), (string) $matches->get('subType'), $this->buildParams($matches->hasKey('params') ? $matches->get('params') : new Str(''))));
 }
Пример #2
0
 public function make(Str $name, Str $value) : HeaderInterface
 {
     if ((string) $name->toLower() !== 'content-language') {
         throw new InvalidArgumentException();
     }
     $values = new Set(HeaderValueInterface::class);
     foreach ($value->split(',') as $language) {
         $values = $values->add(new ContentLanguageValue((string) $language->trim()));
     }
     return new ContentLanguage($values);
 }
Пример #3
0
 public function make(Str $name, Str $value) : HeaderInterface
 {
     if ((string) $name->toLower() !== 'allow') {
         throw new InvalidArgumentException();
     }
     $values = new Set(HeaderValueInterface::class);
     foreach ($value->split(',') as $allow) {
         $values = $values->add(new AllowValue((string) $allow->trim()->toUpper()));
     }
     return new Allow($values);
 }
Пример #4
0
 public function make(Str $name, Str $value) : HeaderInterface
 {
     if ($this->factories->contains((string) $name->toLower())) {
         return $this->factories->get((string) $name->toLower())->make($name, $value);
     }
     $values = new Set(HeaderValueInterface::class);
     foreach ($value->split(',') as $headerValue) {
         $values = $values->add(new HeaderValue((string) $headerValue->trim()));
     }
     return new Header((string) $name, $values);
 }
Пример #5
0
 public function make(Str $name, Str $value) : HeaderInterface
 {
     if ((string) $name->toLower() !== 'accept-language') {
         throw new InvalidArgumentException();
     }
     $values = new Set(HeaderValueInterface::class);
     foreach ($value->split(',') as $accept) {
         $matches = $accept->getMatches('~(?<lang>([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*|\\*))(; ?q=(?<quality>\\d+(\\.\\d+)?))?~');
         $values = $values->add(new AcceptLanguageValue((string) $matches->get('lang'), new Quality($matches->hasKey('quality') ? (double) (string) $matches->get('quality') : 1)));
     }
     return new AcceptLanguage($values);
 }
Пример #6
0
 public function make(Str $name, Str $value) : HeaderInterface
 {
     if ((string) $name->toLower() !== 'accept') {
         throw new InvalidArgumentException();
     }
     $values = new Set(HeaderValueInterface::class);
     foreach ($value->split(',') as $accept) {
         $matches = $accept->getMatches('~(?<type>[\\w*]+)/(?<subType>[\\w*]+)(?<params>(; ?\\w+=\\"?[\\w\\-.]+\\"?)+)?~');
         $values = $values->add(new AcceptValue((string) $matches->get('type'), (string) $matches->get('subType'), $this->buildParams($matches->hasKey('params') ? $matches->get('params') : new Str(''))));
     }
     return new Accept($values);
 }
Пример #7
0
 public function make(Str $name, Str $value) : HeaderInterface
 {
     if ((string) $name->toLower() !== 'accept-charset') {
         throw new InvalidArgumentException();
     }
     $values = new Set(HeaderValueInterface::class);
     foreach ($value->split(',') as $accept) {
         $matches = $accept->getMatches('~(?<charset>[a-zA-Z0-9\\-_:\\(\\)]+)(; ?q=(?<quality>\\d+(\\.\\d+)?))?~');
         $values = $values->add(new AcceptCharsetValue((string) $matches->get('charset'), new Quality($matches->hasKey('quality') ? (double) (string) $matches->get('quality') : 1)));
     }
     return new AcceptCharset($values);
 }
Пример #8
0
 public function make(Str $name, Str $value) : HeaderInterface
 {
     if ((string) $name->toLower() !== 'cache-control') {
         throw new InvalidArgumentException();
     }
     $splits = $value->split(',');
     $values = new Set(HeaderValueInterface::class);
     foreach ($splits as $split) {
         $split = $split->trim();
         switch (true) {
             case $split->match('~^max-age=\\d+$~'):
                 $values = $values->add(new CacheControlValue\MaxAge((int) (string) $split->substring(8)));
                 break;
             case $split->match('~^max-stale(=\\d+)?$~'):
                 $values = $values->add(new CacheControlValue\MaxStale($split->length() > 10 ? (int) (string) $split->substring(10) : 0));
                 break;
             case $split->match('~^min-fresh=\\d+$~'):
                 $values = $values->add(new CacheControlValue\MinimumFresh((int) (string) $split->substring(10)));
                 break;
             case (string) $split === 'must-revalidate':
                 $values = $values->add(new CacheControlValue\MustRevalidate());
                 break;
             case $split->match('~^no-cache(="?\\w+"?)?$~'):
                 $matches = $split->getMatches('~^no-cache(="?(?<field>\\w+)"?)?$~');
                 $values = $values->add(new CacheControlValue\NoCache($matches->hasKey('field') ? (string) $matches->get('field') : ''));
                 break;
             case (string) $split === 'no-store':
                 $values = $values->add(new CacheControlValue\NoStore());
                 break;
             case (string) $split === 'no-transform':
                 $values = $values->add(new CacheControlValue\NoTransform());
                 break;
             case (string) $split === 'only-if-cached':
                 $values = $values->add(new CacheControlValue\OnlyIfCached());
                 break;
             case $split->match('~^private(="?\\w+"?)?$~'):
                 $matches = $split->getMatches('~^private(="?(?<field>\\w+)"?)?$~');
                 $values = $values->add(new CacheControlValue\PrivateCache($matches->hasKey('field') ? (string) $matches->get('field') : ''));
                 break;
             case (string) $split === 'proxy-revalidate':
                 $values = $values->add(new CacheControlValue\ProxyRevalidate());
                 break;
             case (string) $split === 'public':
                 $values = $values->add(new CacheControlValue\PublicCache());
                 break;
             case $split->match('~^s-maxage=\\d+$~'):
                 $values = $values->add(new CacheControlValue\SharedMaxAge((int) (string) $split->substring(9)));
                 break;
         }
     }
     return new CacheControl($values);
 }
Пример #9
0
 public function make(Str $name, Str $value) : HeaderInterface
 {
     if ((string) $name->toLower() !== 'link') {
         throw new InvalidArgumentException();
     }
     $links = new Set(HeaderValueInterface::class);
     foreach ($value->split(',') as $link) {
         $matches = $link->trim()->getMatches('~^<(?<url>\\S+)>(?<params>(; ?\\w+=\\"?[\\w\\-.]+\\"?)+)?$~');
         $params = $this->buildParams($matches->hasKey('params') ? $matches->get('params') : new Str(''));
         $links = $links->add(new LinkValue(Url::fromString((string) $matches->get('url')), $params->contains('rel') ? $params->get('rel')->value() : 'related', $params->contains('rel') ? $params->remove('rel') : $params));
     }
     return new Link($links);
 }
Пример #10
0
 public function parse(RequestInterface $request, ResponseInterface $response, MapInterface $attributes) : MapInterface
 {
     $start = $this->clock->now();
     $languages = null;
     if (!$this->isHtml($attributes)) {
         return $attributes;
     }
     $document = $this->reader->read($response->body());
     try {
         $html = (new Element('html'))($document);
         if ($html->attributes()->contains('lang')) {
             $languages = $html->attributes()->get('lang');
         }
     } catch (ElementNotFoundException $e) {
         //pass
     }
     if (!$languages instanceof AttributeInterface) {
         try {
             $metas = (new Elements('meta'))((new Head())($document))->filter(function (ElementInterface $element) : bool {
                 return $element->attributes()->contains('http-equiv') && $element->attributes()->contains('content');
             })->filter(function (ElementInterface $meta) : bool {
                 $header = $meta->attributes()->get('http-equiv')->value();
                 $header = new Str($header);
                 return (string) $header->toLower() === 'content-language';
             });
             if ($metas->size() === 1) {
                 $languages = $metas->current()->attributes()->get('content');
             }
         } catch (ElementNotFoundException $e) {
             //pass
         }
     }
     if (!$languages instanceof AttributeInterface) {
         return $attributes;
     }
     $languages = $this->parseAttribute($languages);
     if ($languages->size() === 0) {
         return $attributes;
     }
     return $attributes->put(self::key(), new Attribute(self::key(), $languages, $this->clock->now()->elapsedSince($start)->milliseconds()));
 }
Пример #11
0
class Str
{
    protected $string;
    public function __construct($string)
    {
        $this->string = $string;
    }
    public function toUpper()
    {
        $this->string = strtoupper($this->string);
    }
    public function toLower()
    {
        $this->string = strtolower($this->string);
    }
    public function get()
    {
        return $this->string;
    }
    public function explode($delimiter = " ")
    {
        return explode($delimiter, $this->string);
    }
}
$str = new Str('Hello World');
$str->toUpper();
$str->toLower();
echo $str->get();
$array = $str->explode("l");
echo "array<pre>" . print_r($array, 1) . "</pre>";