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%)
-----


@b4rt3kk wydawało mi się, że przykłady są self-explanatory (IMG:style_emoticons/default/smile.gif)

Natomiast bardzo chętnie mogę Ci pokazać, gdzie mógłbyś tego użyć, jeśli wkleisz jakiś swój kod np. test jednostkowy gdzie testujesz coś na arrayach. ZF1/2 nie powinien być przeszkodą.
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:19:56 ) *
@b4rt3kk wydawało mi się, że przykłady są self-explanatory (IMG:style_emoticons/default/smile.gif)

Natomiast bardzo chętnie mogę Ci pokazać, gdzie mógłbyś tego użyć, jeśli wkleisz jakiś swój kod np. test jednostkowy gdzie testujesz coś na arrayach. ZF1/2 nie powinien być przeszkodą.


Hmm, może lepiej przedstawie coś nad czym ostatnio pracuję i mi powiesz, czy można to tutaj jakoś zastosować? (IMG:style_emoticons/default/smile.gif)

  1. <?php
  2. namespace Extensions;
  3.  
  4. /**
  5.  * Klasa słownika.
  6.  *
  7.  * Mapowanie słownikowe dla wiersza
  8.  * source - źródło
  9.  * idField - szukany klucz
  10.  * where - warunki początkowe
  11.  * nameFields - wartości, które chcemy pobrać
  12.  * separator - separator pobranych wartości
  13.  */
  14. class Dictionary {
  15.  
  16. protected $serviceMenager;
  17. /**
  18.   * Warunek where dla zapytania
  19.   *
  20.   * @var \Zend\Db\Sql\Where
  21.   */
  22. protected $where;
  23. protected $model;
  24. /**
  25.   * Zapytanie
  26.   *
  27.   * @var \Zend\Db\Sql\Select
  28.   */
  29. protected $select;
  30.  
  31. protected $nameFields;
  32. protected $separator;
  33. protected $idField;
  34. /**
  35.   * Potraktuj zmienną source jako id słownika
  36.   *
  37.   * @var boolean
  38.   */
  39. protected $sourceAsId;
  40.  
  41. public function __construct($serviceMenager, $sourceAsId = false)
  42. {
  43. $this->serviceMenager = $serviceMenager;
  44. $this->where = new \Zend\Db\Sql\Where();
  45. $this->select = new \Zend\Db\Sql\Select();
  46. $this->sourceAsId = $sourceAsId;
  47. }
  48.  
  49. public function setSource($source, $id = 'order', $where = array('ghost IS NOT TRUE'), $nameFields = array(), $separator = ' ')
  50. {
  51. // ustaw separator dla pobranych wartości
  52. $this->separator = $separator;
  53. $this->idField = $id;
  54.  
  55. if (class_exists($source)) {
  56. // źródłem wyszukiwania jest tabela
  57. $this->model = $this->serviceMenager->get($source);
  58. $this->nameFields = $nameFields;
  59. } else {
  60. // źródłem wyszukiwania jest słownik
  61. $this->model = $this->serviceMenager->get('Application\Model\DictionaryEntryTable');
  62. // znajdź id słownika dla podanego klucza
  63. $modelDictionary = $this->serviceMenager->get('Application\Model\DictionaryTable');
  64. // pobierz włączony język
  65. $locale = $this->serviceMenager->get('translator');
  66. $lang = $locale->getTranslator()->getLocale();
  67. $key = ($this->sourceAsId === true ? 'id' : 'symbol');
  68. $rowDictionary = $modelDictionary->fetchAll(array($key => $source, 'ghost IS NOT TRUE', 'language' => $lang))->current();
  69.  
  70. if ($rowDictionary !== false) {
  71. $this->where->equalTo('id_dictionary', $rowDictionary->id);
  72. } else {
  73. // klucz słownika nie istnieje
  74. $this->where->literal('false');
  75. }
  76.  
  77. $this->nameFields = array('entry');
  78. }
  79.  
  80. // uwzględnienie warunków where
  81. if (sizeof($where) > 0) {
  82. foreach ($where as $key => $condition) {
  83. if (!is_numeric($key)) {
  84. $this->where->equalTo($key, $condition);
  85. } else {
  86. $this->where->literal($condition);
  87. }
  88. }
  89. }
  90.  
  91. $tableIdentifier = $this->model->getTableGateway()->getTable();
  92. $this->select->from($tableIdentifier);
  93. $this->select->where($this->where);
  94.  
  95. return $this;
  96. }
  97.  
  98. public function getDictionary()
  99. {
  100. $data = $this->model->fetchSelect($this->select);
  101. $dictionary = array();
  102.  
  103. if (sizeof($data) > 0) {
  104. foreach ($data as $row) {
  105. $field = '';
  106. foreach ($this->nameFields as $name) {
  107. $field .= $row->{$name} . $this->separator;
  108. }
  109.  
  110. $dictionary[$row->{$this->idField}] = rtrim($field, $this->separator);
  111. }
  112. }
  113.  
  114. return $dictionary;
  115. }
  116.  
  117. }
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: 11.10.2025 - 23:20