Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Ouzo Goodies
askalon
post
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 30.12.2014

Ostrzeżenie: (0%)
-----


Hej,

Wyodrębniliśmy z frameworka Ouzo najciekawsze funkcjonalności. Dla tych, którzy nie chcą migrować całego projektu (MVC i ORM-a) na Ouzo, teraz wystarczy tylko dodać zależność do Ouzo Goodies i używać m.in. płynnych asercji, mockowania, extractora i wielu innych przydatnych utili.

Więcej na:
https://github.com/letsdrink/ouzo-goodies

Kilka przykładów:

Fluent arrays:
  1. $result = FluentArray::from($users)
  2. ->map(Functions::extractField('name'))
  3. ->filter(Functions::notEmpty())
  4. ->unique()
  5. ->toArray();


Fluent functions:
  1. $product = new Product(['name' => 'super phone']);
  2.  
  3. $function = FluentFunctions::extractField('name')
  4. ->removePrefix('super')
  5. ->prepend(' extra')
  6. ->append('! ')
  7. ->surroundWith("***");
  8.  
  9. $result = Functions::call($function, $product); //=> '*** extra phone! ***'


Extract (from Functions):
  1. $cities = Arrays::map($users, Functions::extract()->getAddress('home')->city);


Clock:
  1. $string = Clock::now()
  2. ->plusYears(1)
  3. ->plusMonths(2)
  4. ->minusDays(3)
  5. ->format();


Comparators:
  1. $product1 = new Product(['name' => 'b']);
  2. $product2 = new Product(['name' => 'c']);
  3. $product3 = new Product(['name' => 'a']);
  4.  
  5. $result = Arrays::sort([$product1, $product2, $product3], Comparator::compareBy('name'));


Fluent assertions for arrays:
  1. $animals = ['cat', 'dog', 'pig'];
  2. Assert::thatArray($animals)->hasSize(3)->contains('cat');


Fluent assertions for strings:
  1. Assert::thatString("Frodo")
  2. ->startsWith("Fro")
  3. ->endsWith("do")
  4. ->contains("rod")
  5. ->doesNotContain("fro")
  6. ->hasSize(5);


Mocking:
  1. $mock = Mock::mock();
  2. Mock::when($mock)->someMethod('arg')->thenReturn('result');
  3.  
  4. $result = $mock->someMethod('arg');
  5.  
  6. $this->assertEquals("result", $result);


Zapraszamy do korzystania i czekamy na konstruktywny feedback (IMG:style_emoticons/default/smile.gif)

Ten post edytował askalon 30.12.2014, 14:09:55
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
askalon
post
Post #2





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 30.12.2014

Ostrzeżenie: (0%)
-----


Mógłbyś jeszcze wkleić klasy DictionaryTable i DictionaryEntryTable?
Go to the top of the page
+Quote Post
b4rt3kk
post
Post #3





Grupa: Zarejestrowani
Postów: 1 933
Pomógł: 460
Dołączył: 2.04.2010
Skąd: Lublin

Ostrzeżenie: (0%)
-----


Cytat(askalon @ 15.01.2015, 10:53:19 ) *
Mógłbyś jeszcze wkleić klasy DictionaryTable i DictionaryEntryTable?


W zasadzie są to tylko zwykłe modele, oba wyglądają niemal identycznie, różnią się tylko sekwencją i nazwą:

  1. <?php
  2. namespace Application\Model;
  3.  
  4. use Zend\Db\TableGateway\TableGateway;
  5.  
  6. class DictionaryEntryTable extends \Extensions\Model implements \Extensions\Interfaces\Model
  7. {
  8. protected $sequence = "public.dictionary_entry_id_seq";
  9.  
  10. public function __construct(TableGateway $tableGateway)
  11. {
  12. parent::__construct($tableGateway);
  13. }
  14.  
  15. public function getTable()
  16. {
  17. return new DictionaryEntry();
  18. }
  19.  
  20. public function addRow(DictionaryEntry $table)
  21. {
  22. return parent::addRow($table);
  23. }
  24.  
  25. }
Go to the top of the page
+Quote Post
askalon
post
Post #4





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 30.12.2014

Ostrzeżenie: (0%)
-----


Cytat(b4rt3kk @ 15.01.2015, 11:07:43 ) *
W zasadzie są to tylko zwykłe modele, oba wyglądają niemal identycznie, różnią się tylko sekwencją i nazwą (...)


No to w takim razie bardziej potrzebowałbym zobaczyć klasę Model, bo w tych rzeczywiście nie ma "mięsa" (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
b4rt3kk
post
Post #5





Grupa: Zarejestrowani
Postów: 1 933
Pomógł: 460
Dołączył: 2.04.2010
Skąd: Lublin

Ostrzeżenie: (0%)
-----


Cytat(askalon @ 15.01.2015, 12:00:45 ) *
No to w takim razie bardziej potrzebowałbym zobaczyć klasę Model, bo w tych rzeczywiście nie ma "mięsa" (IMG:style_emoticons/default/smile.gif)


Proszę, klasa modelu. (IMG:style_emoticons/default/smile.gif)

  1. <?php
  2.  
  3. namespace Extensions;
  4. use Zend\Db\TableGateway\TableGateway;
  5.  
  6. class Model {
  7.  
  8. protected $tableGateway;
  9.  
  10. /**
  11.   * Sekwencja dla danego modelu (musi być określona w klasie dziedziczącej)
  12.   *
  13.   * @var string
  14.   */
  15. protected $sequence = null;
  16.  
  17. /**
  18.   * Nazwa klucza głównego tabeli (domyślnie id)
  19.   *
  20.   * @var string
  21.   */
  22. protected $primary_key = 'id';
  23.  
  24. public function __construct(TableGateway $tableGateway)
  25. {
  26. $this->tableGateway = $tableGateway;
  27. }
  28.  
  29. public function getConnection()
  30. {
  31. return $this->tableGateway->getAdapter()->getDriver()->getConnection();
  32. }
  33.  
  34. public function fetchAll($where = null)
  35. {
  36. $resultSet = $this->tableGateway->select($where);
  37.  
  38. return $resultSet;
  39. }
  40.  
  41. public function fetchSelect($select)
  42. {
  43. $resultSet = $this->tableGateway->selectWith($select);
  44.  
  45. return $resultSet;
  46. }
  47.  
  48. public function addRow($table, $skipNull = true)
  49. {
  50. $cols = array_keys(get_object_vars($table));
  51. $data = array();
  52.  
  53. foreach ($cols as $col) {
  54. if ($skipNull AND $table->{$col} === null AND $col !== 'id') {
  55. continue;
  56. }
  57. $data[$col] = $table->{$col};
  58. }
  59.  
  60. if (empty($data['id'])) {
  61. // pobranie kolejnej wartości dla sekwencji
  62. if ($this->sequence === null) {
  63. throw new Exception('Nie podano nazwy sekwencji dla tej tabeli, mimo iż zawiera klucz główny.');
  64. }
  65.  
  66. $connection = $this->getConnection();
  67. $sql = "SELECT nextval('{$this->sequence}') nextval";
  68. $seq = $connection->execute($sql)->current();
  69.  
  70. $data['id'] = $seq['nextval'];
  71. }
  72.  
  73. $this->tableGateway->insert($data);
  74.  
  75. return (empty($data['id']) ? null : $data['id']);
  76. }
  77.  
  78. public function update($data, $where)
  79. {
  80. $this->tableGateway->update($data, $where);
  81. }
  82.  
  83. public function findOne($primary_key)
  84. {
  85. if (empty($this->primary_key)) {
  86. throw new Exception('Tabela nie posiada określonego klucza głównego.');
  87. }
  88.  
  89. $row = false;
  90.  
  91. if (!empty($primary_key)) {
  92. $resultSet = $this->tableGateway->select(array($this->primary_key => $primary_key));
  93. $row = $resultSet->current();
  94. }
  95.  
  96. return $row;
  97. }
  98.  
  99. public function getTableGateway()
  100. {
  101. return $this->tableGateway;
  102. }
  103.  
  104. }
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.10.2025 - 01:27