Learning Swift2 with PHP7

8e528456ff66ec543952daa815353a01?s=47 Norio Suzuki
November 24, 2015

Learning Swift2 with PHP7

Lightning Talks Driven Learning.

This talk for my study.

Perhaps it is not interesting for you☻

8e528456ff66ec543952daa815353a01?s=128

Norio Suzuki

November 24, 2015
Tweet

Transcript

  1. Learning Swift2 with PHP7 PHP BLT #1 2015-11-24 @suzuki

  2. Lightning Talks Driven Learning This talk for my study.
 Perhaps

    it is not interesting for you☻
  3. Language Specifications PHP7 : Swift2

  4. language version % php --version PHP 7.0.0RC6 (cli) (built: Oct

    29 2015 13:49:53) ( NTS ) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies % swift --version Apple Swift version 2.1 (swiftlang-700.1.101.6 clang-700.1.76) Target: x86_64-apple-darwin14.5.0
  5. comment <?php /* * This is a comment. */ //

    This is also a comment. # This is comment, too. // /* // * /* Nested comment not work. */ // */ /* * This is a comment. */ // This is also a comment. // "#" is not a comment syntax. /* * /* Nested comment works. */ */
  6. type <?php $intVar = 123; $floatVar = 1.23; $doubleQoute =

    "string"; $singleQuote = 'string'; $arrayVar = [1, 2, 3]; $hashVar = ['a' => 1, 'b' => 2]; echo $hashVar['a']; var intVar1 = 123 var intVar2:Int = 123 var floatVar1 = 1.23 var floatVar2:Float = 1.23 var doubleQuote1 = "string" var doubleQuote2:String = "string" // single quote is not work //var singleQuote1 = 'string' //var singleQuote2:String = 'string' var arrayVar = [1, 2, 3] var dic:Dictionary<String, Int> = ["a":1, "b":2] // "!" is cast for Optional tyep print(dic["a"]!)
  7. access <?php define('GLOBAL_CONST', 'global const'); $global = 'global var'; class

    ExampleClass { private $privateVar = 'private var'; protected $protectedVar = 'protected var'; public $publicVar = 'public var'; const CLASS_CONST = 'class const'; } // Behavior is different from PHP // These comparison is not correct. let GLOBAL_CONST = "global const" var global = "global var" public class ExampleClass { private var privateVar = "private var" internal var internalVar = "internal var" public var publicVar = "public var" private let privateConst = "private const" internal let internalConst = "inetrnal const" public let publicConst = "public const" } private class PrivateClass {} internal class InternalClass {} public class PublicClass {}
  8. operator <?php $intA = 1; $intB = 2; $intC =

    $intA + $intB; echo $intC; $strA = "a"; $strB = "b"; $strC = $strA . $strB; echo $strC; $nullVar = null; $notNullVar = 'not null'; $result = $nullVar ?? $notNullVar; // PHP7 echo $result; var intA = 1 var intB = 2 var intC = intA + intB print(intC) var strA = "a" var strB = "b" var strC = strA + strB print(strC) var nilVar:String? = nil var notNilVar = "not nil" var result = nilVar ?? notNilVar print(result)
  9. if <?php $a = 123; $b = 456; if ($a

    > $b) { } elseif ($a < $b) { } else { } var a = 123 var b = 456 // Parens can be ommitted. if a > b { } else if a < b { } else { }
  10. switch <?php $foo = 1; $bar = ''; switch ($foo)

    { case 1: $bar = 'a'; break; case 2: $bar = 'b'; break; default: $bar = 'c'; } var foo = 1 var bar = "" switch foo { case 1: bar = "a" // no need "break" case 2: bar = "b" default: bar = "c" }
  11. for <?php $array = []; for ($i = 0; $i

    < 10; $i++) { $array[] = $i; } foreach ($array as $tmp) { echo $tmp; } var array:[Int] = []; for i in 0..<10 { array.append(i) } for tmp in array { print(tmp); }
  12. while <?php $max = 3; $counter1 = 0; $counter2 =

    0; while ($counter1 < $max) { echo 'Hi!'; $counter1++; } do { echo 'Ho!'; $counter2++; } while ($counter2 < $max); let max = 3; var counter1 = 0; var counter2 = 0; while counter1 < max { print("Hi!") counter1++ } repeat { print("ho!") counter2++ } while counter2 < max
  13. function <?php // PHP7: scalar type hints, return type hints

    function foo(string $str): array { $result = []; for ($i = 0; $i < 10; $i++) { $result[] = $str . $i; } return $result; } $list = foo('x'); foreach ($list as $l) { echo $l; } 
 func foo(str: String) -> Array<String> { var result:[String] = [] for i in 0..<10 { result.append(str + String(i)) } return result } let list = foo("x") for l in list { print(l) }
  14. class <?php interface ExampleInterface { public function foo(): string; }

    class Base { protected $bar = 'bar'; } class Example extends Base implements ExampleInterface { public function __construct() { echo 'initialize'; } public function foo(): string { return $this->bar; } } $example = new Example(); echo $example->foo(); protocol ExampleProtocol { func foo() -> String } class Base { internal let bar = "bar" } class Example: Base, ExampleProtocol { override init() { print("initialize") } func foo() -> String { return self.bar } } let example = Example() // no need 'new' print(example.foo())
  15. Swift Application

  16. CLI with Swift2 http://techlife.cookpad.com/entry/2015/11/09/150248

  17. CLI with Swift2 http://techlife.cookpad.com/entry/2015/11/09/150248

  18. #!/usr/bin/env swift

  19. So…

  20. None
  21. Web Application PHP7 : Swift2

  22. system structure nginx php-fpm php7 index.php nginx fcgiwrap swift2 index.swift

  23. form

  24. confirm

  25. complete

  26. execute <?php $view = new View(); $app = new App($view);

    $app->run(); #!/usr/bin/env swift let view = View(); let app = App(view: view); app.run()
  27. App::run() public function run() { $params = $this->parseGetVars(); $html =

    ''; $next = $params['next'] ?? ''; switch ($next) { case 'confirm': $html = $this->view->getConfirmHtml($params); break; case 'complete': $html = $this->view->getCompleteHtml($params); break; default: $html = $this->view->getFormHtml($params); } $this->response($html); } func run() { let params = self.parseGetVars() var html = "" let next = params["next"] ?? "" switch next { case "confirm": html = self.view.getConfirmHtml(params) case "complete": html = self.view.getCompleteHtml(params) default: html = self.view.getFormHtml(params) } self.response(html); }
  28. htmlspecialchars private function renderTemplate( string $html, array $params ): string

    { $replaceKey = []; $replaceValue = []; foreach ($params as $key => $value) { $replaceKey[] = '/{{ ' . $key . ' }}/'; $replaceValue[] = htmlspecialchars( $value, ENT_QUOTES , 'UTF-8' ); } $replaceKey[] = '/{{ action }}/'; $replaceValue[] = 'index.php'; return preg_replace( $replaceKey, $replaceValue, $html); } private func renderTemplate(html: String, params: Dictionary<String, String>) -> String { let pattern = "\\{\\{ action \\}\\}" let replace = "index.swift" var html = html.stringByReplacingOccurrencesOfString(pattern, withString: replace, options: NSStringCompareOptions.RegularExpressionSearch, range: nil) for (key, value) in params { let pattern = "\\{\\{ " + key + " \\}\\}" let replace = self.htmlspecialchars(value) html = html.stringByReplacingOccurrencesOfString(pattern, withString: replace, options: NSStringCompareOptions.RegularExpressionSearch, range: nil) } return html; } private func htmlspecialchars(var html: String) -> String { let replaceDef: Dictionary<String, String> = [ "\"": "&quot;", "'": "&#039;", "<": "&lt;", ">": "&gt;", ] for (key, value) in replaceDef { let pattern = key; let replace = value; html = html.stringByReplacingOccurrencesOfString(pattern, withString: replace, options: NSStringCompareOptions.RegularExpressionSearch, range: nil) } return html }
  29. other code https://github.com/suzuki/learning-swift2-with-php7

  30. Conclusion

  31. • Swift2 can use in web applications, maybe.

  32. perfect http://perfect.org/

  33. • Let's use the new syntax of PHP7. • It

    would be helpful to learn other programing languages.
  34. Thank you

  35. Reference • Swift2Ͱ࡞ΔίϚϯυϥΠϯπʔϧ • http://techlife.cookpad.com/entry/2015/11/09/150248 • Swift 2ඪ४ΨΠυϒοΫ • http://tatsu-zine.com/books/swift2-hyojyun-guide

    • PHP7ͰมΘΔ͜ͱ ——ݴޠ࢓༷ͱΤϯδϯͷվળϙΠϯτ • http://www.slideshare.net/hnw/phpcon-kansai20150530 • Apple͕SwiftΛΦʔϓϯιʔεԽ • http://www.infoq.com/jp/news/2015/06/swift-opensourced